-2

I am advised to use a while loop the Scanner method hasNextLine() and in the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings. I am new to Java so please keep that in mind. I'm not exactly sure if this is right, but this is what I have gotten so far:

Scanner input = new Scanner(new File(""));

    while(input.hasNextLine()) {
        String line = input.nextLine();
        System.out.println(line);
  • Now I'm having trouble on figuring out if this code would read a user specified text file and I'm not sure how to print out what the file says using a method. – JavaNovice Apr 23 '15 at 22:37
  • Would I use an if statement to have it read? – JavaNovice Apr 23 '15 at 22:37
  • Have you at least tried your code? Is this real code or pseudo-code? What exactly do you want? – Alexander Apr 23 '15 at 22:39
  • I've tried it, but I get error because the while loop isn't closed. So I am trying to figure out how to print out the file. – JavaNovice Apr 23 '15 at 22:41
  • possible duplicate , http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file – Shrikant Havale Apr 23 '15 at 22:42
  • What do you mean with `because the while loop isn't closed.`? This code axample is missing a closing `}` for the while loop. Yes. Add it. – Alexander Apr 23 '15 at 22:43
  • I see what you're saying, @Alexander , but how would I get it to ask the user "Input file name? " – JavaNovice Apr 23 '15 at 22:49

1 Answers1

1

With this code you can:

  • prompt a user for a file name.
  • read the file line by line using the Scanner class.
  • add each line to an ArrayList of Strings.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerReadFile {

    public static void main(String[] args) {

        // Location of file to read
        Scanner x = new Scanner(System.in);
        System.out.println("Enter a filename: ");
        String fileName = x.nextLine();
        File file = new File(fileName);

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

        try { 

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                lines.add(line);
                System.out.println(line);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}
MCHAppy
  • 1,012
  • 9
  • 15
  • I've never heard of the e.printStackTrace(); , I've always used the system.out.print(); , should I not use that in this case? – JavaNovice Apr 23 '15 at 22:39
  • @JavaNovice If the file is not found, the code above catch an exception and therefore print the stack trace. – MCHAppy Apr 23 '15 at 22:45
  • how do I get it to ask the user, "Input file name? " – JavaNovice Apr 23 '15 at 22:47
  • Wait, one more question, sorry! For: ArrayList lines = new ArrayList(); it tells me to use the diamond operator instead. and for the Scanner scanner = new Scanner(file); it tells me to convert to try with resources. and it also tells me to delete the print stack trace, why? – JavaNovice Apr 23 '15 at 23:07
  • @JavaNovice , for diamond operator operator just change this `ArrayList lines = new ArrayList(); ` to this `ArrayList lines = new ArrayList<>(); ` and for the print stack trace you can just delete this line of code `e.printStackTrace();`. – MCHAppy Apr 23 '15 at 23:14
  • When I type in a file, it does not search for it or anything, it just says "Build Successful" – JavaNovice Apr 23 '15 at 23:35
  • This is the project I am trying to complete, if that helps solve my question about reading a file: http://stackoverflow.com/questions/29833926/java-need-to-use-methods-and-array-lists-to-search-print-and-read-a-text-fil – JavaNovice Apr 23 '15 at 23:37