TennesseeTickler
TennesseeTickler

Reputation: 11

Parsing a number throws: Exception in thread "main" java.lang.NumberFormatException: For input string

I am making an object oriented program that takes an array of object type Date and writes it to a file. But I am having a problem parsing a number. I can't see where my int and String variables are being mixed up:

public static Date[] createMe() throws FileNotFoundException
{
   Scanner kb = new Scanner(System.in);
   System.out.print("please enter the name of the file: ");
   String fileName= kb.nextLine();
   Scanner fin = new Scanner(new File(fileName));
   int count = 0;
   while (fin.hasNextLine()){
      count++;
      fin.nextLine();
   }
   fin.close();
   Date[] temp = new Date[count];
   fin = new Scanner(fileName);
   while(fin.hasNextLine()){
      String line = fin.nextLine();
      String[] s = line.split("/");
      int month = Integer.parseInt(s[0]);
      int day = Integer.parseInt(s[1]);
      int year = Integer.parseInt(s[2]);
   }
   return temp;
}

I keep getting this error code and I don't know why:

please enter the name of the file: Track.java
Exception in thread "main" java.lang.NumberFormatException: For input string: "Track.java"
    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 Date.createMe(Date.java:49)
    at DateTester.main(DateTester.java:28)

I should be only inputting my string with my Scanner kb right? So why am I getting this error?

Upvotes: 0

Views: 517

Answers (2)

Peter Perháč
Peter Perháč

Reputation: 20782

problem is in this line:

fin = new Scanner(fileName);

you are creating a Scanner from a String filename. Which is the path to file that you type in. Not the file itself. you created fin Scanner correctly a few lines above that. Just do the same again.

Upvotes: 1

Florian Prud'homme
Florian Prud'homme

Reputation: 599

the problem is with your file. A java file does not begin with a number. Here is an example that works fine (I have added try( clause) to safely release file ressource) :

public static Date[] createMe() throws FileNotFoundException {

    try (PrintWriter out = new PrintWriter("filename.txt")) {
        out.print("3/1/12");
    }
    try (Scanner kb = new Scanner(System.in)) {

        String fileName = "filename.txt";
        int count = 0;
        try (Scanner fin = new Scanner(new File(fileName))) {
            while (fin.hasNextLine()) {
                count++;
                fin.nextLine();
            }
        }
        Date[] temp = new Date[count];
        try (Scanner fin = new Scanner(new File(fileName))) {

            while (fin.hasNextLine()) {
                String line = fin.nextLine();
                String[] s = line.split("/");
                int month = Integer.parseInt(s[0]);
                int day = Integer.parseInt(s[1]);
                int year = Integer.parseInt(s[2]);

                System.out.println(month + " " + day + " " + year);
            }
        }
        return temp;
    }

Result of the program :

please enter the name of the file: 3 1 12

Have a nice day

Upvotes: 0

Related Questions