Results 1 to 11 of 11
  1. #1
    smileybear33 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default Need help - Here is an assignment I've been working on for two days.

    Having problems with this assignment:

    Java Code:
    public class StringAssignment {
    	
    	/**
    	 * Look through every character of the String s.
    	 * The string may begin with a '-' but does not need to
    	 * Then, if there are only 0-9 and at most 1 '.' then s
    	 * is a number, and return true
    	 * 
    	 * 
    	 * @param s, the string to be tested
    	 * @return true if is a number, or false otherwise
    	 */
    	public static boolean isStringANumber(String s){
    		//s.length() tells you how many characters are in the file
    		//s.charAt(index) gives you the character at spot index
    		//you can compare characters with the ==, for example if (c == '0')
    
    		//If the character is not a 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, or . it is not a number
    		//If if is a . and there has already been 1 period in the file, it is not a number
    
    		//You can know early if it is not a number, but only after
    		//checking the entire string can you know if it is a number
    		
    		// Integer x initialized.
    		int x = 0;
    		// For loop counting the entire s.length.
    		for (int i = 0; i < s.length(); i++){
    			// c becomes whatever s.charAt is.
    			char c = s.charAt(i);
    			//Finds first period then counts x up one.
    			if (c == '.'){
    					x++;
    					//If more than one period is found returns false.
    				if (x >= 2){
    				}
    			return false;
    			}
    			//Checks if c isn't the number 0 - 9 and returns false if it isn't.
    			if ( c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' ){
    			return false;
    			}
    		}
    		//everything else is true.
    		return true;
    	}
    The result I'm getting from the grader I'm provided with reads this:

    Testing isStringANumber
    For some reason, -99999 is not being returned as a valid number
    Problem with integers
    For some reason, -99999.9 is not being returned as a valid number
    Problem with doubles

    In case it is helpful here is a copy of the grader that is used to test this file.

    Java Code:
    public static void main(String[] args) {
    		int points = 100;
    		int deduct = 100 / 6;
    		boolean b;
    		String answer;
    
    		System.out.println("Testing isStringANumber");
    		// integers
    		try {
    			for (int i = -99999; i < 99999; i++) {
    				b = StringAssignment.isStringANumber("" + i);
    				if (b == false) {
    					System.out.println("For some reason, " + i
    							+ " is not being returned as a valid number");
    					throw new Exception();
    				}
    			}
    		} catch (Exception e) {
    			System.out.println("\tProblem with integers");
    			points -= deduct;
    		}
    		// doubles
    		try {
    			for (double i = -99999.9; i < 99999.9; i += 1.9) {
    				b = StringAssignment.isStringANumber("" + i);
    				if (b == false) {
    					System.out.println("For some reason, " + i
    							+ " is not being returned as a valid number");
    					throw new Exception();
    				}
    			}
    			b = StringAssignment.isStringANumber(".5");
    			if (b == false) {
    				System.out
    						.println("For some reason, .5 is not being returned as a valid number");
    				throw new Exception();
    			}
    
    		} catch (Exception e) {
    			System.out.println("\tProblem with doubles");
    			points -= deduct;
    		}
    		// Invalids
    		try {
    			String[] invalidStrings = { "a", "-567a", "567n", "127.0.0.1",
    					".5a", "7.7.", ".77.", "{}", "Hello World", "---",
    					"a908.3", "5670'", "+=", "$$$", "123.456)5567",
    					"435-555-5555" };
    			for (int i = 0; i < invalidStrings.length; i++) {
    				b = StringAssignment.isStringANumber(invalidStrings[i]);
    				if (b == true) {
    					System.out.println("For some reason, " + invalidStrings[i]
    							+ " is being returned as a valid number");
    					throw new Exception();
    				}
    			}
    		} catch (Exception e) {
    			System.out.println("\tProblem with invalid strings");
    			points -= deduct;
    		}
    
    		if (points < 5) {
    			points = 0;
    		}
    		System.out.println("Total points " + points);
    	}
    
    }
    Any help would be appreciated. Thanks in advance.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Testing isStringANumber
    For some reason, -99999 is not being returned as a valid number

    In the for loop where you test a string the first character should be treated differently from all the others. At the moment you return false if you hit a second '.' otherwise you return false if the character is not a digit. However the first character is different because it can be '-'.

    * Look through every character of the String s.
    * The string may begin with a '-' but does not need to
    * Then, if there are only 0-9 and at most 1 '.' then s
    * is a number, and return true

    The comment at the start of the StringAssignment class is a bit odd as it allows the following to be numbers.

    ""
    "-"
    "."
    "007"

    This may just how the assignment is posed so there's nothing you can do about it.

  3. #3
    smileybear33 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default Thanks for help.

    Thanks for the help, I think I see what you mean. I'll take a closer look at that spot and if there is nothing I can do because of the way the assignment is I'll turn in what I've got. I wasn't even thinking of the '-' as a string I was trying to do something like:

    if (c < 0 ){
    return true
    }

    and it wasn't working. I'll try it differently.

    Thanks again.

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

    Default

    If you improve your indentation you might be able to spot errors like this.
    Java Code:
    if (c == '.'){
        x++;
        //If more than one period is found returns false.
        if (x >= 2){
        }
        return false;
    }

  5. #5
    smileybear33 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Found a solution to the problem I was having but it opened a new problem. Here is the way I changed the code:

    Java Code:
    if ( c != '-' && c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' ){
    			return false;
    			}
    Now I'm getting this error:

    For some reason, --- is being returned as a valid number
    Problem with invalid strings

    I'll keep at it but any help would be appreciated.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    For some reason, --- is being returned as a valid number

    OK. So now your checking for '-' and regarding it as a valid character. But my point before was that '-' is a valid character but only when it is the first character of the string. That is why I was suggesting that you treat the first character differently from all the others.

    ------

    I agree with Junky's point about indentation. And as the logic becomes more complex the chances of error (and the need for readability) increase. If *your* source code is correctly aligned but it becomes mangled when you post it the solution is to use spaces rather than tabs to indent.

  7. #7
    smileybear33 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Ok, I've actually been working on the code and hopefully fixed most of the indentation problems to make it easier. Messing with it some I've gotten rid of all the errors but one. Here is the code I've got so far:

    Java Code:
    		// int x initialize.
    		int x = 0;
    				
    		// Loop counting the entire s.length.
    		for (int i = 0; i < s.length(); i++){
    			
    			// c set to s.charAt(i).
    			char c = s.charAt(i);
    		
    			//If more than one '-' returns false.
    			if ( c == '-'){
    			x++;
    				if (x >= 2){
    				return false;
    				}
    			}
    			
    			//If more than one '.' returns false.
    			if (c == '.'){
    			x++;
    				if (x >= 2){
    				return false;
    				}
    			}
    			
    			//If not equal to - and . and 0-9 returns false.
    			if ( c != '-' && c != '.' && c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' ){
    			return false;
    			}
    		}
    		
    		//everything else is true.
    		return true;
    	}

    And it turns out this is the only error that is left:


    Testing isStringANumber
    For some reason, -99999.9 is not being returned as a valid number
    Problem with doubles

    Turns out I also had this error as well with the last code I posted but didn't see it but I've gotten rid of the '---' problem. As it stands if I turn this in I'll get 84 points instead of the 64 points I'd have gotten with the last one so if I can't figure it out I'm ok. Thanks for all your help everyone. It has come in handy a lot. If you do think of something to help me with this please let me know. I don't have to turn this in for a few hours.

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

    Default

    You are using the same variable to count dots and hyphens.

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

    Default

    Also, please stop posting in red. It makes my eyes bleed.

  10. #10
    smileybear33 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    I had changed the variable earlier and it didn't solve anything so I thought it must not matter. It must have been some of the changes that I made later that made it matter not the variable. When I created a second variable and assigned plugged it into the code I got 100%. Thanks. That was the one thing I was missing. Now I can turn the assignment in and have time to relax from my assignment. Thanks for the help. I'm glad I found this sight today, you all have been great.

  11. #11
    smileybear33 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Sorry, didn't get that post till after I sent the reply. Will stop in the future.

Similar Threads

  1. Problem working out Assignment
    By maknib in forum New To Java
    Replies: 12
    Last Post: 03-14-2011, 09:50 PM
  2. Replies: 8
    Last Post: 01-06-2011, 06:33 PM
  3. Game Help.working on it for days,.
    By xSkittlesx in forum Java Applets
    Replies: 12
    Last Post: 05-31-2010, 03:24 PM
  4. Been working on a code for days
    By Link01 in forum Java Applets
    Replies: 5
    Last Post: 05-19-2010, 03:55 PM
  5. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 09:06 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
  •