Results 1 to 3 of 3
  1. #1
    Googol is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default boolean isDigit() inside if statement

    Im including the entire method as I'm sure improvements can be made, but my problem is with an if statement containing the boolean isDigit(). Even when the boolean evaluates to false the statements following are run. The method runs through an array and appends all of the numerical values to a StringBuilder.

    Java Code:
       public static String extractNumbers(String line)
       {
          StringBuilder str = new StringBuilder();
          for (char i: line.toCharArray())
          {      
            if (Character.isDigit(i));  //also tried (Character.isDigit(i) == true)
            {
              str.append(i);
              System.out.println(Character.isDigit(i));
            }
          }
          return str.toString();
        }

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: boolean isDigit() inside if statement

    You have a semicolon directly after your if-statement. If you delete it, then the condition will be applied to the statements inside the braces.

    Quote Originally Posted by Googol View Post
    Im including the entire method as I'm sure improvements can be made, but my problem is with an if statement containing the boolean isDigit(). Even when the boolean evaluates to false the statements following are run. The method runs through an array and appends all of the numerical values to a StringBuilder.

    Java Code:
       public static String extractNumbers(String line)
       {
          StringBuilder str = new StringBuilder();
          for (char i: line.toCharArray())
          {      
            if (Character.isDigit(i));  <-------  Delete this semicolon
            {
              str.append(i);
              System.out.println(Character.isDigit(i));
            }
          }
          return str.toString();
        }
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    Googol is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  2. Replies: 3
    Last Post: 08-08-2011, 05:07 PM
  3. "boolean isDigit(char ch)" not working...please help
    By cc11rocks in forum New To Java
    Replies: 7
    Last Post: 02-06-2011, 01:40 AM
  4. Return Statement and Boolean Help
    By GhostShaman in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 11:15 AM

Posting Permissions

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