geting a multiline text with scanner class in java

10.4k views Asked by At

In a part of my university project I have to get a text with some lines then saving it in a string or a string array.My problem is that in scanner class using methods gets only one line of the input. So I cannot get the other lines.please help me.

public class Main {
public static void main(String[] args) {
    java.util.Scanner a = new java.util.Scanner(System.in);
    String b = "";
    while (a.hasNextLine()) {
        b += a.nextLine();
    }
}

}

2

There are 2 answers

2
Yohanes Khosiawan 许先汉 On BEST ANSWER

You can try to use isEmpty to detect an enter-only input.

UPDATED:

If your input also contain a blank line, then you may specify another terminator character(s); instead of only an empty string.

public class Main {
    public static void main(String[] args) {
        //for example ",,"; then the scanner will stop when you input ",,"
        String TERMINATOR_STRING = ",,"

        java.util.Scanner a = new java.util.Scanner(System.in);
        StringBuilder b = new StringBuilder();
        String strLine;
        while (!(strLine = a.nextLine()).equals(TERMINATOR_STRING)) {
            b.append(strLine);
        }
    }
}
0
Aman Agnihotri On

If you are building your program from command line, then there's something called "input redirection" which you can use. Here's how it works:

Let's suppose your program is:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ScanningMultiline
{
    public static void main (String[] args)
    {
        List<String> lines = new ArrayList<> ();

        try (Scanner scanner = new Scanner (System.in))
        {
            while (scanner.hasNextLine ())
            {
                lines.add (scanner.nextLine ());
            }
        }

        System.out.println ("Total lines: " + lines.size ());
    }
}

Now suppose you have input for your program prepared in a file.

To compile the program you'd change the current directory of terminal/command prompt to the program directory and then write:
javac ScanningMultiline.java

And then to run, use input redirection like:
java ScanningMultiline < InputFile.txt

If your InputFile.txt is in another directory, just put its complete path instead like:
java ScanningMultiline < "/Users/Xyz/Desktop/InputFile.txt"

Another Approach

You can try reading your input directly from a file. Here's how that program would be written:

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ScanningMultiline
{
    public static void main (String[] args)
    {
        final String inputFile = "/Users/Xyz/Desktop/InputFile.txt";

        List<String> lines = new ArrayList<> ();

        try (Scanner scanner = new Scanner (Paths.get (inputFile)))
        {
            while (scanner.hasNextLine ())
            {
                lines.add (scanner.nextLine ());
            }
        }
        catch (IOException e)
        {
            e.printStackTrace ();
        }

        System.out.println ("Total lines: " + lines.size ());
    }
}

This approach reads directly from a file and puts the lines from the file in a list of String.

Another Approach

You can read the lines from a file and store them in a list in a single line as well, as the following snippet demonstrates:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class ScanningMultiline
{
    public static void main (String[] args) throws IOException
    {
        final String inputFile = "/Users/Xyz/Desktop/InputFile.txt";

        List<String> lines = Files.readAllLines (Paths.get (inputFile));
    }
}

Yohanes Khosiawan has answered a different approach so I'm not writing that one here.