为什么Java不打印行中的所有单词(当单词添加到ArrayList时)?
当将用户输入作为一行中的单个单词打印出来时,我会得到该行中所有单词的打印输出为什么Java不打印行中的所有单词(当单词添加到ArrayList时)?,java,arraylist,println,Java,Arraylist,Println,当将用户输入作为一行中的单个单词打印出来时,我会得到该行中所有单词的打印输出 System.out.println(userInput.next()); 但是,当我将Individual单词添加到ArrayList时,我似乎得到了随机单词: al.add(userInput.next()); 有人能给我解释一下发生了什么事吗 谢谢 这是代码的完整副本: import java.util.*; public class Kwic { public static void main
System.out.println(userInput.next());
但是,当我将Individual单词添加到ArrayList时,我似乎得到了随机单词:
al.add(userInput.next());
有人能给我解释一下发生了什么事吗
谢谢
这是代码的完整副本:
import java.util.*;
public class Kwic {
public static void main(String args[]){
Scanner userInput = new Scanner(System.in);
ArrayList<String> al = new ArrayList<String>();
while(userInput.hasNext()){
al.add(userInput.next());
System.out.println(userInput.next());
}
}
}
import java.util.*;
公共类Kwic{
公共静态void main(字符串参数[]){
扫描仪用户输入=新扫描仪(System.in);
ArrayList al=新的ArrayList();
while(userInput.hasNext()){
al.add(userInput.next());
System.out.println(userInput.next());
}
}
}
您打印的不是存储在ArrayList中的值,而是对userInput.next()的另一个调用
修订版
@谢尔顿这对我有用
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
ArrayList<String> al = new ArrayList<String>();
while(userInput.hasNext()){
al.add(userInput.next());
System.out.println(al); //LINE CHANGED FROM YOUR QUESTION
}
}
publicstaticvoidmain(字符串[]args){
扫描仪用户输入=新扫描仪(System.in);
ArrayList al=新的ArrayList();
while(userInput.hasNext()){
al.add(userInput.next());
System.out.println(al);//问题的行已更改
}
}
我用输入测试了你的代码
1234567890
然后我按下回车键,得到:
2
4.
6.
8.
0
userInput.next()在添加到ArrayList的和由System.out.println捕获的之间交替使用。我会这样写:
import java.util.*;
public class Kwic {
public static void main(String args[]){
Scanner userInput = new Scanner(System.in);
List<String> al = new ArrayList<String>();
while(userInput.hasNext()){
al.add(userInput.next());
}
System.out.println(al);
}
}
import java.util.*;
公共类Kwic{
公共静态void main(字符串参数[]){
扫描仪用户输入=新扫描仪(System.in);
List al=新的ArrayList();
while(userInput.hasNext()){
al.add(userInput.next());
}
系统输出打印项次(al);
}
}
首先将所有值存储到数组列表中,然后将其打印出来,这对您更有利。您现在要做的是打印对userInput.next()
的另一个调用,该调用可能存在,也可能不存在
while(userInput.hasNext()){
al.add(userInput.next());
}
for(String s : al) {
System.out.println(s);
}
因为会使用来自扫描仪的下一个令牌。因此,当您:
al.add(userInput.next());
System.out.println(userInput.next());
实际上,您正在使用扫描仪中的两个令牌。第一个正在添加到阵列列表
,另一个正在打印到系统.out
。一种可能的解决方案是将令牌存储在局部变量中,然后将其添加到数组并打印:
while (userInput.hasNext()) {
String token = userInput.next();
al.add(token);
System.out.println(token);
}
尝试打印ArrayList中的值
for(String s : al){
System.out.println(s);
}
您不仅没有打印行中的所有单词,也没有将所有单词添加到ArrayList
。因为按照您的编码方式,它会将每个备选单词添加到ArrayList
中,并打印备选单词。我认为我对自己的解释不够清楚。当我使用System.out.println(userInput.next())时;我没有把行中的所有单词都记下来,例如“你好”不是打印“你好”,而是打印“你”。这只在我将值添加到ArrayList时发生。@Sheldon,如果我打印ArrayList而不是userInput。next()仍然有效。@Sheldon,我现在得到了它。。。这是while循环,第一个条目进入列表,第二个条目进入System.println
al.add(userInput.next()); //Adding an Item call to ArrayList
System.out.println(userInput.next()); //Printing the next userInput with a call**
for(String s : al){
System.out.println(s);
}