如何仅返回扫描字符串中的数字? “不兼容类型"错误.爪哇 [英] How to return only digits from a scanned string? "incompatible types" error. java

查看:69
本文介绍了如何仅返回扫描字符串中的数字? “不兼容类型"错误.爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编译我的代码,但是上一个代码(如果不是)则出现不兼容错误".我试图在该代码中说:如果输入是数字和字母的混合,则仅返回数字",但这是告诉我我做的事情有问题,我无法弄清楚./p>

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

推荐答案

用户伊朗的权利.无论如何,您的程序有点不干净.我建议您摆脱注释,并使用变量名告诉读者它们应该包含的内容.您想要的可能是这样:

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

示例控制台日志:

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

这篇关于如何仅返回扫描字符串中的数字? “不兼容类型"错误.爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆