Java 扫描程序使用.hasNext()调用字符串输入豁免

Java 扫描程序使用.hasNext()调用字符串输入豁免,java,java.util.scanner,Java,Java.util.scanner,我正试图做一个while循环来为扫描器输入添加一个豁免,但无法解决它 我正在尝试创建下面的异常,并在字符串大小为(1)时调用它 这就是我试图建立的概念: public static void main(String[] args){ Scanner keyboard = new Scanner(System.in); ArrayList<String> exception = new ArrayList<String>(); while (keyboard.hasN

我正试图做一个while循环来为扫描器输入添加一个豁免,但无法解决它

我正在尝试创建下面的异常,并在字符串大小为(1)时调用它

这就是我试图建立的概念:

public static void main(String[] args){ 
Scanner keyboard = new Scanner(System.in); 
ArrayList<String> exception = new ArrayList<String>();

while (keyboard.hasNextString()) {
    exception.add(keyboard.nextString());
    System.out.println(exception);
}
publicstaticvoidmain(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
ArrayList异常=新建ArrayList();
while(keyboard.hasNextString()){
exception.add(keyboard.nextString());
System.out.println(例外);
}

}

在java.util.Scanner中没有“nextString()”或“hasNextString()”这样的方法。如果我错了,请纠正我

我想你是想用

keyboard.hasNext()

因此,完整的while循环将是:

while (keyboard.hasNext()) {
    exception.add(keyboard.next());
    System.out.println(exception);
}

Scanner
没有任何名为
hasNextString
nextString
的方法。您将它们误认为是
hasNext
next
。当您扫描文件时,
hasNext
方法很有用,但在这种情况下,它不是必需的

以下是应如何做到这一点:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("[\\n\\r]");// Add new line character as delimiter

        ArrayList<String> list = new ArrayList<>();
        String input = "";
        do {
            input = keyboard.next();
            if (input.length() == 1) {
                list.add(input);
            }
        } while (input.length() > 0);

        // Display the list
        System.out.println(list);
    }
}

不确定您在这里要做什么,但本例从键盘收集数据行,直到输入的值没有长度为止。如果输入的值的长度为1,则会将其添加到异常列表中。然后,它打印异常列表的内容

List<String> exceptions = new ArrayList<String>();
try (Scanner keyboard = new Scanner(System.in)) {
  while (true) {
    System.out.print("Enter value: ");
    String exception = keyboard.nextLine().trim();
    if (exception.length() == 1) {
      exceptions.add(exception);
    } else if (exception.length() == 0) {
      break;
    }
  }
} 
        
for (String exception : exceptions) {
  System.out.println(exception);
}
List exceptions=new ArrayList();
try(扫描仪键盘=新扫描仪(System.in)){
while(true){
系统输出打印(“输入值:”);
字符串异常=keyboard.nextLine().trim();
if(exception.length()==1){
例外。添加(例外);
}else if(exception.length()==0){
打破
}
}
} 
for(字符串异常:异常){
System.out.println(例外);
}

希望这是有帮助的。

那么到底是什么问题?那么,您想检查
如果keyboard.nextString().length()==1
?不断得到:错误:无法找到符号while(keyboard.hasNextString()){^@dee_man您是想使用
while(keyboard.hasNext())
?是的。这就是我试图使用的,试图捕获从扫描仪输入中可能获得的任何字符串。这有意义吗?那么如何将字符串输入捕获到arraylist中?@dee_man查看编辑
a
Hello
b
c
World
d

[a, b, c, d]
List<String> exceptions = new ArrayList<String>();
try (Scanner keyboard = new Scanner(System.in)) {
  while (true) {
    System.out.print("Enter value: ");
    String exception = keyboard.nextLine().trim();
    if (exception.length() == 1) {
      exceptions.add(exception);
    } else if (exception.length() == 0) {
      break;
    }
  }
} 
        
for (String exception : exceptions) {
  System.out.println(exception);
}