0

I'm trying to create a Java guessing game, but the first part I'm working on is having issues and I would like some help. The program first asks a user to input a number, then the program asks them to confirm whether their number is what they inputted.

If they enter yes to whether they inputted the correct number, it currently just outputs "bru". If no is inputted then they re-enter the input number and the cycle will go on until the user correctly inputs their number and confirms it. I'm trying to accomplish this with a while loop.

Unfortunately when I run the program everything works fine until I'm asked to enter yes or no to confirm my number. If I enter yes it still asks for me to re-enter the number. But if I enter no and then I say no again confirming my number it gives me the output for when I confirm that I inputted the correct number.

import java.util.Scanner;
import java.util.Random;

public class Assignment6 {

    public static void main(String args[]) {
        Scanner input = new Scanner( System.in );
        System.out.print ( "Please enter the upper bound of the secret number.");
        int UpperBound = input.nextInt();
        System.out.print ( "The UpperBound you entered is" + " " + UpperBound + "." + "Is that correct?" + "" + "If yes please enter yes, if not please enter no.");
        String TrueOrFalse = input.next();
        while (TrueOrFalse == "no" | TrueOrFalse == "No");
        {
            System.out.print ( "Please enter the new upper bound of the secret number.");
            UpperBound = input.nextInt();
            System.out.print ( "The UpperBound you entered is" + " " + UpperBound + "." + " " + "Is that correct. If yes please enter yes, if not please enter no.");
            TrueOrFalse = input.next();
        }
        System.out.print ("Bru");
    }
}
Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • You compare strings with equals(), not ==. Example: ``"no".equals(trueOrFalse)`` – NomadMaker Aug 19 '20 at 04:52
  • 2
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – AndiCover Aug 19 '20 at 04:54
  • 1
    Java coding conventions have variables and methods starting with a lower case letter, such as "trueOrFalse." Classes start with an upper case letter. By following these conventions, it makes debugging easier. – NomadMaker Aug 19 '20 at 04:54

2 Answers2

0

Replace

while (TrueOrFalse == "no" | TrueOrFalse == "No");

with

while(TrueOrFalse.equalsIgnoreCase("no"))

i.e. remove ; from the while

also if possible renamme ur variable TrueOrFalse

Hades
  • 4,315
  • 3
  • 16
  • 33
  • 2
    and the `;` after while loop, delete it – Lê Hoàng Dững Aug 19 '20 at 05:00
  • This answer has nothing to do with the OP's question. I agree, these are good suggestions, but please keep suggestions in the comments section, so people can find answers in the answers section. – Charlie Armstrong Aug 19 '20 at 05:04
  • @CharlieArmstrong (TrueOrFalse == "no" | TrueOrFalse == "No") is not a suggestion it is causing unexpected behavior in the application since its a mistake. – Hades Aug 19 '20 at 06:07
  • 1
    @Hades My mistake, forgot we were not dealing with string literals here. My original comment was unwarranted, and I apologize, but I would still like to encourage you to try to address the whole problem. Your answer was going in the right direction, but there were several other issues with the OPs code, as pointed out by Sagar. – Charlie Armstrong Aug 19 '20 at 18:23
0

Your major problem is with ; and comparing String using equals and ==.

Also you need to check difference between nextInt() and nextLine()

Try below code, it will help you.

public static void main(String args[]) {
        Scanner input = new Scanner( System.in );
        System.out.print ( "Please enter the upper bound of the secret number.");
        int UpperBound = Integer.valueOf(input.nextLine());
        System.out.print ( "The UpperBound you entered is" + " " + UpperBound + "." + "Is that correct?" + "" + "If yes please enter yes, if not please enter no.");
        String TrueOrFalse = input.nextLine();
        while (TrueOrFalse.equalsIgnoreCase("no"))
        {
            System.out.print ( "Please enter the new upper bound of the secret number.");
            UpperBound = Integer.valueOf(input.nextLine());
            System.out.print ( "The UpperBound you entered is" + " " + UpperBound + "." + " " + "Is that correct. If yes please enter yes, if not please enter no.");
            TrueOrFalse = input.nextLine();
        }
        System.out.print ("Bru");
    }
Sagar Gangwal
  • 6,124
  • 3
  • 16
  • 31
  • if you use `equalsIgnoreCase()` then you don't have to compare with "no" and with "No" - equalsIgnoreCase already takes care of that... – Thomas Kläger Aug 19 '20 at 06:14