1

Currently editing code to create a skip list, and i'm trying to read in a file that has the data that needs to be inserted, searched for, or deleted (denoted by i,s,d respectively).

I'm getting thrown Number Format Exceptions when trying to run it and when it prints the stacktrace in the catch statement it looks like it isn't reading the very first character, which should be an "i".

Here is what my Main code looks like currently as that seems to be where the issue resides

public static void main(String[] args) {

    Main list = new Main();

    boolean result = false;

    try {

        BufferedReader br;

        br = new BufferedReader(new FileReader("text.txt"));

        StringBuilder sb = new StringBuilder();

        String line = br.readLine();

        while (line != null) {

            sb.append(line);

            sb.append(System.lineSeparator());

            line = br.readLine();

        }

        String everything = sb.toString();

        String[] input = everything.split(",");

        for (int i = 0; i < input.length; i++) {

            String temp = input[i];
            System.out.printf("%s", input[i]);
            if (temp.startsWith("i")) {

                result = list.insert(Integer.parseInt(temp.substring(2)));

            } else if (temp.startsWith("s")) {

                Node node = list.search(Integer.parseInt(temp.substring(2)));

                if (node != null) {

                    System.out.println(temp.substring(2) + " found");

                } else {

                    System.out.println(temp.substring(2) + " not found");

                }

            } else if (temp.startsWith("d")) {

                result = list.delete(Integer.parseInt(temp.substring(2)));

                if (result == true) {

                    System.out.println(temp.substring(2) + " deleted");

                } else {

                    System.out.println(temp.substring(2) + " not deleted");

                }

            } else if (temp == "p") {

                System.out.println("For the input file ");

                System.out.println("With the RNG succeeded");

                System.out.println("The current skip list is shown below");

                list.printAll();

            }


        }

    } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

}

And here is what my example input data text file looks like

i 503
i 302
i 596
i 198
i 270
i 629
i 603
i 110
i 354
i 947
p
q

When it runs and spits back the stack trace it says

java.lang.NumberFormatException: For input string: "503
i 302
i 596
i 198
i 270
i 629
i 603
i 110
i 354
i 947
p
q
"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at Main.main(Main.java:295)

So my guess currently is that fro some reason it isn't reading the "i" in front of the 503 when it is definitely there in the text data file

3
  • 3
    Please edit your post and indent that code. You're presumably already using a code editor, so just tell that to auto-format your code, then copy-paste that into your question. And remember to include the minimal bits of java around your code so that it's a minimal reproducible example, so people can run it. Commented Mar 9, 2020 at 23:24
  • Looks like you read every line into a single stringbuffer - attempt to split it with "," (which there are none) and then attempt to parse everything after the first "i" - that would explain the error. Commented Mar 9, 2020 at 23:26
  • 1
    Just ran your code through an online "beautifier". There are lots of them online. Please use one in the future. Commented Mar 9, 2020 at 23:28

1 Answer 1

2

Problem is that you are splitting data with comma while your data have no comma at all. So when it reaches temp.substring(2), it actually uses following string and tries to parse:

503
i 302
i 596
i 198
i 270
i 629
i 603
i 110
i 354
i 947
p
q

which is whole file content (not a number).

Just replace comma (String[] input = everything.split(",");) with new line character \n (String[] input = everything.split("\n");) and it should work.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.