1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

break when only return

Discussion in 'Java' started by Todilo, Nov 20, 2007.

  1. Todilo

    Todilo New Member

    Joined:
    Nov 14, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I am trying to do what really should be a simple task. My program asks a question, the user inputs and answer and then that repeats itself. I want the program to break the loop when the user input is RETURN only. So when no text is written and the user presses return it should break.

    I have made a scanner object and am trying to use
    Code:
    Scanner so = new Scanner(System.in);
    while (so.nextLine() !=null){
    ... do stuff		
    }
    I've also tried

    Code:
    BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
    String word;
    while(true){
    word= myIn.readLine();
    	if(word==null)
    	break;
    				
    }



    What should I do? I want it to be short and well "correct", so it is safe(no continious loop or crashes).

    Thanks in advance!
     

Share This Page