如何仅返回扫描字符串中的数字? “不兼容的类型”错误。 java的

时间:2014-11-10 12:06:06

标签: java incompatibility

我尝试编译我的代码,但是我的上一个代码出现了“不兼容的错误”(否则如果)。我试图在那段代码中说,“如果输入是数字和字母的混合只返回数字”,但它告诉我,我所做的事情有问题,我无法理解。< / p>

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

3 个答案:

答案 0 :(得分:2)

n[i] = scan.nextLine();

您正在为String变量分配一个String。你不能这样做。

您应该显示一条错误消息,提示用户只输入数字。如果您希望向用户显示无效输入,请将String返回的scan.nextLine()存储在String变量中。

答案 1 :(得分:0)

用户 Eran 是对的。无论如何,你的程序有点不洁净。我建议你摆脱评论并使用变量名称告诉读者他们应该包含什么。你想要的可能就是:

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

示例控制台日志:

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 :(得分:0)

下面是从string中过滤数字的示例..代码有点乱......希望你能得到这个想法。

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

    }
}