Scanner Only Reads File Name and Nothing Else

Scanner only reads file name and nothing else

You've misunderstood the API for Scanner. From the docs for the Scanner(String) constructor:

Constructs a new Scanner that produces values scanned from the specified string.

Parameters:

source - A string to scan

It's not a filename - it's just a string.

You should use the Scanner(File) constructor instead - or better yet, the Scanner(File, String) constructor to specify the encoding as well. For example:

try (Scanner scanner = new Scanner(new File(this.fileName), "UTF_8")) {
...
}

(Note the use of a try-with-resources statement so the scanner gets closed automatically.)

Java Scanner can't read from only File Name

Scanner scanner = new Scanner(new File (new File("TempFile.csv").getAbsolutePath()));

Use above.

FileReader/Scanner reading file name rather than contents of .txt file

You were really close - you're just iterating over inputFileName.toCharArray(), which is giving you the characters in the file name. You need to read characters from your in variable (which will give you the file contents) instead, eg:

while (in.hasNextLine())
{
char[] line = in.nextLine().toCharArray();
for (char c : line)
charFreqs[c]++;
}

Note this will probably discard any newline characters in the file, so if you want to count them you'll probably have to do it manually. Or switch to reading raw char[]s from the FileReader you already have, which is probably a better approach than above (you want raw character data, not "text" data, which is what Scanner operates on).

When using a scanner to read a file, why must the scanner be in the method?

Your scanner gets the file name - and not the file.

public Printer(String fileName) {
this.file = new File(fileName);
this.reader = new Scanner(file); //note the change
}

This should help you get to the contents.

Java scanner not going through entire file

There's a problem with Scanner reading your file but I'm not sure what it is. It mistakenly believes that it's reached the end of file when it has not, possibly due to some funky String encoding. Try using a BufferedReader object that wraps a FileReader object instead.

e.g.,

   private static Set<String> posible2(String posLoc) {
Set<String> result = new TreeSet<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(posLoc)));
String availalbe;
while((availalbe = br.readLine()) != null) {
result.add(availalbe);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}

Edit
I tried reducing your problem to its bare minimum, and just this was enough to elicit the problem:

   public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File(FILE_POS));
int count = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.printf("%3d: %s %n", count, line );
count++;
}

I checked the Scanner object with a printf:

System.out.printf("Str: %-35s size%5d; Has next line? %b%n", availalbe, result.size(), s.hasNextLine());

and showed that it thought that the file had ended. I was in the process of progressively deleting lines from the data to file to see which line(s) caused the problem, but will leave that to you.

Why am I getting the output of zero when scanning the file for a word?

Pass the FileReader object into your Scanner, at the moment you're passing it a String

        Scanner scanner = new Scanner(fin);

Also make sure that your file is in the root directory of your project.

Please enter the filename: 
hi.txt
Please enter a word:
lol
2

File content:

lol
lol


Related Topics



Leave a reply



Submit