0

I want to write a program is for checking a real number, so i input "99aa", it says it is a right, but in fact, it should be wrong. i have check many time and i still can't fix the problem. can some one give me some hints?

public class jj {
    public static void main( String[] args ) {

        String num;

        // Create a Scanner object for console input
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the number: ");
        num = new String( input.nextLine() );

        for ( int i=0; i<=num.length(); i++ ) {
        int j = num.charAt(i);

            if (j>57 || j<42 || j==44 || j==47 ) {
            System.out.print("This is not a real number.");
            break;
            } else 

            System.out.print("This is a real number.");
            break;
        }
    }
}
Jonik
  • 80,077
  • 70
  • 264
  • 372
Billy Lo
  • 15
  • 5
  • 3
    Are you aware that you will break your for loop at the first iteration ? If your input 99aa, you will check that 9 is a digit, print its a number and break your loop. In fact, every input that starts with a digit will print valid number. – Alexis C. Dec 07 '13 at 15:39
  • possible duplicate of [How to check if a string is numeric?](http://stackoverflow.com/questions/14206768/how-to-check-if-a-string-is-numeric) – nhgrif Dec 07 '13 at 15:40
  • Your brackets are wrong. Your for loop terminates after one iteration, no matter what – Blub Dec 07 '13 at 15:43
  • are you considering `negative values` in real number ? – exexzian Dec 07 '13 at 15:44
  • I'm voting to put on hold, as questions involving missing brackets are likely not of widespread interest to future readers. Nevertheless, there's plenty of answers below. – halfer Dec 07 '13 at 18:09

5 Answers5

2

I commented what's wrong with your logic but don't reinvent the wheel.

Use NumberUtils.isNumber from org.apache.commons.lang.math.NumberUtils :

Checks whether the String a valid Java number. Valid numbers include hexadecimal marked with the 0x qualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).

if(NumberUtils.isNumber(num)) 
     System.out.println("This is a valid number");
else        
     System.out.println("This is not a valid number");

Alternatively, if you want to check that you have only digits in your String, you can use
NumberUtils.isDigits:

Checks whether the String contains only digit characters.

boolean valid = NumberUtils.isDigits(num);
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
1

Your logic is wrong.

try this instead

if ((j >= 48 && j <= 57) || j==44 || j==47 ) {

}

You want to check whether it is between 48 (0) and 57 (9), boundaries included.

See the ascii table.

Sidenotes:

  • You're allowing j==47. 47 is /, dot is 46. What one do you want?
  • Your second break; will leave the iteration after the first cycle.
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
1

Try,

  char ch = num.charAt(i);

  if (!Character.isDigit(ch)) {
       System.out.print("This is not a real number.");
      break;
  }
Masudul
  • 21,823
  • 5
  • 43
  • 58
1

I'd just like to point out some logic trouble that's giving you some fits:

if (j>57 || j<42 || j==44 || j==47 ) {
    System.out.print("This is not a real number.");
    break;
    } else 
        System.out.print("This is a real number.");
    break;
}

First of all, nevermind the problems with the if check. Jeroen Vannevel's answer covers this.

After any number returns true on the if check, you print the error and break; the loop. This is fine (assuming we fix the if check). You don't need to check every digit if you know the first one is wrong, you can quit checking.

But your else prints a message guaranteeing that the whole number is real despite just checking a single letter.

And then the break; isn't contain in the if or the else (not your brackets and my indentation that makes it more clear). No matter what happens, you'll break; after a single iteration.

What you need should look something more like this:

boolean numberFlag = true;
for ( int i=0; i<=num.length(); i++ ) {
    int j = num.charAt(i);

    if ((j >= 48 && j <= 57) || j==44 || j==47 ) {
        numberFlag = false;
        break;
    }
}

if(numberFlag) {
    // logic when a valid number is checked
} else {
    // logic when an invalid number is checked
}

We can't say whether num is a valid number of not until we've checked every single character in the string.


And please be sure to check @ZouZou's answer, as this is what you should really be doing.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
0

You could think about this in terms of Characters and implement the following:

if (Character.isDigit(num.charAt(i))) {
//do something
}
Tdorno
  • 1,571
  • 4
  • 22
  • 32