问题
I tried to compile my code, but I got an "incompatible error" with my last code (else if). I was trying to say in that code, "if the input was a mixture of digits and letters return only digits", but it's telling me that there is something wrong with what I have done, and I couldn't figure it out.
import java.util.*;
public class Pr8{
public static void main(String[] args){
Scanner scan = new Scanner (System.in);
//Prompt the user for how many numbers are going to be entered
System.out.print("* Please write how many numbers are going to be entered: ");
if (!scan.hasNextInt())
System.out.println("- Sorry your entery was not correct. The compiler except's digits only.");
else {
int a = scan.nextInt(); //a is the scanned number of the user request
int[] n = new int[a]; //is an array to declare a variable as much as the user entered
int num = 1; //to show the sorting number of the string when printing
//prompt the user to enter a mixture of digits and letters
for (int i = 0; i < a; i++){
System.out.print("* Please enter a string #" + num++ + ": ");
if (scan.hasNextInt()){ //check if the input has only integers
n[i] = scan.nextInt();
System.out.println("- " + n[i] + " = " + n[i]);
}//if
else if (!scan.hasNextInt()){ //if the input was a mixture of digits and letters, return only the digits
n[i] = scan.nextLine();
System.out.println("- there is letters");
}//else
}//for
}//else if
}//main
}//Pr8
回答1:
User Eran ist right. Your program is a bit unclean anyway. I suggest you get rid of the comments and use variable names which tell the reader what they are supposed to contain. What you want is probably this:
import java.util.Scanner;
public class Pr8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("How many numbers do you wish to enter? ");
while (!scanner.hasNextInt()) {
System.err.print("Again, how many numbers? ");
scanner.next();
}
int numberCount = scanner.nextInt();
int[] numbers = new int[numberCount];
for (int i = 0; i < numberCount; i++) {
String numberString;
System.out.print("Please enter string #" + (i + 1) + " containing a number: ");
scanner.nextLine();
while ((numberString = scanner.findInLine("[+-]?[0-9]+")) == null) {
System.err.print("Again, string #" + (i + 1) + " should contain a number: ");
scanner.nextLine();
}
numbers[i] = Integer.parseInt(numberString);
System.out.println("Number #" + (i + 1) + " found = " + numbers[i]);
}
}
}
Sample console log:
How many numbers do you wish to enter? weewdfsa
Again, how many numbers? 324klj
Again, how many numbers? 5
Please enter string #1 containing a number: 23
Number #1 found = 23
Please enter string #2 containing a number: sdfsdf
Again, string #2 should contain a number: werq
Again, string #2 should contain a number: sdf345df
Number #2 found = 345
Please enter string #3 containing a number: -34ewr
Number #3 found = -34
Please enter string #4 containing a number: wqweq+555
Number #4 found = 555
Please enter string #5 containing a number: xxx-11yyy
Number #5 found = -11
回答2:
n[i] = scan.nextLine();
You are assigning a String to an int variable. You can't do that.
You should probably display an error message prompting the user to enter only digits. If you wish to display the invalid input to the user, store the String
returned by scan.nextLine()
in a String
variable.
回答3:
Below is the example to filter numbers from string..code is bit messy...hope you can get the idea.
package strings;
public class ScanNumbersOnly {
public static void main(String[] args) {
StringBuilder builder=new StringBuilder();
int i=0,k=0,counter=0;
String input="4 h5i dhfg 454hdkjfg dkjfhg iudhfg";
System.out.println("String="+input);
String spaceRemover[]=input.split(" ");
for(String s:spaceRemover){
builder.append(s);
}
input=builder.toString();
char stringTocharacter[]=input.toCharArray();
int stringarray[]=new int[stringTocharacter.length];
for(char s:stringTocharacter){
int charTonumericvalue=Character.getNumericValue(s);
if(charTonumericvalue<=9){
stringarray[i]=charTonumericvalue;
counter++;
}
i++;
}
int finallist[]=new int[counter];
for(int s:stringarray){
if(s>0){
finallist[k]=s;
k++;
}
}
System.out.print("After filtiring string=");
for(int numberextractor:finallist){
System.out.print(numberextractor);
}
}
}
来源:https://stackoverflow.com/questions/26843457/how-to-return-only-digits-from-a-scanned-string-incompatible-types-error-jav