1

I have a little question for you brilliant people!

I'm in the process of making a little console game, nothing too fancy, but certainly not simple, and would like to figure out a more efficient way to do something! That something I would like to do is get input from the user!

From the beginning I was taught to use Scanners to get user input from a console! So that's what I have always used! Below is an example of what I would write.

package com.mattkx4.cgamedev.main;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Print out a message asking the user if ready
        System.out.println("Are you ready to continue? (Enter \"YES\" or \"NO\")");

        // Initialize a Scanner
        Scanner s = new Scanner(System.in);

        // Initialize a String to hold the User Input
        String in = s.nextLine();

        // Check the input with IF statements
        if(in.equalsIgnoreCase("YES")) {
            System.out.println("Let's continue!");
        }else if(in.equalsIgnoreCase("NO")) {
            System.out.println("Goodbye!");
            s.close();
            System.exit(0);
        }else{
            System.out.println("Please input a correct response!");
            main(null);
        }
    }
}

I'm absolutely sure there is a more efficient or easier way to do this! Any suggestions would be appreciated!

Thanks in advance -Matthew

6
  • Swing tutorial. Learn how to make a GUI and listen for keystrokes or mouse events to get input. Commented Jun 9, 2014 at 19:43
  • 1
    An easier way than Scanner.nextLine()? Also why does it need to be any more efficient? How fast are you expecting the user to type? Commented Jun 9, 2014 at 19:43
  • @DSlomer64 from where swing come into picture here. Commented Jun 9, 2014 at 19:44
  • Did I misinterpret? I thought he was wanting to get away from console input. Commented Jun 9, 2014 at 19:45
  • 1
    @DSlomer64 "I'm in the process of making a little console game", also the title of the post Commented Jun 9, 2014 at 19:45

1 Answer 1

2

The proper way is always illustrated in Oracle Java Tutorial itself.

Read it here I/O from the Command Line and find the sample code as well.


EDIT

Some points:

  • calling main() method again will initialize all the things again.
  • try with switch-case in case of multiple checks.
  • you can achieve the same functionality using do-while loop.

Sample code:

public static void main(String args[]) throws IOException {

    Console c = System.console();
    if (c == null) {
        System.err.println("No console.");
        System.exit(1);
    }

    boolean isValidResponse = false;
    do {
        String response = c.readLine("Are you ready to continue? (Enter \"YES\" or \"NO\"): ");
        switch (response.toUpperCase()) {
            case "YES":
                isValidResponse = true;
                System.out.println("Let's continue!");
                break;
            case "NO":
                isValidResponse = true;
                System.out.println("Goodbye!");
                break;
            default:
                isValidResponse = false;
                System.out.println("Please input a correct response!");
                break;
        }
    } while (!isValidResponse);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I wanted to find. I guess I should have checked the docs before posting a question here, but I like the community... :)
YES! YES! AND YES! That is the syntax I was looking for! You put it very simply! I think that might have been hidden somewhere in the docs, but you made it much simpler! Now I can continue on with my Console Game Development :) Thanks so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.