Java input methods?

I'm currently attempting to write a role-playing game with Eclipse (don't know how far I'll get, but who knows) and I'd like to prompt the user to answer with a sentence/phrase.

I'm learning very basic Java right now, but we know how to prompt the user to enter in either an integer or a double variable/number (er... mind is a little messed up) - similar to:

variable=input.nextInt();, or input.nextDouble(); 

Can anyone please list how to prompt the user for a phrase, and how to make the program recognize that certain phrase (and get results)? Thank you.

(One final note: I'm not the best programmer, so can you please list the simplest ways to do so?)

Upvotes: 0

Views: 943

Answers (2)

havexz
havexz

Reputation: 9590

Soln:

  public static void main(String[] args) {
    System.out.println("Enter the phrase");
    String line;
    try {
    BufferedReader input =new BufferedReader(new InputStreamReader(System.in));

    while ((line = input.readLine()) != null) {
      System.out.println(line);
      if(!line.isEmpty()) {
        StringTokenizer st = new StringTokenizer(line);
        while (st.hasMoreTokens()) {
          System.out.println("token=" + st.nextToken());
        }
      }
    }
    input.close();
    } catch (IOException e){
      e.printStackTrace();
    }
    System.out.println("DONE");
  }

Upvotes: 1

Ryan Stewart
Ryan Stewart

Reputation: 128829

Probably your input is a Scanner, so just use nextLine() to get a line of text. That will wait for the user to enter an arbitrary amount of text and press enter, then you'll get all the text they entered.

Upvotes: 1

Related Questions