简体   繁体   English

Java读取数字文件并打印出来

[英]Java reading a file of numbers and printing them out

I have this code 我有这个代码

Scanner scanner = new Scanner("hello.txt");
   while(scanner.hasNextInt()){
       int i = scanner.nextInt();
       System.out.println(i);
    }

the data values I have in a hello.txt are 我在hello.txt中的数据值是

1 2 3 22 33 123 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 . 您正在使用的Scanner构造函数采用一个字符串来读取值 It's not a filename. 不是文件名。 There are no integers in the string hello.txt so you get no output. 字符串hello.txt中没有整数,因此没有输出。

If you want to read from the file called hello.txt , try 如果要从hello.txt文件中hello.txt ,请尝试

Scanner scanner = new Scanner(new File("hello.txt"));

它应该是

Scanner scanner = new Scanner(new File("hello.txt"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM