1
votes

I am a newbie to Java, and I'm trying to get a string from an input with multiple lines.

e.g. a string ="The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick fox jumps over the lazy dog. " from the input, like this:

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

I've tried to use a while loop with .nextLine() like this

String s="";
Scanner Input = new Scanner (System.in);
    
while(Input.hasNextLine()){
    s = s + Input.nextLine() + " ";
}

But the loop just seemingly runs infinitely. Thanks for any help that can be given.

4
Need to have a better exit, if you press Ctrl-Z program cant get more processing / program will exit. Add some System.out.printlns ... see my 2nd sample it compiles and can be runtgkprog

4 Answers

3
votes

You have to press Ctrl+z (or Ctrl+d on UNIX) to indicate that there won't be more inputs.

2
votes

The standard input stream usually never ends if you do not indicate that. How could your program know, that the user does not enter another line?

So you should only read the lines that you explicitely know will get entered, like that:

Scanner input = new Scanner(System.in);
String first = input.nextLine();
String second = input.nextLine();
input.close();
System.out.println(first);
System.out.println(second);

If you want to have a variable number of lines, you should go with your while loop, but the user must indicate the end of input, as Christian suggested. Like that:

Scanner input = new Scanner(System.in);
StringBuilder builder = new StringBuilder();
while (input.hasNext()) {
    builder.append(input.nextLine()).append(System.lineSeparator());
}
input.close();
System.out.println(builder.toString());

Running that will loop infinitely (seemingly). But when pressing CTRL-Z you will end the input and input.hasNext() will return false. (Note, that CTRL-Z works in Linux and Windows.)


EDIT as per Erwin Bolwidt's comment to Christian's answer:

If you are redirecting the standard input stream, so that it for example reads from a file, the EOF marker (end of file) will indicate the end of input to the scanner. So obviously no user interaction is involved and the program will nevertheless behave correctly.

0
votes

add a variable count and increment it in each looping.If the count meets some criteria then you can break; from the loop.Someething like ,

    String s = "";
    Scanner Input = new Scanner(System.in);
    int count = 0;

    while (Input.hasNextLine()) {
        if (count == 4) {
            break;
        } else {
            count++;
            s = s + Input.nextLine() + " ";
        }

    }
    System.out.println(s);
0
votes

Need to end the loop on some criteria, plus your var names are 1 char which is not good.

Your way works press ctrl -Z (windows) to end loop but i dont think can enter more input from standard input after that.

    import java.util.*;
    public class ReadStrs{
        public static void main(String []args){
            String s = "";
            String readStr = "";
            StringBuilder sb = new StringBuilder();
            Scanner input = new Scanner (System.in);
            int cnt = 0;
            //while( !(readStr = input.nextLine()).equals("end"))
            System.out.println("Enter str " + cnt + " Or end to stop accepting strings");
            while(input.hasNextLine()){
                cnt++;
                readStr =input.nextLine();

                sb.append(readStr).append(" ");
                System.out.println("Enter str " + cnt + " Or end to stop accepting strings");
             }//this will go on for ever unless you have a check to break the input and start processing
             s = sb.toString();//can manipulate sb or s now
             System.out.println("Out of loop got :" + s + "---");
        }
     }

Another way prompts user to end input by entering a specific string:

    import java.util.*;
    public class ReadStrs{
        public static void main(String []args){
            String s = "";
            String readStr = "";
            StringBuilder sb = new StringBuilder();
            Scanner input = new Scanner (System.in);
            int cnt = 0;
            while( !(readStr = input.nextLine()).equals("end")){
                cnt++;
                System.out.println("Enter str " + cnt + " Or end to stop accepting strings");
                sb.append(readStr).append(" ");
             }//this will go on for ever unless you have a check to break the input and start processing
             s = sb.toString();//can manipulate sb or s now
        }
     }