I have this code
Scanner scanner = new Scanner("hello.txt");
while(scanner.hasNextInt()){
int i = scanner.nextI开发者_StackOverflow中文版nt();
System.out.println(i);
}
the data values I have in a hello.txt are
1 2 3 22 33 123
but when I run the program there is no output. Is there something I am not using / line of code??
The Scanner
constructor you are using takes a string from which to read values. It's not a filename. There are no integers in the string hello.txt
so you get no output.
If you want to read from the file called hello.txt
, try
Scanner scanner = new Scanner(new File("hello.txt"));
it should be
Scanner scanner = new Scanner(new File("hello.txt"));
精彩评论