gpt4 book ai didi

java - 使用 nextLine() 字符串 java 空字符

转载 作者:行者123 更新时间:2023-11-30 04:58:59 25 4
gpt4 key购买 nike

    System.out.println("-----------------------------------------------------------");
System.out.println("Please select a task");
System.out.println("1: List employees with details");
System.out.println("2: List of clients in state");
System.out.println("3: List portfolio of client");
System.out.println("4: Release an employee");
System.out.println("5 Display stocks available for purchase/selling");
System.out.println("6: Display client details");
System.out.println("7: Buy Stock for client ");
System.out.println("0: Exit program");
System.out.println("-----------------------------------------------------------" + "\n" + "\n");

System.out.print("Input:");

Scanner scan = new Scanner(System.in);

input = scan.nextInt();

if (input == 1)
{
System.out.println("length of array list: " + employeeList.size());

int index = 0;
while ( index < employeeList.size() )
{
System.out.println(employeeList.get(index));
index++;
}
}
else if (input == 2)
{
String state_choice;
System.out.println("Please enter the abbrivation for the state:");
state_choice = scan.nextLine();

这是我当前代码的一部分,当我尝试运行时,我在 [else if (input == 2)] 处遇到问题,它不允许我输入,实际上只是忽略它。我认为这是因为上一个条目中的“\n”。有没有办法删除“\n”或多余的字符,而无需在我的条目之前放置另一个 scan.nextLine() ?在java文档中没有真正看到任何内容..

最佳答案

只需在 input = scan.nextInt(); 之后添加 scan.nextLine(); 即可。问题出在 input = scan.nextInt(); 中,它只读取整数,因此您需要 scan.nextLine(); 丢弃新行 \n 按 Enter 键生成。

关于java - 使用 nextLine() 字符串 java 空字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7572367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com