0

For a few projects, I've been trying to make a console menu in Java similar to:

 (1) Do this
 (2) Do that
 (3) Blah blah
 (4) etc

I'm using a do {...} while (...) loop but I can't get the right value for the controlling variable.

The code I had was:

 String status = "e";

    do {
        System.out.println("---------------------------------------");
        System.out.println(b1.toString());
        System.out.println(b2.toString());
        System.out.println(b3.toString());
        System.out.println("---------------------------------------");
        System.out.println("Borrow(b), Return(r), Check(c), Exit(e)");
        status = r.nextLine();
        ....
    } while(!status.equals("e"));

This code resulted in all the printlns outputting correctly, but upon pressing enter, the same thing would output again and the code I replaced with .... will not excute. This code had other console outputs which never came about.

I thought this was because the value returned by r.nextLine() continually changes as new data gets outputted. So I made a separate static function:

 public static String getInfo(Scanner r, Book b1, Book b2, Book b3) {
    System.out.println("---------------------------------------");
    System.out.println(b1.toString());
    System.out.println(b2.toString());
    System.out.println(b3.toString());
    System.out.println("---------------------------------------");
    System.out.println("Borrow(b), Return(r), Check(c), Exit(e)");
    String status = r.nextLine();
    return status;
}

But this function also returns the same result. What can I do to fix this problem?

Edit:

Right now, this is my full code for the menu portion, this runs in the main.

`String status = "e";

    do {
        status = getInfo(reader,b1,b2,b3);
        if (status == "b") {
            System.out.println("Which patron ( (1)" + p.getName() + " or (2)" + p2.getName() + " is borrowing?");
            int cur = reader.nextInt();
            System.out.println("Which book is " + cur + " borrowing?");
            String curbk = reader.nextLine();
            if (p.hasBook(curbk)){
                System.out.println(p.getName() + " has this book already.");
            } else {
                if (p2.hasBook(curbk)) {
                    System.out.println(p2.getName() + " has this book already.");
                } else {
                    if (cur==1) {
                        System.out.println(p.borrowBook(curbk));
                    } else {
                        System.out.println(p2.borrowBook(curbk));
                    }
                }
            }
        } else if (status == "r") {
            System.out.println("Which patron ( (1)" + p.getName() + " or (2)" + p2.getName() + ") is returning?");
            int cur = reader.nextInt();
            System.out.println("Which book is " + cur + " returning?");
            String curbk = reader.nextLine();
            if (cur==1) {
                if (p.hasBook(curbk)){
                    System.out.println(p.returnBook(curbk));
                } else {
                    System.out.println(p.getName() + " does not have this book.");
                }
            } else {
                if (p2.hasBook(curbk)){
                    System.out.println(p2.returnBook(curbk));
                } else {
                    System.out.println(p2.getName() + " does not have this book.");
                }
            }
        } else if (status == "c") {
            System.out.println("Which book would you like to check for?");
            String curbk = reader.nextLine();
            if (p.hasBook(curbk)){
                System.out.println(p.getName() + " has this book.");
            } else {
                if (p2.hasBook(curbk)) {
                    System.out.println(p2.getName() + " has this book.");
                } else {
                    System.out.println("This book is ready to be checked out!");
                }
            }
        }
    } while(!status.equals("e"));`

The getInfo() is from above.

ashraj98
  • 384
  • 5
  • 17
  • What is the code in the `....`? – Andy Turner Oct 05 '15 at 23:01
  • `nextLine()` returns **one** value, it will not *"continually changes as new data gets outputted".* – Kevin Oct 05 '15 at 23:02
  • What is the variable `e`? Also, you should compare strings using `!String.equals`, not `!=`. – Andy Turner Oct 05 '15 at 23:02
  • Voting to close as yet another String Comparison question. `"".equals("")`, not `"" == ""`. – Matt Clark Oct 05 '15 at 23:02
  • 4
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – JosEduSol Oct 05 '15 at 23:03
  • No that was my old code I am using `String.equals` and I still do not get desired result. The `e` was also `"e"`. Sorry for the confusing, I updated the question. Just was typing and copying a bit fast... – ashraj98 Oct 05 '15 at 23:07
  • 1
    Ah, then @john3136's answer is what you're looking for, since you're reading the next line into one variable named `status` and comparing against the other. – azurefrog Oct 05 '15 at 23:09
  • I can run your code with some extra print statements replacing the `...` and it displays just fine. Perhaps this has something to do with the code you're actually trying to run (which you haven't posted)? – azurefrog Oct 05 '15 at 23:18
  • I added the code that I replaced with `....` – ashraj98 Oct 05 '15 at 23:24
  • ...and now we're back to not comparing strings with `==` in all of your if conditions... – azurefrog Oct 05 '15 at 23:28
  • Ugh that would be it... thanks... feel free to downvote me I do things like that sometimes – ashraj98 Oct 06 '15 at 00:01

1 Answers1

0
String status = r.nextLine();

Remove the keyword String, as it's creating a new String rather than using the variable you already created.

hermitmaster
  • 155
  • 1
  • 10