Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Simple fix for simple program

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Simple fix for simple program

    Hello forum, new here and a beginner to java. I have a small program with input, output and a switch statement.

    What I need, is for the program to take in answers and compare them to the variable.

    Code:

    import java.util.Scanner;

    public class Dutch {

    public static void main(String[] args)
    {
    answer();
    }

    static void answer()
    {
    Scanner input = new Scanner(System.in);
    String name = "";
    String theAnswer = "";
    boolean answering = true;

    System.out.println("Enter answers: " + "\n");

    while(answering == true)
    {
    switch(1)
    {
    case 1:
    {
    while(answering == true)
    {
    theAnswer = input.nextLine();
    if(theAnswer.equals("hond") || theAnswer.equals("Hond"))
    {
    System.out.println("Well done! You are correct. Next one." + "\n");
    } else
    {
    System.out.println("Oops! Try again!" + "\n");
    }
    }
    }
    case 2:
    {
    while(answering == true)
    {
    theAnswer = input.nextLine();
    if(theAnswer.equals("kat") || theAnswer.equals("Kat"))
    {
    System.out.println("Well done! You are correct. Next one." + "\n");
    } else
    {
    System.out.println("Oops! Try again!" + "\n");
    }
    }
    }

    case 3:
    while(answering = true)
    {
    theAnswer = input.nextLine();
    if(theAnswer.equals("vis") || theAnswer.equals("Vis"))
    {
    System.out.println("Well done! You are correct. Next one." + "\n");

    } else
    System.out.println("Oops! Try again!" + "\n");
    }
    }
    }
    }
    }

    But when I get to "kat" all that comes up is "Oops! Try again!" completely bypassing the original if statement, the first case works as needed, but the rest just don't.. I've been trying to work it out for a few hours now but the code seems fine, maybe just my logic?

    Any help is appreciated, thanks!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple fix for simple program

    Have you tried printing out the contents of the variables that have the values being tested so you can see what the computer sees when it executes the code?

    Please copy the contents of the command prompt window and paste it here so we can see what happens.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple fix for simple program

    Quote Originally Posted by Norm View Post
    Have you tried printing out the contents of the variables that have the values being tested so you can see what the computer sees when it executes the code?

    Please copy the contents of the command prompt window and paste it here so we can see what happens.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Hello there,

    import java.util.Scanner;
     
    public class Dutch {
     
    	public static void main(String[] args)
    	{
    	answer();
    	}
     
    	static void answer()
    	{
    		Scanner input = new Scanner(System.in);
    		String name = "";
    		String theAnswer = "";
    		boolean answering = true;
     
    		System.out.println("Enter answers: " + "\n");
     
    		while(answering == true)
    			{
    			 switch(1)
    			 {
    			 case 1: 
    			 {
    				 while(answering == true)
    				 {
    				  theAnswer = input.nextLine();
    				 if(theAnswer.equals("hond") || theAnswer.equals("Hond"))
    				 {
    					 System.out.println("Well done! You are correct. Next one." + "\n");
    				 } else
    				 {
    					 System.out.println("Oops! Try again!" + "\n");
    				 }
    			 }
    			 }
    			 case 2: 
    			 {
    				 while(answering == true)
    				 {
    				  theAnswer = input.nextLine();
    				 if(theAnswer.equals("kat") || theAnswer.equals("Kat"))
    				 {
    					 System.out.println("Well done! You are correct. Next one." + "\n");
    				 } else 
    				 {
    					 System.out.println("Oops! Try again!" + "\n");
    				 }
    				 }
    				 }
     
    			 case 3:  
    				 while(answering = true)
    			 {
    				  theAnswer = input.nextLine();
    				 if(theAnswer.equals("vis") || theAnswer.equals("Vis"))
    				 {
    					 System.out.println("Well done! You are correct. Next one." + "\n");
     
    				 } else 
    					 System.out.println("Oops! Try again!" + "\n");
    	}
    }
    			}
    	}
    }
    [/noparse]

    Output:
    Enter answers:

    hond
    Well done! You are correct. Next one.

    kat
    Oops! Try again!

    when it should be
    kat
    Well done! You are correct. Next one.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple fix for simple program

    Did you try doing some debugging by adding print statements that print out the values in theAnswer?
    Make sure each print statement has a unique id so you know which statement printed the output:
         System.out.println("id1 " + theAnswer + "<");   // show theAnswer with an id
    Change the 1 to 2 etc for each print statement so that they all are unique.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple fix for simple program

    Good point, theAnswer variable is not changing from hond to kat, i'm not sure why that is as theAnswer is initialized before the if statements..

    Enter answers:

    hond
    hond
    Well done! You are correct. Next one.

    hond
    hond
    Well done! You are correct. Next one.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple fix for simple program

    I assume that the Strings hond shown in the post was from a print statement you added. Was one of them entered and one printed by the program?
    Where is the "id1" String I recommended so you can match the printed output with the print statement that printed it?

    Please post the new code so we can see where the print statement was located.

    theAnswer variable is not changing from hond to kat
    Why would it do that? I thought the value in theAnswer was received from the user entering it. If you enter hond then its value should be hond.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple fix for simple program

    Hello, I have no clue why it won't change but I rewrote some of the code. Now it's bypassing the if and going straight to else.. no idea again why that is because it's not set up that way.

    Now what I'm doing is storing the values in an array called theAnswer and comparing that to "name" which is a string variable you get from user input.

    import java.util.Scanner;
    import java.util.Arrays;
     
    public class Dutch {
     
    	public static void main(String[] args)
    	{
    	answer();
    	}
     
    	static void answer()
    	{
    		Scanner input = new Scanner(System.in);
    		String name = "";
    		String[] theAnswer = {"hond", "kat", "vis", "vogel", "koe", "muis", "paard", "varken", "vleugel", "Dier"};
    		boolean answering = true;
     
     
    			 switch(1)
    			 {
    			 case 1: 
    			 {
     
    				 for(int i=1; i<=10; i++)
    					 {
    						 System.out.println("Enter answers: " + "\n");
    						 name = input.nextLine();
    						 System.out.println(name);
    						 if(theAnswer[i] == name)
    						 {
    						 System.out.println("Well done! You are correct. Next one." + "\n");
    						 } else 
    						 {
    							 System.out.println("Oops! Try again!" + "\n");
    						 }
    					 } 
    					 }
    			 }
    			 }
    	}
    [/noparse]

    Output:
    Enter answers:

    hond
    hond
    Oops! Try again!

    Enter answers:

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple fix for simple program

    Did you miss this where I recommended adding an id String to the print out so its output can be identified?
    Also print out the value being compared against:
         System.out.println("id1 " + name + " vs " + theAnswer[i]);   // show name with an id

    it's bypassing the if
    The code should use the equals() method to compare the contents of String objects, not the == operator.
    The previous code was correct:
    if(theAnswer.equals("hond") |
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. simple program
    By KollyBright in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2019, 07:54 AM
  2. please help with simple program!!!
    By jokneez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 14th, 2011, 10:42 AM
  3. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM