【问题标题】:Concatenation of white spaces as a delimter连接空格作为分隔符
【发布时间】:2017-03-16 01:21:51
【问题描述】:

有人可以对我在代码中的第二个分隔符有什么问题提供一些见解吗?当我运行代码并用逗号或空格和逗号分隔数字时,它工作得很好,但是当我尝试用单个空格运行它时,我收到了这个错误:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at main.Mean.main(Mean.java:28)

我的代码:

package main;

import java.util.Scanner;

public class Mean {
    @SuppressWarnings("resource")
    public static void main (String [] args){
        // Console prompts use to enter their numbers
        System.out.println("Please Enter your Numbers: ");

        // Creating a String of numbers that will be stored in the 'nums' variable
        Scanner nums = new Scanner(System.in);

       // Making 'input' equal to the
       String input = nums.nextLine();

       // Now the scanner class scans the string that was put into the variable 'input'.
       Scanner scan = new Scanner(input);

       scan.useDelimiter(",");              // Any commas are now a delimiter
       scan.useDelimiter("\\s*");           // Any combination of concatenated of whitespace is now a delimiter
       scan.useDelimiter("\\s*,\\s*");      // Any combination of concatenated of whitespace followed by a single comma followed by any number of concatenated whitespace is now a delimeter

        double total = 0.0;                 // Initializing the variable total and setting it equal to 0.0
        double counter = 0.0;               // Initializing the variable counter and setting it equal to 0.0

        while(scan.hasNextLine()){          // While the variable 'scan' still has integers left...
            total += scan.nextInt();        // Make the total equal to the old total plus the new presented integer
            counter++;                      // Add one to the 'counter' variable to keep track of the total amount of numbers

            //System.out.println("Total: "+total);      Commented out code that was used for testing
            //System.out.println("Counter: "+counter);  Commented out code that was used for testing
        }
        scan.close();                       // Closes the scanner

        double mean = total / counter;      //  Mean is equal to the total divided by the amount of numbers
        System.out.println();
        System.out.println("Average: " + mean);
    }
}

【问题讨论】:

  • 它工作正常,因为您只是使用输入的最后一个模式。

标签: java concatenation whitespace delimiter


【解决方案1】:

分隔符不堆叠。只有最后一个被认为你应该使用一个单一的模式,如:

scan.useDelimiter("\\s*,\\s*|\\s*|,");

编辑

这应该也可以:

    scan.useDelimiter("\\s*,\\s*|\\s*");

【讨论】:

    【解决方案2】:

    异常类型可以自行解释。始终参考 api 文档:

    https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()

    public int nextInt()

    将输入的下一个标记扫描为 int。一个 调用 nextInt() 形式的此方法的行为与 与调用 nextInt(radix) 相同,其中 radix 是默认值 此扫描仪的基数。

    返回:从输入扫描的 int

    投掷:

    InputMismatchException - 如果下一个标记与 Integer > 正则表达式不匹配,或者超出范围

    NoSuchElementException - 如果输入已用尽

    IllegalStateException - 如果此扫描仪已关闭

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 2013-09-07
      • 2021-01-20
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多