I am trying to read every word (from every line) from a file which is sent in through args.
It's just a simple text file:
aa bb cc d
dd ee f gg
xx yy zz
The scanner seems to not be able to find the scanner symbol.. Here is my code:
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Args
{
public static void main(String[] args)
{
ArrayList<String> wordArrayList = new ArrayList<String>();
System.out.println(args[0]);
File filename = new File(args[0]);
if (args.length == 1)
{
try
{
Scanner scanner = new Scanner(filename);
while (scanner.hasNextLine())
{
while (scanner.hasNext())
{
wordArrayList.add(scanner.Next());
}
}
}
catch (FileNotFoundException e)
{
System.out.println("Oops, file "+ filename + "not found.");
}
}
else
{
System.out.println("Nothing to do.. Powering down..");
}
}
}
Does any1 know what I am doing wrong with the scanner here?
This is the Error code:
Args.java:24: error: cannot find symbol
wordArrayList.add(scanner.Next());
^
symbol: method Next()
location: variable scanner of type Scanner
1 error
Because in java methods name are case sensitive you should check how your method is named.
According to http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html your method is named next()
not Next()
.
Changing it to next()
should fix the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.