Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default Help with a Method

    I need a boolean method that returns true if string represents an integer
    Here is my code..I can get this to work

    Java Code:
    public class ASD {
    	public static void main(String args[]) {
    		isInteger("230987");
    	}
    	
    	
    	public static boolean isInteger(String source) {
    		char c;
    		for (int x = 0; x < source.length(); x++) {
    			c = source.charAt(x);
    		if (c == 0 || c == 1 || c == 2 || c == 3 || c == 4 || c == 5 || c == 6 || c == 7 || c == 8 || c == 9) {
    			return true;
    		} else {
    			return false;
    			}
    		}
    		
    	}
    }
    what am I doing wrong here??

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

    Default

    In future "it doesn't work" provides zero information. Supply more details such as errors or actual beahviour/ desired behaviour.

    Java Code:
    c == 0
    You are comparing the character c to an integer value 0 and not the character value 0.

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

    Default

    By the way your logic is flawed. If the String to be checked was "9A" your code would return true.

  4. #4
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    i forgot the single quotations to denote a character, thanks..

    the error im getting is that nothing is returned..it says i need to add a return statement or change the method to void..at this point i just want the program to run and work out the logic later..

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

    Default

    What happens if the your code never enters the for loop? What gets returned?
    Java Code:
    public static boolean isInteger(String source) {
        char c;
        for (....) { // loop never enters
    
        }
        // we get here
    }
    As long as you resolve the logic error correctly, this problem will also go away.

  6. #6
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Java Code:
    public static boolean isInteger(String source) {
    		char c;
    		for (int x = 0; x < source.length(); x++) {
    			c = source.charAt(x);
    			if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9') {
    				return true;
    			} else {
    				return false;
    I tried doing that and get the same result..do I need another method for the for loop? and use an if statement in isInteger alone?

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

    Default

    You tried doing what?

    You reposted code has only fixed the comparison to a character. The logic is still wrong as you code will only ever check the first character in the String. If it is a digit return true, ignoring all the other characters in the String. Let me post 2 questions and see if they stimulate your thought process.

    When do you know that the String IS NOT a number?
    When do you know that the String IS a number?

    The order I asked those questions is very important.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can do this in a fairly tricky way with very little logic using Integer.parseInt.

  9. #9
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Sunde,
    im not suppose to use that..im just limited to using boolean methods loops etc..this is what we covered in class so far..
    Junky
    if im string is "23k9" the first loop would assignment c '2' then the if statement checks if its and return true i see what you mean by that Junky...
    so do i put the return statement outside the loop?

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

    Default

    If you answer my two questions above, where you place the return statement(s) will become more obvious.

  11. #11
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    I know this is easy but I am completely lost as to what to do...Ive been working on this problem for hours and cant figure it out..

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

    Default

    Have you answered my 2 questions.

    Post your answers here.

  13. #13
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    I know that the string is not a number when there is a character other than a digit while going through the loop

    String is a number when the whole string is comprised of digits while going through the loop

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

    Default

    Exactly.

    As you loop over the String and you get to a non-digit chacter what should you do?
    When you get to the end of the String and haven't encountered a non-digit character what will the loop do? What should you do?

  15. #15
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Exactly.

    As you loop over the String and you get to a non-digit chacter what should you do?
    When you get to the end of the String and haven't encountered a non-digit character what will the loop do? What should you do?
    If i get to a non digit character, i should return false..but how do i compare each index to a non character? i would assume
    if (c != non character) {
    return false;

    how do I express that into code? there are many non digit chars $*$&#($hjkdsf how do i account for all of those?

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

    Default

    Java Code:
    if(c < '0' || c > '9')
    if( ! Character.isDigit(c))

  17. #17
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    ok so i came up with this code

    Java Code:
    public class ASDFSA {
    	public static void main(String args[]) {
    		isInteger("01");
    	}
    	
    	
    	public static boolean isInteger(String source) {
    		char c;
    		for (int x = 0; x < source.length(); x++) {
    			c = source.charAt(x);
    			if (c < '0' || c > '9') {
    				return false;
    			} else {
    				return true;
    			}
    		}
    	}
    }
    ok i revise it to your way..its much more shorter and simple..

    when I run the program it throws this error in eclipse

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at BenKim1.ASDFSA.main(ASDFSA.java:4)

    it is telling me that I need to add a return statement...
    Last edited by bkim33; 02-16-2011 at 06:50 AM.

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

    Default

    When you get to the end of the String and haven't encountered a non-digit character what will the loop do? What should you do?

    When you get to the end of the String and haven't encountered a non-digit character what will the loop do? What should you do?

    When you get to the end of the String and haven't encountered a non-digit character what will the loop do? What should you do?

    How many times do I have to ask?

  19. #19
    bkim33 is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    you would return true if i have not encountered a non digit character

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

    Default

    Yeah, so why are you trying to return true before you get to the end?

Page 1 of 2 12 LastLast

Similar Threads

  1. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 07:46 AM
  2. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  4. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 09:37 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
  •