1

I'm new to maven and am working on a personal project. I have a line of code that is actually running before the previous line and it is confusing me beyond belief.

    System.out.println("----------------------------------------------------");
    System.out.println("                    Main Menu");
    System.out.println("----------------------------------------------------");
    System.out.println("Select an option from below");
    System.out.println("\t(1)Search\n"
                        + "\t(2)TEST\n"
                        + "\t(0)Quit");
    System.out.print("Enter Here: ");
    return input.nextLine().charAt(0);
   

This results in this output printing everything BUT the "Enter Here: " if you then give a char input it takes the input and then prints the previous line of code. I couldn't understand what was going on and thought I was losing my mind so when I copied the function over to a non-Maven file it executes exactly as expected. Allowing you to enter your input on the same line as the print statement. Has anyone seen this before and if they have how can I fix it? Admittedly this isn't a huge problem but it shouldn't be happening from what I can tell.

Output Image

Output Image

Pshemo
  • 113,402
  • 22
  • 170
  • 242
Nash
  • 13
  • 2
  • 1
    Try to put `System.out.flush();` after `System.out.print("Enter Here: ");` – ismail durmaz Jan 07 '21 at 22:37
  • you can look over this [answer](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) or maybe [this](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) one can help you. – studs-need-help Jan 07 '21 at 22:46

2 Answers2

1

System.out flashes when new line is output see another SO answer That results to the issue you see, last line is not println but print and it is not flushed to the output immediately.

Try adding System.out.flush();

funnymay
  • 143
  • 6
0

You will get different locations when printing the input and returning the input. That should do it! Presuming that solves your question, to save a little time and typing, here's a trick: use the text blocks feature in Java. How it works: start with three quotes, enter, and end with three quotes. The nice thing is you don't need \n or anything; however you format it as code is how it will print out. You can see the implementation below:

System.out.println("""
        ----------------------------------------------------
                                Main Menu
        ----------------------------------------------------
          Select an option from below
            (1)Search
            (2)TEST
            (0)Quit
            Enter Here:
        """);
    return input.next();
    System.out.println(input.next();

Very useful - only one System.out.println and no escaping formatting. That said, Texts blocks did not solve the error, rather:

return input.next();
    System.out.println(input.next();

The above solved the error.

Cole Henrich
  • 89
  • 1
  • 17
  • This is awesome! Thanks for the shortcut! I always hate thatwhenI am trying to make sure my formatting is correct only to see some sloppy output when I test because of a missed"\n". – Nash Jan 08 '21 at 18:56
  • Great to hear! I'm so glad to help. – Cole Henrich Jan 08 '21 at 19:29