0

Using java, I need to make a program that asks the user which file to scan, and to do some work with the data in the file.

My program is supposed to select a file, scan the file for a specific character that the user specifies, and return with how many specific characters there are in that file.

This is my code so far:

import java.io.*;
import java.util.Scanner;

public class CharSearch {
    public static void main(String[] args) throws Exception{

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter the name of the file you want to search.");
    String fileInput = scanner.nextLine();

    System.out.println("What character would you like to look for in " + fileInput + "?");

    Scanner fileScanner = new Scanner(fileInput);
    System.out.println(fileScanner);
    }
}

I imported io and scanner, then set up the scanner to read which file the user inputs. I print back out that file name. The last two lines are where I need help. How can I make the scanner return with the data in the file.

There is a file in my folder called data.txt and all that is written in it is "dataWord." For starters, I want the scanner to read the file and the program to display dataWord, but its not working. I am a rookie, so please work with me. Thanks.

Tim Keptya
  • 19
  • 2
  • 8
  • `fileScanner.nextLine();` will get the first line of the file and return everything on that line as a `String` to loop through just do `while(fileScanner.hasNext())` – 3kings Oct 13 '15 at 01:50
  • Oh ok, thats helpful. Is there a way for it to return everything in the file, just for future purposes? – Tim Keptya Oct 13 '15 at 01:53
  • Look into `Files.readAllLine(path)` it returns an `List` – 3kings Oct 13 '15 at 01:56

1 Answers1

0

Instead of passing path of file as String pass it as a File

    Scanner fileScanner = new Scanner(new File(fileInput));
    while (fileScanner.hasNext()) {
        System.out.println(fileScanner.next());
    }
    scanner.close();
    fileScanner.close();

If the file is an external file or not in your project, you can specify the absolute path of file.

If it is in classpath you can read using ClassLoader

     java.io.InputStream inputStream = this.getClassLoader().getResourceAsStream("file_path")

Scanner has a constructor with InputStream

Saravana
  • 12,647
  • 2
  • 39
  • 57
  • After I put that in, the command prompt says that it can't find the file that I want it to look for. In the same folder as the JAVA and CLASS files, I have the text file, data.txt, that I am inputting in the command prompt where the fileInput is in the code. Do I need to specify the path of the file in a specific way because I tried M:\data.txt which is where the file is located, on my USB. What could be causing the program to not be able to locate my file? – Tim Keptya Oct 13 '15 at 23:03
  • Where do I put the java.io.InputStream thing in my code? I am very lost at this point. – Tim Keptya Oct 14 '15 at 04:24
  • `Scanner scanner = new Scanner(inputStream)` hope this helps, if you face any other issues share the project structure and .classpath – Saravana Oct 14 '15 at 04:27
  • So do I put the inputStream as a separate variable before all this stuff? – Tim Keptya Oct 14 '15 at 04:28
  • Also what is the "this" in the InputStream? – Tim Keptya Oct 14 '15 at 04:29
  • see my code snippet, either you can declare a variable and pass or just pass `this.getClassLoader().getResourceAsStream("file_path")` to scanner, its your wish. – Saravana Oct 14 '15 at 04:31
  • http://stackoverflow.com/questions/4023344/difference-between-this-andsuper-keywords-in-java here you'll get answer – Saravana Oct 14 '15 at 04:31
  • I just get a bunch of errors that say "non-static variable this cannot be referenced from a static context" and the this.getClassLoader symbol can't be found. – Tim Keptya Oct 14 '15 at 04:38
  • oh, we can't access `this` and `super` in static method, so replace `this` with `YouClass.class` i.e `CharSearch.class.getClassLoader().getResourceAsStream("file_path")` – Saravana Oct 14 '15 at 04:40
  • Exception in thread "main" java.lang.NullPointerException at java.io.Reader.(Unknown Source) at java.io.InputStreamReader.(Unknown Source) at java.util.Scanner.(Unknown Source) at CharSearch.main(CharSearch.java:14) Thats what the program says after i run it after compiling it. – Tim Keptya Oct 14 '15 at 04:46
  • did you try with `scanner = new Scanner(new File("M:\data.txt"))`, it'd work – Saravana Oct 14 '15 at 07:27
  • I tried that out and today in class, I got the answer. Thanks for all the help. – Tim Keptya Oct 14 '15 at 22:27