1

I am having trouble finding the source of my error. All I am doing is reading text from a file

public static void main(String[] args) throws Exception {
    int T;

    Scanner sc = new Scanner(new FileInputStream("problem3.txt"));

    T = sc.nextInt(); // first int in file, so T should be 2
}

and the error message shows an InputMismatchException:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at round1.Problem3.main(Problem3.java:11)

The content of problem3.txt is as following (3 lines, no spaces):

2
36
127

I have searched for other questions addressing InputMismatchException, but most have errors with 'wrong format' (trying to read ints as Strings, or vice versa). But in my case it shouldn't have a problem since the file contents are all integers.

I've also thought the error might be with the 'new line character (\n)'. So tried

T = sc.nextInt(); // error
sc.nextLine();

and the other way around

sc.nextLine();
T = sc.nextInt(); // error

Both still give the same error, on the same line.

Seems like a simple issue, but I just can't find it. Thanks in advance.


Problem solved: I changed the encoding to Cp1252 and it reads the 2. Thanks all

wO_o
  • 181
  • 3
  • 13
  • What is the output if you just read several lines with `sc.nextLine()` and print them? – Cinnam Oct 24 '15 at 21:28
  • 1
    What is printed when you print `new File("problem3.txt").getAbsolutePath()`? Is it the path of the file you actually expect to read? What is printed when you print `Files.readAllLines(Path.get("problem3.txt"))`? – JB Nizet Oct 24 '15 at 21:30
  • @Cinnam Printing sc.nextLine() shows me ÿþ2 What is this? Seems like this is why I'm getting the error. – wO_o Oct 24 '15 at 21:32
  • 2
    @ChanwOoPark What is the encoding of the problem3.txt file? – Cinnam Oct 24 '15 at 21:36
  • @Cinnam the default was set as UTF-16. Should I change this? – wO_o Oct 24 '15 at 21:39
  • What is `T` variable? Why it's not defined as int? – Klever Oct 24 '15 at 21:40
  • 3
    The Scanner constructor can take a charsetName as argument. You MUST pass the right one if the file doesn't have the platform default encoding. – JB Nizet Oct 24 '15 at 21:41

3 Answers3

0

Try to give correct path of the file. I am able to get all the values in the file

public static void main(String[] args) {
    // TODO Auto-generated method stub


    Scanner sc;
    try {
        sc = new Scanner(new FileInputStream("/Users/Zero/Desktop/problem3.txt"));


         int    T = sc.nextInt(); // first int in file, so T should be 2
         System.out.println(T);

        T = sc.nextInt();
         System.out.println(T);

         T = sc.nextInt();
         System.out.println(T);     
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

Can you check what is getting printed on the console

     StringBuffer a = new StringBuffer();
        while(sc.hasNext())
        {
        a.append(sc.nextLine());    
        }
         System.out.println(a.toString());
bond007
  • 2,434
  • 1
  • 17
  • 17
  • The file path doesn't seem to be the problem in my case. I have made other similar classes like this one reading from text files in the exact same format, but they work fine. – wO_o Oct 24 '15 at 21:41
0

It is an Encoding Problem. Try to use UTF-8 or ANSI and your Code should run fine without any problem.

MikeVe
  • 1,062
  • 8
  • 13
-1

Try FileIO, depending on what you want to read it may be a lot easier

Hans
  • 2,354
  • 3
  • 25
  • 35