0

I am having problems with STDIN

I would to read the following 2 string, for example:

Input:

abc

xyz

When typing "abc", then press Enter, I get abc back. However i dont want that. I would like to type another string just like input above.

So what want is: Type abc, Enter, type xyz enter

here is my code:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s; 
while ((s = in.readLine()) != null && s.length() != 0){     
      System.out.println(s);
}

Thanks

2 Answers 2

1

You should use Scanners for this.

Here is an example implementing scanners:

Scanner scanner = new Scanner(System.in);

String s = scanner.nextLine();
String s2 = scanner.nextLine();

System.out.println(s + ":" + s2);

//Close scanner when finished with it:
scanner.close();

Here is the full documentation for further reading and examples: Oracle documentaion

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

1 Comment

Thank you, thats what i wanted.
0

Scanner is the preferred way to get input from the console. Example:

Scanner in = new Scanner(System.in);
System.out.print("Please enter a string: ");
String input = in.nextLine();

System.out.println("You entered: \"" + input + "\"");

Scanner also has other useful methods like nextInt and nextChar. Full docs on Scanner

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.