Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

java string variable using .next() or .nextLine()

the following is my sources code:

package functiontest;

import java.io.BufferedWriter;
import java.io.File;

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

public class FunctionTest {

public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
    int option;
    String cname, cpassword="testpassword",chp="010-000000";

//        cname="clerk test";

    System.out.println("1. add");
    System.out.println("2. delete");
    option = scan.nextInt();
    switch(option)
    {
        case 1:
            System.out.print("Enter clerk name:");
            cname = scan.nextLine();
            File cfile = new File("clerk/"+cname+".txt");
               FileWriter cwrite;
               try
               {
                   cwrite = new FileWriter(cfile);
                   BufferedWriter cbuf = new BufferedWriter(cwrite);
                   cbuf.write("clerk name:" +cname);
                   cbuf.write("clerk password:"+cpassword);
                   cbuf.write("clerk handphone number:"+chp);
                   cbuf.flush();
                   cbuf.close();
                   System.out.println("The clerk profile create completely");
               }catch(IOException e)
               {
                   System.out.println("Error! clerk profile cannot create");
               }
            ;break;
        case 2:
            String dclerk;
//                dclerk = "clerk test";
                System.out.print("
Please enter clerk name for delete:");
                dclerk = scan.next();
                File dcfile = new File("clerk/"+dclerk+".txt");
                if(!dcfile.exists()) 
                {
                    System.out.println("Error! the clerk profile not exist");
                }
                try
                {
                    dcfile.delete();
                    System.out.println(dclerk + "'s prifle successful delete");
                }catch(Exception e)
                {
                    System.out.println("Something wrong! " + dclerk +" profile cannot delete");
                    };break;
        }
    }
}

i cannot the enter the variable name cname

        cname = scan.nextLine()

when i run the program it show the result as below:

run:
1. add
2. delete
1
Enter clerk name:   The clerk profile create completely

when i use .next():

cname = scan.next()

it cannot read the

cname

with spaces, example

clerk test 

it will read

clerk 

only how should i do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem is that the line

option = scan.nextInt();

does not read the new line character after the integer. So that new line is finally read when you do nextLine(), later on.

To fix this, you should add an extra

scan.nextLine()

after the call to nextInt(), then use

cname = scan.nextLine();

when you want to read the clerk's name.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...