-3

What is a Java equivalent for this C++ code snippet:

string str ; while(cin>>str){ //code } 
4
  • 1
    stackoverflow.com/questions/4644415/… Commented Mar 2, 2014 at 17:30
  • can u explain what code mean in while loop Commented Mar 2, 2014 at 17:31
  • @Youngistan is plain c++ to read every line from user input and store it into a string. Commented Mar 2, 2014 at 17:37
  • Check out this Java program to get input from user Commented Nov 5, 2014 at 5:35

4 Answers 4

2

Something like this

String str ; while((str = System.in.readLine()) != null){ //code } 
Sign up to request clarification or add additional context in comments.

Comments

0
Scanner scanner = new Scanner(System.in); String sentence = scanner.nextLine(); 

2 Comments

This is just part of the code. Where's the while loop?
you don't need while loop. you can do your action with this. to finish taking input you can set a flag.
0
Scanner scanner = new Scanner(System.in); boolean flag = true; while(flag) { String str = scanner.nextLine(); // Add any condition to set flag = false to exit from while loop } 

5 Comments

@Luiggi Mendoza its equivalent to c++ pasted code ??
forget about equivalent. use it to see you get your desired output
i dnt knw much abt that operator in c++.What luiggi explain me i rght it @LynAs
Kinda. Another way can be String str = null; while ((str = scanner.nextLine() != null) { ... }.
yes,to exist the loop we need to write break inside it
-1

you can do it either with a GUI style .

import javax.swing.JOptionPane; String Input=JOptionPane.showInputDialog("Title","Message"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.