简体   繁体   English

扫描程序仅读取字符串的第一个标记

[英]Scanner only reading 1st token of a string

New to the Java Language, Java语言的新手,

The following is my bit of code. 以下是我的一些代码。 However the whole string isn't written to the file. 但是,整个字符串不会写入文件。 Only the First token is written to the file. 仅第一个令牌写入文件。 Any Explanations ? 有什么解释吗?

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class FichierTexteWrite {

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

        try {
            System.out.println("Entrez le nom d'un fichier :");
            Scanner in = new Scanner(System.in);
            String filename = in.next();
            FileWriter fwrite = new FileWriter(filename);
            System.out.println("Entrez une phrase a memoriser");
            fwrite.write(in.next());
            System.out.println("Writing on file complete ");
            fwrite.close();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I have also tried nextLine() method but that didn't seem to help. 我也尝试过nextLine()方法,但这似乎没有帮助。 It forcefully wrote a blank caracter and terminated the program. 它强行编写了一个空白的角色,并终止了该程序。

---EDIT--- - -编辑 - -

I also tried : 我也尝试过:

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class FichierTexteWrite {

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

        try {
            System.out.println("Entrez le nom d'un fichier :");
            Scanner in = new Scanner(System.in);
            String filename = in.next();
            FileWriter fwrite = new FileWriter(filename);
            System.out.println("Entrez une phrase a memoriser");
            fwrite.write(in.nextLine());
            System.out.println("Writing on file complete ");
            fwrite.close();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

If You don't believe please compile and run the program. 如果您不相信,请编译并运行该程序。 It doesn't work. 没用 It doesn't let me input a string and goes straight into the next instructions which closes the stream and output File Written successfully. 它不允许我输入字符串,而是直接进入下一条指令,该指令关闭流并成功输出文件写入。

You should use in.nextLine() to get the whole line. 您应该使用in.nextLine()来获得整行。

fwrite.write(in.nextLine());

Use nextLine() both times: 两次都使用nextLine():

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class FichierTexteWrite {

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

        try {
            System.out.println("Entrez le nom d'un fichier :");
            Scanner in = new Scanner(System.in);
            String filename = in.nextLine(); // *** note change
            FileWriter fwrite = new FileWriter(filename);
            System.out.println("Entrez une phrase a memoriser");
            fwrite.write(in.nextLine());
            System.out.println("Writing on file complete ");
            fwrite.close();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Otherwise your nextLine() call will read the end-of-line token for the first line, the one where you get the file name, and you don't want this. 否则,您的nextLine()调用将读取第一行的行尾令牌,即您在其中获得文件名的令牌,而您不需要这样做。

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

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