使用 .next() 或 .nextLine() 的 java 字符串变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20181887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:47:50  来源:igfitidea点击:

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

java

提问by wesleylim1993

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:\t");
            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:\t" +cname);
                   cbuf.write("clerk password:\t"+cpassword);
                   cbuf.write("clerk handphone number:\t"+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("\nPlease enter clerk name for delete:\t");
                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

        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():

当我使用 .next():

cname = scan.next()

it cannot read the

它无法读取

cname

with spaces, example

带空格,示例

clerk test 

it will read

它会读

clerk 

only how should i do?

只是我该怎么办?

回答by Dawood ibn Kareem

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.

不读取整数后的换行符。因此,当您这样做时nextLine(),最终会读取该新行,稍后。

To fix this, you should add an extra

要解决此问题,您应该添加一个额外的

scan.nextLine()

after the call to nextInt(), then use

调用后nextInt(),然后使用

cname = scan.nextLine();

when you want to read the clerk's name.

当你想读店员的名字时。