简体   繁体   English

程序无法从文件读取并保存到变量

[英]Program unable to read from file and save to variable

When I run my code it says that there is an InputMismatchException? 当我运行我的代码时,它说有一个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? 适用于前两个读取行,但是我尝试读取int和双行,但不会读取,并且字符串行实际上未将任何内容读入变量,它为空,因为它不打印任何内容在system.out.println(a + b)...有什么提示吗?

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. 这是因为当您使用next(),nextInt()和nextDouble()时,它不会移至新行。 Only newLine() moves the cursor to the next line. 只有newLine()将光标移动到下一行。 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). next(),nextInt(),nextDouble(),nextLong()等...都在任何空格(包括行尾)之前停止。

That is because you have in file: 那是因为您有文件:

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

Where \\n represents new line character. 其中\\n代表换行符。

When you call first time 第一次打电话时

innfil.nextLine().charAt(0)

it reads A , and scanner reading points to first \\n 它读取A ,而扫描仪读取点指向第一个\\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 . 它读取直到\\nnextLine()读取直到\\n并将扫描器读取指针放在\\n ),并使读取指针超过\\n Reading pointer will be at C in next line. 读指针将在下一行的C

Then you call 然后你打电话

innfil.nextInt()

duh! h! scanner can't recognize Canis as integer, input mismatch! 扫描仪无法识别Canis为整数,输入不匹配!

According to the documentation on Scanner.nextLine() it 根据Scanner.nextLine()上的文档
" Advances this scanner past the current line and returns the input that was skipped. " 将此扫描程序前进到当前行之外,并返回被跳过的输入。

So, after calling char a=innfil.next().charAt(0); 因此,在调用char a=innfil.next().charAt(0); the "cursor" is at the end of the first line. “光标”在第一行的末尾。 Calling String b=innfil.nextLine(); 调用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). 读取直到当前行的末尾(没有要读取的所有内容),并前进到下一行(实际的String所在的位置)。

Solution
You need to advance to the next line before calling String b=innfil.nextLine(); 在调用String b=innfil.nextLine();之前,需要前进到下一行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. 虽然Scanner.nextInt()Scanner.nextDouble()的行为方式相同Scanner.next() ,你不面对同样的问题,因为这些方法将读取下一个完整标记(其中“ 一个完整标记的前后其后跟匹配定界符模式 “) 的输入和空白字符(例如换行符)都被视为定界符。 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. 在关闭PrintWriter之前,请尝试调用flush()。 EDIT: sorry I was wrong here because I was thinking of the auto line flush. 编辑:对不起,我在这里错了,因为我在考虑自动行刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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