-1

I have this program that I'm writing that mainly runs similar to a simple choose your own adventure game. It's a trouble shooting guide for rocketry, that mainly for personal use. It has simple value inputs, y/n, a,b,c,d, and so on. After a few different segments I decided to ask the user (incase I give it to friends or something) if they would like to hear a tip, if they said yes, the tip would be printed out, if not, the program would continue running. When I tested it, i got the error message: 'bash y: command not found'.

All of the syntax is correct (that was the best answer I could find to try to correct the problem, didn't apply), and I don't know what bash is, and I'm using replit, incase that matters. I get this error in two different areas, but here is an example of one:

//problem code
class A{
   public static void tip(){
      Scanner scan = new Scanner(System.in);
      System.out.println("Would you like to hear a tip? (y/n)");
      String tipA = scan.nextLine();
      if (tipA.equals("y")){
         System.out.println("blahblahblah");
      }
   }
}
//there was a typo in this post but not in the actual code, wish it was the actual problem, thanks for pointing it out

Then I changed it to this:

//the 'fixed' code
class A{
   public static void tip(){
      Scanner scan = new Scanner(System.in);
      System.out.println("Would you like to hear a tip? (y(1)/n(2))");
      int tipA = scan.nextInt();
      if (tipA == 1){
         System.out.println("blahblahblah");
      }
   }
}

It works, but as you can see in the 'fixed' code, the y/n doesn't look as nice, and I am also curious as to why, because I could not find any answers pertaining to this scenario. The other area is similar, but instead of "y/n", it is 'A, B, C, D' choices. I have my current workaround, but it would be nice to have a solution to polish the program a bit.

Any help is much appreciated.

voyhager3
  • 3
  • 5
  • 1
    Debugging hint: when a string isnt "equal" to your expecation, then consider to do a `System.out.println("got: <" + tipA +">")` for example. – GhostCat Mar 16 '22 at 12:39

1 Answers1

1

It sounds like your problem is confusion about what nextLine does. It doesn't do what 99% of java programmers (and most tutorials) think it does. It's not a bug - it does exactly what the javadoc says, but, it's not what you'd expect.

See This answer that explains precisely what to do. Boils down to:

public static void tip() {
      Scanner scan = new Scanner(System.in);
      scan.useDelimiter("\\R");
      System.out.println("Would you like to hear a tip? (y/n)");
      String tipA = scan.next();
      if (tipA.equals("y")) {
         System.out.println("blahblahblah");
      }

Generally you'd want to make a scanner exactly once (in your main method perhaps), create a new instance of your Main class, set up the Scanner as a field of it, and then reuse it in all your methods. This saves typing, is easier to test, more flexible, etc:

public class ExampleMain {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    s.useDelimiter("\\R");
    ExampleMain app = new ExampleMain();
    app.scanner = s;
    app.go();
  }

  private Scanner scanner;
  private boolean running;

  void go() {
    running = true;
    while (running) {
      tip();
      // whatever other commands and such you'd like go here
    }
  }

  void tip() {
    System.out.println("Would you like to....");
  }
}

This also lets you do things like write a method that repeatedly asks for a specific kind of input (say, an integer between 1 and 9, as you're showing a menu with 9 options), and, using a while loop, keep asking if the user fails to enter appropriate responses. Otherwise you have to copy/paste a ton of code which is naturally not how you should be programming.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72