简体   繁体   中英

Program unable to read from file and save to variable

When I run my code it says that there is an InputMismatchException? Works for the two first read-lines, but hwne I try to read the int and double-lines it doesn't and the string-line doesn't actually read anything into the variable, it is empty as it doesn't print anything at the system.out.println(a +b)... Any tips?

import java.util.*;
import java.io.*;

class Uke55{
    public static void main(String[]args){
    Scanner input=new Scanner(System.in);
    try{
        PrintWriter utfil=new PrintWriter(new File("minfil55.txt"));
        utfil.println('A');
        utfil.println("Canis familiaris betyr hund");
        utfil.println(15);
        utfil.printf("%.2f", 3.1415);
        utfil.close();
    }catch(Exception e){
        e.printStackTrace();
    }
    try{
        Scanner innfil=new Scanner(new File("minfil55.txt"));
        char a=innfil.next().charAt(0);
        String b=innfil.nextLine();
        System.out.println(a +b);
        int c=(int)innfil.nextInt();
        double d=(double)innfil.nextDouble();
        innfil.close();
    }catch(Exception e){
        e.printStackTrace();
    }
    }
}

That's because when you use next(), nextInt(), and nextDouble(), it doesn't go to a new line. Only newLine() moves the cursor to the next line. Do this:

try{
    Scanner innfil=new Scanner(new File("minfil55.txt"));
    char a=innfil.nextLine().charAt(0); //first error was here. calling next() only
                                        //read A and not the \r\n at the end of the 
                                        //line. Therefore, the line after this one was 
                                        //only reading a newline character and the 
                                        //nextInt() was trying to read the "Canis" line.
    String b=innfil.nextLine(); 
    System.out.println(a +b);
    int c=(int)innfil.nextInt(); 
    innfil.nextLine(); //call next line here to move to the next line.
    double d=(double)innfil.nextDouble();
    innfil.close();
}
catch(Exception e){
    e.printStackTrace();
}

next(), nextInt(), nextDouble(), nextLong(), etc... all stop right before any whitespace (including the end of a line).

That is because you have in file:

A\n
Canis familiaris betyr hund\n
15\n
3.14

Where \\n represents new line character.

When you call first time

innfil.nextLine().charAt(0)

it reads A , and scanner reading points to first \\n

Then you call

innfil.nextLine()

it reads till \\n ( nextLine() reads till \\n and puts scanner reading pointer past \\n ), and makes reading pointer past \\n . Reading pointer will be at C in next line.

Then you call

innfil.nextInt()

duh! scanner can't recognize Canis as integer, input mismatch!

According to the documentation on Scanner.nextLine() it
" Advances this scanner past the current line and returns the input that was skipped. "

So, after calling char a=innfil.next().charAt(0); the "cursor" is at the end of the first line. Calling String b=innfil.nextLine(); reads until the end of the current line (where there is nothing left to read) and advances to the next line (where the actual String is).

Solution
You need to advance to the next line before calling String b=innfil.nextLine(); :

...
char a=innfil.next().charAt(0);
innfil.nextLine();
String b=innfil.nextLine();
...

Note :
Although Scanner.nextInt() and Scanner.nextDouble() behave the same way as Scanner.next() , you don't face the same problem, because those methods will read the next complete token (where " A complete token is preceded and followed by input that matches the delimiter pattern ") and white-space characters (such as newline-characters) are considered delimiters. So, those methods will automatically advance to the next line if needed, in order to find the next complete token .

Did you check something is actually written to your file? I doubt that. Try calling flush() before you close your PrintWriter. EDIT: sorry I was wrong here because I was thinking of the auto line flush.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM