1

I am a very new programmer and I just learned about scanner, but I am trying to find a way to make it so when I press the up or down keys, it cycles through the options, putting a > in front to know that it is the option currently selected.

This is the prompt:

What would you like to do:  
Attack   
Inventory  
Stats  
Flee

I would like it to have an arrow in front of the selected one and change the selection with the up and down arrow keys. So for example, as soon as you are prompted it looks like this:

What would you like to do:

What would you like to do:  
> Attack   
Inventory  
Stats  
Flee

And if you pressed the down arrow key twice:

What would you like to do:  
Attack   
Inventory  
> Stats  
Flee

So my current code looks like this:

public class Prompter {

    Scanner input = new Scanner(System.in);
    public int options() throws IOException {
        System.out.printf("What would you like to do:%nAttack %n Inventory %n Stats %n Flee %n");
        while (!input.hasNextInt()) input.next();
        int choice = input.nextInt();
        System.out.printf("Choice: %d", choice);
    return choice;
    }
}

What it does right now is accepts an int input and returns it.

Please do not send me a huge block of code without explaining it because I am a pretty new programmer. Thanks!

4
  • 2
    This is not how console programs work, but rather is behavior that is mainly found in GUI programs such as JavaFx or Swing programs. For example, if this were a Swing GUI, I'd recommend that you use a JComboBox<String> for this. Commented Jan 16, 2016 at 19:00
  • You might want to check out this library that uses the JNI (Java Native Interface): github.com/kwhat/jnativehook Commented Jan 16, 2016 at 19:12
  • also Charva (pitman.co.za/projects/charva/index.html) or JCurses (sourceforge.net/projects/javacurses) Commented Jan 16, 2016 at 19:16
  • Thank you for the feedback, I was not sure if it was possible to do this, and I guess not without using a GUI, but I will look into these options. Commented Jan 16, 2016 at 19:42

1 Answer 1

1

As mentioned in the comments before it is not possible to implement such a behaviour easily in the console.

An easy workaround could be to map numbers behind the options. (Attack (1), Inventory (2),..) and extend your code like:

int inputChoice = input.nextInt();
String choice = "";
switch(inputChoice){
      case 1: choice = "Action";
      case 2: choice = "Inventory";
      // add cases
}
System.out.println("Choice: " +choice);
return choice; // or inputChoice if you want to return an int value

It's not the best way to do this but could be enough for your current need.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.