1

I have a string variable that is set equal to userInput.nextLine(), I want to check if this string contains anything other than numeric values.

if(string has non-numeric) {
    break;    
}
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
  • Try String#match. http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#matches(java.lang.String) – Shobit Nov 16 '13 at 05:34
  • If this is homework, most likely the answer wanted would be to iterate through the string and check it char by char – Zavior Nov 16 '13 at 05:41

4 Answers4

2

Try Catch may be another alternative way.

try{
   Long.parseInt(string);

do what ever youy need to do with your number value.

}catch(NumberFormatException ex){

}
Dipak
  • 6,532
  • 8
  • 63
  • 87
  • This was my initial thought, I have used these on some other assignments in AP CS but my teacher said to try to avoid them because they are "expensive" is this true? –  Nov 16 '13 at 06:07
  • Don't know about "expensive", you should ask to teachers then and figure out "complexity" of it, also try with a worst case of your problem. Don't forget to share that... – Dipak Nov 16 '13 at 07:02
  • 1
    As far as I know the try catch methode might actually be faster then most other checks. the parser runs in O(n) steps (that is one step per letter in the input) and the exceptions works very fast O(1)(no matter the input it's always 1 step). In fact because a number of exceptions are done by the CPU rather than java code some exceptions can actually be faster then if then statements (trough the difference is very small both only take around 1/millionth of a second). – Thijser Nov 16 '13 at 07:14
  • @Nick hope you got what you are looking for. – Dipak Nov 16 '13 at 09:18
0

use string.matches(regex) function. The regular expression would be \\d+ and check against whither the output is false.

  1. The \d is known as Predefined Character Classes and an expression X+: means that X occurs one or more times, which is known as Quantifiers.

  2. if you wish to allow sign(+ or -) to be counted as part of the numeric value, you should use "[\\+\\-]?\\d+" expression which will match against input string "+523" or "-563". Check the Pattern class documentation for more details about this expression.

  3. If you wish to allow "." in the numeric value as in the case of decimal point, Then you should use regular expression: "[\\+\\-]?(\\d+|\\d*\\.\\d+)" which will match against all type of numeric input like "1254", "-.256", "+58.235" etc.

Choose Any one of the pattern which will satisfy your need and than match against your input string checking wither it results in false: meaning that not a valid numeric input.

String pattern = "[\\+\\-]?(\\d+|\\d*\\.\\d+)" ; // or only \\d+ 
                                                //or [\\+\\-]?\\d+
    if(!string.matches(pattern)) // not a valid numeric input    {

    }
Sage
  • 15,290
  • 3
  • 33
  • 38
0

If you want to show off, use a String.matches() with a regular expression. Or you could iterate over it, char by char, using String.charAt().

Robert
  • 139
  • 4
0

I think this will do

String str1 = "123456";
        System.out.println(str1.matches("[^0-9]+"));

Output will be false

and if you do

System.out.println(str1.matches("[0-9]+"));

Output will be true

AJ.
  • 4,526
  • 5
  • 29
  • 41