Results 1 to 4 of 4
Thread: Check if string is numbe
- 02-01-2011, 08:53 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Check if string is numbe
Hey all, i've been trying to work some magic here but no such luck. I have a method that tests user input for space, however i'd like to enable a test also should user input a number.
I saw some methods online using try catch, but i'm hoping I can get one excluding try catch.
Below is a snippet of my code testing whether user entered a space or hit enter key.
Java Code:public static boolean isFamName(String b){ if(b.length()==0||b==("")){ System.out.println("Please enter your family name correctly?"); return false; }else{ return true; }//close if-else statement }//close isFamName
- 02-01-2011, 09:07 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Same problem as this one.
- 02-02-2011, 12:49 AM #3
:confused:
Title says to check is String contains a number, yet posted code has nothing to do with it.
Method is called isFamName, yet all it does is check if string length is greater than 0.
The b == "" check is pointless! If the String has a zero length then it must be "". So either both conditions are true or both conditions are false.
The b == "" check is wrong as you should use the equals method instead.
It does no such thing.Below is a snippet of my code testing whether user entered a space
- 02-02-2011, 01:16 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Hi,
Do you get the answer already?
Or you can try in such way :
public static boolean isFamName(String b){
if(b.length()==0||b==null){
System.out.println("Please enter your family name correctly?");
return false;
}else{
return true;
}//close if-else statement
}//close isFamName
And since when user input String b is empty, then you will println to user to key in family name correctly and also return false, so you must call this method properly, either is if( ! isFamName){ ... } or if( isFamName){ ... }, depends on your situation you want to apply.
Thanks.
Similar Threads
-
Check if a string contains characters entered in a array
By roud9 in forum New To JavaReplies: 4Last Post: 11-05-2010, 10:43 AM -
check string inside string
By alacn in forum New To JavaReplies: 8Last Post: 07-07-2010, 12:52 PM -
How to check whether the string contains only the specified characters ????
By j_kathiresan in forum New To JavaReplies: 1Last Post: 04-30-2010, 03:21 PM -
How to check String confirmation with integer
By Basit56 in forum New To JavaReplies: 4Last Post: 01-06-2010, 11:35 AM -
check if String is an integer?
By McChill in forum New To JavaReplies: 5Last Post: 05-02-2009, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks