简体   繁体   中英

Scanner only reading 1st token of a string

New to the Java Language,

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. 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.

fwrite.write(in.nextLine());

Use nextLine() both times:

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.

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