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