简体   繁体   中英

Concatenation of white spaces as a delimter

Can someone give some insight as to what is wrong with my second delimiter in the code? When I run the code and separate numbers with a comma, or a whitespace and a comma it works perfectly fine, but when I try to run it with a single whitespace, I get this error:

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)

My Code:

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);
    }
}

the delimiters do not stack up. Only the last one is considered you should use a single pattern like:

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

EDIT

This should work too:

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

The exception kind of explain itself. Always refer the api doc:

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

public int nextInt()

Scans the next token of the input as an int. An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.

Returns: the int scanned from the input

Throws:

InputMismatchException - if the next token does not match the Integer > regular expression, or is out of range

NoSuchElementException - if input is exhausted

IllegalStateException - if this scanner is closed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM