有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有扫描仪的Java应用程序中出现字符串错误。Next()与NextLine()以及为什么我会出错?

我的Java应用程序有问题。应用程序需要执行以下操作:


(1)提示用户输入一个字符串,该字符串包含由逗号分隔的两个字符串。(1磅)

可接受的字符串示例: 吉尔,艾伦 吉尔,艾伦 吉尔,艾伦 例:

输入字符串:吉尔,艾伦

(2)如果输入字符串不包含逗号,则报告错误。继续提示,直到输入有效字符串。注意:如果输入包含逗号,则假定输入还包含两个字符串。(2个临时秘书处)

例:

输入字符串:吉尔·艾伦 错误:字符串中没有逗号 输入字符串:吉尔,艾伦

(3)从输入字符串中提取两个单词并删除所有空格。将字符串存储在两个单独的变量中并输出字符串。(2个临时秘书处)

例:

输入字符串:吉尔,艾伦 第一个字:吉尔 第二个词:艾伦

(4)使用循环,扩展程序以处理多行输入。继续,直到用户输入q退出。(2个临时秘书处)

例:

输入字符串:吉尔,艾伦 第一个字:吉尔 第二个词:艾伦

输入字符串:Golden,Monkey 第一个字:金色 第二个词:猴子

输入字符串:华盛顿特区 第一个词:华盛顿 第二个字:DC

输入输入字符串:q


我的代码:

package parsestrings;

import java.util.Scanner;

public class ParseStrings {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in); // Input stream for standard input
        Scanner inSS = null;                   // Input string stream
        String lineString = "";                // Holds line of text
        String firstWord = "";                 // First name
        String secondWord = "";                  // Last name
        boolean inputDone = false;             // Flag to indicate next iteration

        // Prompt user for input
        System.out.println("Enter input string: ");

        // Grab data as long as "Exit" is not entered
        while (!inputDone) {

            // Entire line into lineString
            lineString = scnr.next();

            // Create new input string stream
            inSS = new Scanner(lineString);

            // Now process the line
            firstWord = inSS.next();

            // Output parsed values
            if (firstWord.equals("q")) {
                System.out.println("Exiting.");

                inputDone = true;

                if (firstWord.matches("[a-zA-Z]+,[a-zA-Z]+")) {
                    System.out.print("Input not two comma separated words");
                }
            } else {
                secondWord = inSS.next();

                System.out.println("First word: " + firstWord);
                System.out.println("Second word: " + secondWord);

                System.out.println();
            }
        }
        return;
    }
}

返回的错误为:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at parsestrings.ParseStrings.main(ParseStrings.java:53)
Java Result: 1

共 (1) 个答案

  1. # 1 楼答案

    Scanner.next文件说:

    Throws: NoSuchElementException - if no more tokens are available

    例外情况的原因:

    lineString = scnr.next(); // this is reading just one word (token)
    //...
    inSS = new Scanner(lineString); // this creates a new scanner with a string consisting in just one word
    firstWord = inSS.next(); // this reads the word loaded in the inSS scanner
    //...
    secondWord = inSS.next(); // here is the problem. There are no more words left in this scanner (tokens) and throws the exception.
    

    因此,如果您更改这一行,它将起作用:

    lineString = scnr.next();
    

    为此:

    lineString = scnr.nextLine();
    

    Scanner.nextLine将按预期读取整行内容。无论如何,用户可以输入一个单词。因此,在继续之前验证输入会很好。您可以这样做:

    lineString = scnr.nextLine();
    
    if(lineString == null || lineString.length() == 0 || lineString.split("\\s+").length < 2){
        System.out.println("2 words required. Try again:");
        continue;
    }
    

    这里我使用正则表达式来验证输入中是否有多个单词