English 中文(简体)
2. 未经审判而没收愤怒或强食
原标题:Validating an integer or String without try-catch

Ok, I m丢失。 我必须说明如何证实愤怒,但出于某种原因,我只能使用Try-Catch方法。 我知道这是最容易的方法,因此互联网上的所有解决办法都在使用。

Java。

交易是这样,我需要有人以数字身份识别和密码。 如果两个投入中的任何一个都无效,我必须告诉它们,它们是错误的。

谁能帮助我?

最佳回答

如果我正确理解你的话,你就会看到一种愤怒或从标准投入中加以扼杀,你希望证实,愤怒实际上是一种愤怒。 http://java.sun.com/a>。 如同你的任务一样,它禁止使用例外处理方法(因此我正确理解这一点),因此不允许你利用这一内在功能,必须加以执行。

Ok. 因此,由于这是家庭工作,我不会给你完整的回答,但这里是假装:

let result = 0 // accumulator for our result
let radix = 10 // base 10 number
let isneg = false // not negative as far as we are aware

strip leading/trailing whitespace for the input string

if the input begins with  + :
    remove the  + 
otherwise, if the input begins with  - :
    remove the  - 
    set isneg to true

for each character in the input string:
    if the character is not a digit:
        indicate failure
    otherwise:
        multiply result by the radix
        add the character, converted to a digit, to the result

if isneg:
     negate the result

report the result

这里的关键是,每一位数的分数比位数直接高出几倍,因此,如果我们总是用红.乘,我们就可以正确看到str,那么每位数就具有适当的意义。 现在,如果我错了,而且你实际上能够使用捕捉物,但只是粗略地指出:

int result = 0;
boolean done = false;
while (!done){
     String str = // read the input
     try{
         result = Integer.parseInt(str);
         done = true;
     }catch(NumberFormatException the_input_string_isnt_an_integer){
         // ask the user to try again
     }
}  
问题回答

并非确切地确定,正在验证的<代码>String应当核对内容是否只包含编号,或者是否可在int的有效范围内实际代表。 解决这一问题的一个途径是围绕String的特性加以调整,并检查这些特性只是由数字构成。

由于这似乎属于家务劳动,因此,这里的假体编码很少:

define isANumber(String input):
  for (character in input):
    if (character is not a number):
      return false

  return true

您可使用>java.util.Scanner hasNextInt()>,以核实可转换成/code>而无例外。

作为一种额外特征,它能够跳出白色空间,容忍额外的垃圾(你可以检查)。

String[] ss = {
   "1000000000000000000",
   "  -300  ",
   "3.14159",
   "a dozen",
   "99 bottles of beer",
};
for (String s : ss) {
   System.out.println(new Scanner(s).hasNextInt());
} // prints false, true, false, false, true

另见:。 当错误类型进入时,我如何让扫描员放弃例外?

我用 Java1.6,为我工作:

if (yourStringVariable.trim().matches("^\d*$"))

那么,你就会发现一种积极的愤怒。

Pattern p = Pattern.compile("\d{1,}");
Matcher m = p.matcher(inputStr); 
boolean isNumber = false;

if (m.find()) {
    isNumber = true;   
}
public static boolean IsNumeric(String s)
{
    if (s == null)
        return false;
    if (s.length() == 0)
        return false;
    for (int i = 0; i < s.length(); i++)
    {
        if (!Character.isDigit(s.charAt(i)))
            return false;
    }
    return true;
}     




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签