Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default I Can Not Seem to Get This to Work

    So I've been trying to build a simple 'program' (Wouldn't even call it a program) but I am struggling with the strings part, I have past experience in coding but not in Java so I have a problem with checking if the string that the user enters is correct to what I've entered in the code and expected the user to write.
    It bassicly always writes the error message which could be found in row 34, here's the full code, thanks for any assistance.

    Java Code:
    
    import java.util.Scanner;
    
    public class SomethingITried {
    
    	static Scanner getInput = new Scanner(System.in);
    	
    	public static void main(String[] args) {
    		
    		System.out.println("What's your age?");
    		if(getInput.hasNextInt()) {
    			int age = validAge(getInput.nextInt());
    			if(age == 0) {
    			} else {
    				System.out.printf("So you were born in %d right?", 2015-age);
    				System.out.println("\nIf so write YES, if not write NO.");
    				if(getInput.hasNextInt()) System.out.println("Please enter YES or NO only."); else {
    					getResponse(getInput.nextLine());
    				}
    			}	
    		} else System.out.println("Please enter a number.");
    	}
    
    	public static int validAge(int age) {
    		
    		if(age == 0 || age > 70) {
    			System.out.println("Please enter a valid age.");
    			return age = 0;
    		} else return age;
    	}
    	
    	public static String getResponse(String response) {
    		if(response != "NO" || response != "YES") {
    			System.out.println("ENTER ONLY YES or NO!");
    		} else if(response == "YES") System.out.println("It appears we are right!"); else System.out.println("NO way! The computer is NEVER wrong.");		
    			return response;
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: I Can Not Seem to Get This to Work

    Java Code:
        if(response != "NO" || response != "YES") {
    Don't compare Strings with ==.
    Use the equals() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: I Can Not Seem to Get This to Work

    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: I Can Not Seem to Get This to Work

    Quote Originally Posted by Tolls View Post
    Java Code:
        if(response != "NO" || response != "YES") {
    Don't compare Strings with ==.
    Use the equals() method.

    Java Code:
    public static String getResponse(String response) {
    		if(!response.equals("NO") || !response.equals("YES")) {
    			System.out.println("ENTER ONLY YES or NO!");
    		} else if(response == "YES") System.out.println("It appears we are right!"); else if(response == "NO") System.out.println("NO way! The computer is NEVER wrong.");		
    			return response;
    	}

    I've tried this out but I can't seem to get it to work, I've also tried the .equals() method in the else if and else statements with no success though, what appears to be the problem?

    And regarding this, I've got experience with variables therefore I didn't read the second and third links you gave me but the first one was actually what I needed and is what Tolls said above, but I still didn't manage to get it to work properly.
    Last edited by OxigEN.; 03-03-2015 at 03:49 PM.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: I Can Not Seem to Get This to Work

    Well you replaced only half of the == statements with equals(). Half effort is only half results.

    plus the first if statement is always true. You want it to fire if the input is not equal to both NO and YES. Take close notice of how I phrased that and then look at your code to spot a noticeable difference.
    Last edited by gimbal2; 03-03-2015 at 03:51 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: I Can Not Seem to Get This to Work

    Quote Originally Posted by gimbal2 View Post
    Well you replaced only half of the == statements with equals(). Half effort is only half results.

    plus the first if statement is always true. You want it to fire if the input is not equal to both NO and YES. Take close notice of how I phrased that and then look at your code to spot a noticeable difference.
    This probably sounds stupid asf but, eh..
    I've tried this since I've taken a look what what you've said and you said 'You want it to fire if the input is not equal to both NO and YES' so I've used the && statement because it's bassicly this AND that, which isn't supposed to work and it didn't and at first I've used || which is pretty much saying 'or' so it's like if you haven't entered YES or you haven't entered NO then this message will come up, I might've understood you wrong though, but here's the code that I've wrote just to check if what you said about the AND would work, I guess what I wrote is wrong and I didn't understand you, any further help would be greatly appreciated.

    Java Code:
    public static String getResponse(String response) {
    		if((!response.equals("NO")) && (!response.equals("YES"))) {
    			System.out.println("ENTER ONLY YES or NO!");
    		} else if(response.equals("YES")) System.out.println("It appears we are right!"); else if(response.equals("NO")) System.out.println("NO way! The computer is NEVER wrong.");		
    			return response;
    	}
    Last edited by OxigEN.; 03-03-2015 at 04:02 PM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I Can Not Seem to Get This to Work

    First, are you entering in all caps? Second, De Morgan might be proud of you but that is not the object of the code. Hence,

    Java Code:
    if (answer.equals("NO")) {
        // do something
    } else if (answer.equals("YES")) {
       // do something else
    } else {
      // ask for either yes or no.
    }
    Much easier to read.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: I Can Not Seem to Get This to Work

    Java Code:
    public static String getResponse(String response) {
    		if((!response.equals("NO")) && (!response.equals("YES"))) {
    			System.out.println("ENTER ONLY YES or NO!");
    		} else if(response.equals("YES")) {
    			System.out.println("It appears we are right!"); 
    		} else if(response.equals("NO")) {
    			System.out.println("NO way! The computer is NEVER wrong.");
    		}
    		return response;
    	}
    So, what problem are you having with the above code?
    I've laid it out a bit better so we can see exactly what's happening.

    ETA: Of course Jim has laid it out far better than me...:P
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: I Can Not Seem to Get This to Work

    My pro-tip for today: always use curly braces for if-blocks. The day will come you add a second statement and wonder why it is executed all the time.

    Bad example:
    Java Code:
    if ( someCondition) System.out.println("foo");
        System.out.println("bar");
    "bar" is always printed, regardless of someCondition.

    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Code for readability.
    Source: https://groups.google.com/forum/#!ms...w/oITtSkZOtoUJ

    Java Code:
    if ( !response.equals("NO")  && !response.equals("YES") ) {
       System.out.println("ENTER ONLY YES or NO!");
    }
    else if ( response.equals("YES")) {
       System.out.println("It appears we are right!");
    }
    else if( response.equals("NO")) {
       System.out.println("NO way! The computer is NEVER wrong.");    
    }   
    return response;
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: I Can Not Seem to Get This to Work

    Well thanks everyone, now it's more readable but the problem still occurs, I assume it's because of rows 17-18, take a look at my code:

    Java Code:
    import java.util.Scanner;
    
    public class EverythingIveLearnedSoFar02032015 {
    
    	static Scanner getInput = new Scanner(System.in);
    	
    	public static void main(String[] args) {
    		
    		System.out.println("What's your age?");
    		if(getInput.hasNextInt()) {
    			int age = validAge(getInput.nextInt());
    			if(age == 0) {
    			} else {
    				System.out.printf("So you were born in %d right?", 2015-age);
    				System.out.println("\nIf so write YES, if not write NO.");
    				if(getInput.hasNextInt()) System.out.println("Please enter YES or NO only.");
    				else {
    					getResponse(getInput.nextLine());
    				}
    			}	
    		} else System.out.println("Please enter a number.");
    	}
    
    	public static int validAge(int age) {
    		
    		if(age == 0 || age > 70) {
    			System.out.println("Please enter a valid age.");
    			return age = 0;
    		} else return age;
    	}
    	
    	public static String getResponse(String response) {
            if((!response.equals("NO")) && (!response.equals("YES"))) {
                System.out.println("ENTER ONLY YES or NO!");
            } else if(response.equals("YES")) {
                System.out.println("It appears we are right!"); 
            } else if(response.equals("NO")) {
                System.out.println("NO way! The computer is NEVER wrong.");
            }
            return response;
        }
    }

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I Can Not Seem to Get This to Work

    And here is a test. Assuming everything that has been discussed works just fine, the output says that I was born in 2076. Now how is that possible?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: I Can Not Seem to Get This to Work

    Quote Originally Posted by jim829 View Post
    And here is a test. Assuming everything that has been discussed works just fine, the output says that I was born in 2076. Now how is that possible?

    Regards,
    Jim
    That's because you've entered -61 because -(-) results in a +, and what the code does is bassicly reduces the age you entered from 2015 therefore if you enter a minus it results in a plus, I've not yet achieved a higher level of skill in java, therefore I am not quite sure of how to fix it.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I Can Not Seem to Get This to Work

    The secret lies in your validAge method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: I Can Not Seem to Get This to Work

    Quote Originally Posted by jim829 View Post
    The secret lies in your validAge method.

    Regards,
    Jim
    Java Code:
    public static int validAge(int age) {
    		
    		if(age <= 0 || age > 70) {
    			System.out.println("Please enter a valid age.");
    			return age = 0;
    		} else return age;
    	}
    I fixed it, thought it'd be harder than that, thanks.
    But that still doesn't answer my original question which regards my last function(getReponse).

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: I Can Not Seem to Get This to Work

    PS: to not trigger the psychopath, this also works:

    Java Code:
            if(response.equals("YES")) {
                System.out.println("It appears we are right!");
            } else if(response.equals("NO")) {
                System.out.println("NO way! The computer is NEVER wrong.");
            } else {
                System.out.println("ENTER ONLY YES or NO!");
            }

    As to the "remaining problem", there is still a common pitfall in your code. Before any solutions are discussed its best that you see what is happening for yourself. System.out the response value in getResponse().

    Java Code:
    public static String getResponse(String response) {
    
       System.out.println("RESPONSE IS: " + response);
       
       ...
    }
    Then try to run your code and see what is printed out. You'll be surprised.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I Can Not Seem to Get This to Work

    Sprinkling print statements throughout your program is the first best way to debug. Perhaps you should print out the responses before you check them to see what they look like. And as I said earlier, your test is case sensitive. And please follow Surfman's advice and use curly braces (even for single statements). It can save you lots of time in the future.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: I Can Not Seem to Get This to Work

    Quote Originally Posted by gimbal2 View Post
    PS: to not trigger the psychopath, this also works:

    Java Code:
            if(response.equals("YES")) {
                System.out.println("It appears we are right!");
            } else if(response.equals("NO")) {
                System.out.println("NO way! The computer is NEVER wrong.");
            } else {
                System.out.println("ENTER ONLY YES or NO!");
            }

    As to the "remaining problem", there is still a common pitfall in your code. Before any solutions are discussed its best that you see what is happening for yourself. System.out the response value in getResponse().

    Java Code:
    public static String getResponse(String response) {
    
       System.out.println("RESPONSE IS: " + response);
       
       ...
    }
    Then try to run your code and see what is printed out. You'll be surprised.
    So, I've gotten nothing pretty much, lol.
    It wrote 'THE RESPONSE IS: ' with just blank there.

    But I've then added this to the above(When I first wrote the code I've placed it originally inside it but I thought it had nothing to do with it therefore I removed it, guess I was wrong):

    Java Code:
    public static String getResponse(String response) {
    		response = getInput.nextLine();
    		if(response.equals("YES")) {
    		    System.out.println("It appears we are right!");
    		} else if(response.equals("NO")) {
    		    System.out.println("NO way! The computer is NEVER wrong.");
    		} else {
    		    System.out.println("ENTER ONLY YES or NO!");
    		}
    		System.out.println("RESPONSE IS: " + response);
            return response;
        }
    }
    I'm speaking about line number two, thanks for everyone who helped me!

    By the way can you spot any more errors or bugs? Because I've scanned the code and I can't find any, thanks again!

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: I Can Not Seem to Get This to Work

    Now you've just created weird code. You pass a response to the method and then you toss it away immediately and ask for new input. Do you at least understand why you were getting an empty response initially?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    OxigEN. is offline Member
    Join Date
    Nov 2014
    Posts
    28
    Rep Power
    0

    Default Re: I Can Not Seem to Get This to Work

    Quote Originally Posted by gimbal2 View Post
    Now you've just created weird code. You pass a response to the method and then you toss it away immediately and ask for new input. Do you at least understand why you were getting an empty response initially?
    Ahh **Sighs deeply** just as I thought I understood it, and no I actually don't understand why's that happening sadly.

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I Can Not Seem to Get This to Work

    I think you are several posts too late.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 1 of 2 12 LastLast

Similar Threads

  1. I can't get jar to work on the CLI
    By Mr Wogan in forum New To Java
    Replies: 4
    Last Post: 12-21-2012, 12:51 PM
  2. Replies: 2
    Last Post: 11-25-2012, 02:51 AM
  3. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  4. Getting SWT app to work on a different OS
    By Ravioli in forum SWT / JFace
    Replies: 0
    Last Post: 02-20-2011, 09:35 AM
  5. Cant seem to get this to work
    By nivekeleets in forum Java 2D
    Replies: 4
    Last Post: 11-13-2010, 07:09 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
  •