コード内の2番目の区切り文字に何が間違っているかについての洞察がありますか?私は、コードやカンマで区切りの数字、または空白やカンマを実行すると、それは完全に正常に動作しますが、私は、単一の空白とそれを実行しようとすると、私はこのエラーを取得する:デリミタとしての空白文字の連結
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);
}
}
を:これはあまりにも動作するはず
EDIT
:最後のものだけがあなたのような単一のパターンを使用する必要があります考えられています入力されました。 –