import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class MainProgram {
public static void main(String[] args ) throws IOException{
String seconds = " ";
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("/Users/mohammadmuntasir/Downloads/customersfile.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
boolean first = true;
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String s = s2.next();
if (first == true){
seconds = s;
first = false;
}
}
}
System.out.println(Integer.parseInt(seconds)); // causes ERROR?
}
}
I am trying to read a number from a text file which is in the first line by itself. I made an integer called seconds that will take in the first number and will be parsed into an integer. But I always get a numbers exception error and that I can't parse it. When I display s as a string, it displays a number without spaces next to it. Can anyone explain why this happens?
Here is the stacktrace:
Exception in thread "main" java.lang.NumberFormatException: For input string: "300"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at MainProgram.main(MainProgram.java:29)
secondswill remain as" "which of course is not a number. Try debugging it