Java 读取字符串 next() 和 nextLine()
问题是我无法使用 next() 读取变量输入,因为当我尝试拆分 (.split" ") 每个空格时,数组只获取我输入的前两个单词,所以我必须使用 Keyboard.nextLine() 和拆分过程按其应有的方式工作,我得到了数组中的所有单词,但问题是,如果我使用 nextLine() 那么我必须创建另一个键盘对象来读取第一个变量(答案),这是唯一的方法我可以让它工作这里是代码
Scanner keyboard=new Scanner(System.in);
Scanner keyboard2=new Scanner(System.in);//just to make answer word
int answer=keyboard.nextInt();//if I don't use the keyboard2 here then the program will not work as it should work, but if I use next() instead of nextLine down there this will not be a problem but then the splitting part is a problem(this variable counts number of lines the program will have).
int current=1;
int left=0,right=0,forward=0,back=0;
for(int count=0;count<answer;count++,current++)
{
String input=keyboard.nextLine();
String array[]=input.split(" ");
for (int counter=0;counter<array.length;counter++)
{
if (array[counter].equalsIgnoreCase("left"))
{
left++;
}
else if (array[counter].equalsIgnoreCase("right"))
{
right++;
}
else if (array[counter].equalsIgnoreCase("forward"))
{
forward++;
}
else if (array[counter].equalsIgnoreCase("back"))
{
back++;
}
}
}
}
谢谢:)
The problem is I cant read the variable input with next() cause when I try to split (.split" ") every whitespace then the array just get the first two words I type so I had to use keyboard.nextLine() and the splitting process works the way it should work and I get all the words in the array but the problem is that If I use nextLine() then I have to create another keyboard object to read the first variable (answer) and that is the only way I can make it work here is the code
Scanner keyboard=new Scanner(System.in);
Scanner keyboard2=new Scanner(System.in);//just to make answer word
int answer=keyboard.nextInt();//if I don't use the keyboard2 here then the program will not work as it should work, but if I use next() instead of nextLine down there this will not be a problem but then the splitting part is a problem(this variable counts number of lines the program will have).
int current=1;
int left=0,right=0,forward=0,back=0;
for(int count=0;count<answer;count++,current++)
{
String input=keyboard.nextLine();
String array[]=input.split(" ");
for (int counter=0;counter<array.length;counter++)
{
if (array[counter].equalsIgnoreCase("left"))
{
left++;
}
else if (array[counter].equalsIgnoreCase("right"))
{
right++;
}
else if (array[counter].equalsIgnoreCase("forward"))
{
forward++;
}
else if (array[counter].equalsIgnoreCase("back"))
{
back++;
}
}
}
}
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
keyboard.nextLine()
放在此行之后:这是一个常见问题,通常在
nextInt()
之后使用nextLine()
方法时发生Scanner
类的方法。实际发生的情况是,当用户在
int answer = Keyboard.nextInt();
输入整数时,扫描仪将仅获取数字并保留换行符\n.因此,您需要通过调用
keyboard.nextLine();
来丢弃换行符,然后您可以调用String input = Keyboard.nextLine();
> 没有任何问题。Put
keyboard.nextLine()
after this line:This is a common problem that usually happens when you use
nextLine()
method afternextInt()
method ofScanner
class.What actually happens is that when the user enters an integer at
int answer = keyboard.nextInt();
, the scanner will take the digits only and leave the new-line character\n
. So you need to do a trick by callingkeyboard.nextLine();
just to discard that new-line character and then you can callString input = keyboard.nextLine();
without any problem.我的第一个建议是将整行(以空格分隔)添加到
String
数组中。然后,利用 Integer.parseInt 方法将
answer
分配给数组的 0 索引。这是您的代码,已重构。
输入
输出
My first suggestion is to just add the entire line, split on a space, to a
String
array.Then, assign
answer
the 0 index of the array, utilizing theInteger.parseInt
method.Here is your code, refactored.
Input
Output