简体   繁体   English

将扫描仪的下一个字符串转换为Int并检查输入的文本是否为数字

[英]Convert Scanner next string to Int and check that the text entered is a number

I am trying to Convert Scanner next string to Int and check that the text entered is a number, if not number system print this is not a number but if number it is converted to int and divided by ten. 我正在尝试将Scanner的下一个字符串转换为Int,并检查输入的文本是否为数字,如果不是数字系统打印,则不是数字,但如果是数字,则将其转换为int并除以十。

If i dont check for the condition in the code, the code is running as it is supposed to. 如果我不检查代码中的条件,则代码按预期运行。 But if text is entered it is calculating the charachters index value. 但是,如果输入了文本,它将计算字符索引值。

If the condition is there then it is making calculation for each number instead of suming up the number Before making the calculation, but it dont calculate other charachters index value. 如果存在该条件,则它将对每个数字进行计算,而不是在进行计算之前对数字求和,但不会计算其他字符索引值。

Without checking for the condition: 不检查条件:

    Scanner inputtext = new Scanner(System.in); 

    System.out.print("Mata in ett tal: ");
    String number =    inputtext.next();  //Här matar användaren in text.              
    int sum = 0;
    double number2 = 10.0;

    for (int i=0; i < number.length(); i++) { //Här omvandlas texten till tal.

    sum *= 10;
    sum += (int) number.charAt(i)-(int)'0'; 
    }   

    System.out.println("Resultat: " + sum); //Här matas talet ut.
    System.out.println("Resultat delat med 10 : " + (sum/number2)); //Här divideras talet med 10.


  }

}

When checking: 检查时:

    Scanner inputtext = new Scanner(System.in); 

    System.out.print("Mata in ett tal: ");
    String number =    inputtext.next();  //Här matar användaren in text.              
    int sum = 0;
    double number2 = 10.0;

    for (int i=0; i < number.length(); i++) { //Här omvandlas texten till tal.
    char ch = number.charAt(i);

    if (ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' ||  ch == '5' || ch == '6'|| ch == '7'|| ch == '8'|| ch == '9'){ 
    sum *= 10;
    sum += (int) number.charAt(i)-(int)'0';

    } else { 
    System.out.println("Detta är inte ett tal");
    break; 
    }
    System.out.println("Resultat: " + sum); //Här matas talet ut.
    System.out.println("Resultat delat med 10 : " + (sum/number2)); //Här divideras talet med 10.            
   }

}

} }

I expect that if a string is entered containing numbers then the calculation is performed, if other charachters are added then the user is told that this is not a number 我希望如果输入的字符串包含数字,则将执行计算;如果添加了其他字符,则将告知用户这不是数字

You can use methods from Character which are : 您可以使用Character中的方法:

  • Character.isDigit 字符是数字
  • Character.isLetter 字符是字母
  • Character.isLetterOrDigit Character.isLetterOrDigit

By using this, iterate over the string and test if all characters are number or not. 通过使用它,遍历字符串并测试所有字符是否都是数字。

Also, remember that "//d" stands for a digit so if you want it to scan for a digit only you can use that. 另外,请记住,“ // d”代表一个数字,因此,如果您只想扫描一个数字,则可以使用该数字。 I know this doesn't really explain too much but I thought a reminder might help. 我知道这并不能说明太多,但我认为提醒可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM