Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By wsaryada

Thread: Help Please!

  1. #1
    Laon8orsSon is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Question Help Please!

    Ok, Im trying to get the system to output "Great!" if the user inputs "Yes" and "Oops!" if they answer "No".
    Im using buffered reader for input and the if statement. Please Help! Here is my code;

    Java Code:
    public class inputTest {
    	public static void main(String[] args) throws Exception {
    		InputStreamReader iname = new InputStreamReader(System.in);
    		BufferedReader brname = new BufferedReader(iname);
    		
    		InputStreamReader ianswer = new InputStreamReader(System.in);
    		BufferedReader branswer = new BufferedReader(ianswer);
    		
    		System.out.println("Welcome to my program to test input. What is your name?");
    		String name = brname.readLine();
    		
    		System.out.println("Hello " + name + "!");
    		
    		System.out.println("That is your name, right? (Type Yes or No)");
    		String answer = branswer.readLine();
    		
    
                    //This is where I need help!
    		if (answer == branswer.readLine(Yes)) {
    			System.out.println("Great!");
    		}else{
    			System.out.println("Oops!");
    		}
    		
    		
    		
    
    	}
    
    }
    Last edited by Laon8orsSon; 05-11-2012 at 05:13 AM. Reason: error, awesome to great

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help Please!

    What is wrong with your code? Is it causing any errors or exceptions? If so, please post the error messages? Is it not behaving appropriately? If so, then please fully describe what it's doing wrong.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Help Please!

    You can compare the answer variable with a string "Yes" or using a constant value (final field). And remember when you compare a string value you must use the equals() or equalsIgnoreCase() method provided in the String class. The == operator will not give you the correct result when comparing strings for their content equality.
    Fubarable likes this.

  5. #5
    Laon8orsSon is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help Please!

    nothing is wrong with it except the if statement. Im trying to get it so if the user inputs Yes when it says (name + "is your name, right?") it outputs Great!
    Last edited by Laon8orsSon; 05-11-2012 at 05:12 AM.

  6. #6
    Laon8orsSon is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help Please!

    Can you show what the code would look like, i learn better by seeing things.

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Help Please!

    It something like this:

    Java Code:
    ...
    if ("Yes".equalsIgnoreCase(answer)) {
    }
    ...

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Help Please!

    Why have you got two readers for the user input?
    You only actually need the one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    j0rdan is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Help Please!

    Java Code:
    import java.util.Scanner;
    
    
    /**
     *
     * @author ryan0j3
     */
    public class NewClass {
    
            public static void main(String[] args) 
        { 
            
            Scanner in = new Scanner(System.in);
            System.out.println("Enter name");
            String name = in.next();
            System.out.println("is this your name:" + name);
            String answer = in.next();
            
            if (answer.equals("yes"))
            {
                System.out.println("great");
            }
            else 
            {
                System.out.println("oops");
            }
        }
        
    }
    this is jut a simple thing like this just shows the equal etc try working it out from there.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Help Please!

    Forum Rules

    Particularly the third paragraph

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Tags for this Thread

Posting Permissions

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