1

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();
    }
}

}

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Kasra Ghavami
  • 15
  • 1
  • 1
  • 6
  • Your program intent is correct. The way you are trying to give multiline input is wrong. Try reading from a file, by writing: `Scanner scanner = new Scanner(Paths.get());`. Also use `StringBuilder` for appending lines in `b`, since `String` is immutable, so it'll incur performance hits if you do a `+=` to it. – Aman Agnihotri Nov 21 '14 at 06:09
  • sorry. i didn't understand what you mean. – Kasra Ghavami Nov 21 '14 at 06:43

2 Answers2

2

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);
        }
    }
}
  • 1
    there is a problem with this. In my program lines could be empty and there might be lines after it. in this way it just save the string until not be a empty line. – Kasra Ghavami Nov 21 '14 at 06:42
  • then you may just specify any other character(s) that would be unique and will never occur in your input, and use that as a terminator. you might also want to check this out: http://stackoverflow.com/questions/16206813/how-to-terminate-scanner-when-input-is-complete, but you need to run it from the terminal instead of the IDE. – Yohanes Khosiawan 许先汉 Nov 21 '14 at 06:52
1

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.

Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22