Results 1 to 4 of 4
  1. #1
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default 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

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Same problem as this one.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    :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.

    Below is a snippet of my code testing whether user entered a space
    It does no such thing.

  4. #4
    CTheSky is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    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

  1. Replies: 4
    Last Post: 11-05-2010, 10:43 AM
  2. check string inside string
    By alacn in forum New To Java
    Replies: 8
    Last Post: 07-07-2010, 12:52 PM
  3. Replies: 1
    Last Post: 04-30-2010, 03:21 PM
  4. How to check String confirmation with integer
    By Basit56 in forum New To Java
    Replies: 4
    Last Post: 01-06-2010, 11:35 AM
  5. check if String is an integer?
    By McChill in forum New To Java
    Replies: 5
    Last Post: 05-02-2009, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •