We help IT Professionals succeed at work.

Java 1.3 string contains alpha or special characters

galaxy573
galaxy573 asked
on
I need to check in Java 1.3 if a string contains anything other than the characters 0-9.  If there is any alpha or special characters I need to handle the exception.

What is the easiest way to accomplish this?
Comment
Watch Question

Commented:
Here are two different ways.  
<% 
  String s = "3378333";
  if(!s.startsWith("-")){
                      try{
                          out.print(Integer.parseInt(s));
                      }catch(NumberFormatException nfe){
                                                        out.print("not a number");
                       }
  } else out.print("negative not allowed");


  boolean digits = true;
  for(int x = 0; x < s.length(); x++){
              if(!Character.isDigit(s.charAt(x))){
                                                digits = false;
                                                break;
              }
  }
  out.print("digits = " + digits);
%>

Open in new window

Below is enought to handle the situation.


        String s = "3378333";
        
        for(int x=0; x<s.length(); x++){
            try{
                Integer.parseInt(s);
            }
            catch(NumberFormatException e){
                System.out.println("String conaints invalid characters.");
                break;
            }
        }

Open in new window

Author

Commented:
Thank you!  This worked.

Commented:
I don't think awarding points to pramodkrjsr was fair. He posted the same method that I did before him.
Also, his code does not disallow the "-"  for negative integers.

Author

Commented:
I tried parsing through each character in the string as you suggested, however for some reason I couldn't get my code to work.  It may have been something else with my code however it just wasn't working.  I tried printing inside the if(!Character.isDigit(s.charAt(x))) statement and there was no output.  I'm not sure why however it's possible it was on my end.

Is there any way I can go back & give you partial credit?
Top Expert 2016

Commented:
Why not just do the following?
boolean valid = false;
try {
	valid = Integer.parseInt(s) >= 0;
}
catch(NumberFormatException e) {
	// valid remains false
}

Open in new window

Commented:
>for some reason I couldn't get my code to work.
Please show us your code.  I tested mine. So I know it works.  

>Is there any way I can go back & give you partial credit?  
Yes, get one of the moderators to reopen this question. Thank you.
I did ask Venabili (the zone adviser) to look at this. But she is probably busy at this time.  


CEHJ
>Why not just do the following?  
What about negative integers ?
Also, the reason that I posted the two different codes is because of Joshua Bloch's book "Effective Java"  
Item 39:
"Use exceptions only for exceptional conditions"
Top Expert 2016

Commented:
>>What about negative integers ?

The code i posted handles them (not allowed since character '-' is disallowed)

Commented:
CEHJ>The code i posted handles them  
I see now. You are right