输入字符串而不是整数(Java)

时间:2014-04-14 07:03:14

标签: java

我是一个绝对的初学者,没有任何编程语言的经验。

我写了一个程序,作为将阿拉伯数字转换为罗马数字的练习。有用。但是,如果输入字符串而不是整数,我想添加一个处理问题的部分。而且不知道该怎么做。我尝试使用try / catch,但我不知道如何正确使用它。现在程序要我两次输入一个数字。怎么办?

这是他的主要方法:

public static void main(String[] args) {

    int numArabic; 
    boolean validEntry;

    try {
            System.out.println("Enter an integer number between 1 and 3999!");
            Scanner scan = new Scanner(System.in); 
            numArabic = scan.nextInt();
            validEntry = true;
        } catch (InputMismatchException e) {
            System.out.println("Entered value is not an integer!");
        }

    System.out.println("Enter an integer number between 1 and 3999!");
    Scanner scan = new Scanner(System.in); 
    numArabic = scan.nextInt();                            

    if ((numArabic < 1) || (numArabic > 3999)) {
            System.out.println();
            System.out.print("Wrong number. ");
            System.out.print("Enter an integer number between 1 and 3999!");
            System.out.println();
            }
    else {
            String numRoman1 = toRomanOne(numArabic % 10);
            String numRoman2 = toRomanTwo(((numArabic / 10) % 10));
            String numRoman3 = toRomanThree(((numArabic / 100) % 10));
            String numRoman4 = toRomanFour(numArabic / 1000);
            System.out.print("The number " + numArabic + " is equal to: ");
            System.out.print(numRoman4+numRoman3+numRoman2+numRoman1 + ".");
    }
}

6 个答案:

答案 0 :(得分:1)

您的控制机制是真的但只能运行一次。你必须把它放在一个循环中,以便它允许用户最后输入一个整数。

boolean validEntry;

do {
   try {
      System.out.println("Enter an integer number between 1 and 3999!");
      Scanner scan = new Scanner(System.in); 
      numArabic = scan.nextInt();
      validEntry = true;
   }
   catch (InputMismatchException e) {
      validEntry = false;
      System.out.println("Entered value is not an integer!");
   }
}
while(!validEntry);

答案 1 :(得分:0)

您可以使用Scanner#hasNextInt()方法检查。

如果此扫描仪输入中的下一个标记可以解释为int值,则此方法返回true

if (scan.hasNextInt()) {
   // Do the process with Integer.
} else {
   // Do the process if it is not an Integer.
}

请注意,这将涵盖所有不是Integer的输入,而不仅仅是String

答案 2 :(得分:0)

public static void main(String[] args) {

    int numArabic; 
    boolean validEntry = false;

    while (validEntry = false){
    try {

            System.out.println("Enter an integer number between 1 and 3999!");
            Scanner scan = new Scanner(System.in); 
            numArabic = scan.nextInt();
            validEntry = true;
        }

        catch (InputMismatchException e) {
            System.out.println("Entered value is not an integer!");
            validEntry = false;
        }
    }                 

    if ((numArabic < 1) || (numArabic > 3999)) {
            System.out.println();
            System.out.print("Wrong number. ");
            System.out.print("Enter an integer number between 1 and 3999!");
            System.out.println();
            }
    else {
            String numRoman1 = toRomanOne(numArabic % 10);
            String numRoman2 = toRomanTwo(((numArabic / 10) % 10));
            String numRoman3 = toRomanThree(((numArabic / 100) % 10));
            String numRoman4 = toRomanFour(numArabic / 1000);
            System.out.print("The number " + numArabic + " is equal to: ");
            System.out.print(numRoman4+numRoman3+numRoman2+numRoman1 + ".");
    }
}

答案 3 :(得分:0)

试试这个:

     boolean b=true;
     while(b)
    {
       try 
        {
        System.out.println("Enter an integer number between 1 and 3999!");
        Scanner scan = new Scanner(System.in); 
        numArabic = scan.nextInt();
        validEntry = true;
        }
      catch (InputMismatchException e) 
         {
          System.out.println("Entered value is not an integer!");

         }
      if(validEntry) 
       {
        if ((numArabic < 1) || (numArabic > 3999)) 
        {
        System.out.println();
        System.out.print("Wrong number. ");
        System.out.print("Enter an integer number between 1 and 3999!");
        System.out.println();
        }
        else 
        {
        String numRoman1 = toRomanOne(numArabic % 10);
        String numRoman2 = toRomanTwo(((numArabic / 10) % 10));
        String numRoman3 = toRomanThree(((numArabic / 100) % 10));
        String numRoman4 = toRomanFour(numArabic / 1000);
        System.out.print("The number " + numArabic + " is equal to: ");
        System.out.print(numRoman4+numRoman3+numRoman2+numRoman1 + ".");
        }
       b=false;
   }//end of if 
 }//end of while 

答案 4 :(得分:-1)

试试这个,

您需要先将numArabic初始化为某个值。

public static void main(String[] args) {
    int numArabic = 0;
    boolean validEntry;

    try {
        System.out.println("Enter an integer number between 1 and 3999!");
        Scanner scan = new Scanner(System.in);
        numArabic = scan.nextInt();
        validEntry = true;
    }

    catch (InputMismatchException e) {
        System.out.println("Entered value is not an integer!");
    }


    if ((numArabic < 1) || (numArabic > 3999)) {

    } else {

    }

}

答案 5 :(得分:-1)

你输入的程序没有语法错误和逻辑错误。但只要求一次输入整数值只需重写代码如下。我们在catch块中附加第二个sop函数。如果输入是不匹配它会再次要求重新进入。     public static void main(String [] args){

    int numArabic; 
    boolean validEntry;

    try {
            System.out.println("Enter an integer number between 1 and 3999!");
            Scanner scan = new Scanner(System.in); 
            numArabic = scan.nextInt();
            validEntry = true;
        } catch (InputMismatchException e) {
            System.out.println("Entered value is not an integer!");


System.out.println("Enter an integer number between 1 and 3999!");
        Scanner scan = new Scanner(System.in); 
        numArabic = scan.nextInt();
        }