0

i just started to study and "play" a bit with java. I'm a Computer Engineering student (last year we studied C). However, today i had some problem with "Scanning from File". I work in Eclipse, and after i created a file text "in.txt" containing this:

20 31,12 Luca

I stuck at the same error gived by the compiler:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at it.devapp.corsojava.esempi.FileScanner.main(FileScanner.java:20)

What's the point? i can't find the error. This is the code:

public class FileScanner {

public static void main(String[] args) 

        throws FileNotFoundException { // needed for file operations

    int num1;
    double num2;
    String name;
    double sum;
    // Scanning input operations

    Scanner in = new Scanner(new File("in.txt"));

    num1 = in.nextInt();
    num2 = in.nextDouble();
    name = in.next();

    // Display
    sum = num1 + num2;
    System.out.println("The integer read is: " + num1);
    System.out.println("The floating point number read is: " + num2);
    System.out.println("The string read is: " + name);
    System.out.println("Hi " + name +", the sum of " + num1 + " and " + num2  + " is: " + sum );
    in.close();
    }
 }

Thanks to everyone! :)

Luca Pierri
  • 53
  • 1
  • 5

0 Answers0