简体   繁体   English

如何使Java从文本中读取数字列表?

[英]How do I get Java to read a list of numbers from a text?

How do I get my Java code to read the next number from a list of numbers in a text file. 如何获取Java代码以从文本文件中的数字列表中读取下一个数字。 My output repeats the first number multiple times, how do I fix this? 我的输出多次重复第一个数字,如何解决这个问题?

public static void main(String[] args) throws Exception {
    for (int l = 0; l < 9; l++) {
        java.io.File myfile;
        String mypath;
        mypath = "/Users/tonyg/Downloads";
        myfile = new java.io.File(mypath + "/file.txt");
        Scanner myinfile = new Scanner(myfile);
        int val1;
        val1 = myinfile.nextInt();
        System.out.println(val1);
    }
}

OUTPUT: 输出:

385 385

385 385

385 385

385 385

385 385

385 385

385 385

385 385

385 385

It looks like your initializing the Scanner on every iterations of the for loop by simply initializing before the loop, your issue should be resolved. 就像您在for循环的每个迭代中初始化Scanner一样,只需在循环之前进行初始化即可解决您的问题。 It's also best practice to close the Scanner after using the resource 最好的做法是在使用资源后关闭扫描仪

public static void main(String[] args) throws Exception {
    java.io.File myfile;
    String mypath;
    mypath = "/Users/tonyg/Downloads";
    myfile = new java.io.File(mypath + "/file.txt");
    Scanner myinfile = new Scanner(myfile);
    for (int l = 0; l < 9; l++) {
        int val1;
        val1 = myinfile.nextInt();
        System.out.println(val1);
    }
  1. Initialize Scanner out of loop 循环初始化Scanner
  2. Catch FileNotFoundException 捕获FileNotFoundException
  3. Combine declaration and initialization of variables (in this particular case) 结合声明和变量初始化(在这种情况下)
  4. Use clear camelCase identifiers for your variables 对变量使用清晰的camelCase标识符
  5. Avoid using l (lowercase L ) as a variable identifier. 避免使用l (小写L )作为变量标识符。 In many fonts l (lowercase L ) and 1 (digit one) look similar. 在许多字体中, l (小写L )和1 (数字1)看起来很相似。 This may lead to future bugs caused by typos. 这可能会导致将来由于错别字而引起的错误。
  6. Close your Scanner in the end 最后关闭Scanner

(#1, #2 and #6 can be achieved by using try-with-resources ) (可以使用try-with-resources来实现#1,#2和#6)

String dirPath = "/Users/tonyg/Downloads";
String filePath = dirPath + "/file.txt";
int count = 9;

try(Scanner scanner = new Scanner(new File(filePath))){
    for (int i = 0; i < count; i++) {
        int value = scanner.nextInt();
        System.out.println(value);
    }
} catch (FileNotFoundException e){
    // Print stack-trace or do something else
    e.printStackTrace();
}

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

相关问题 Java:如何从文件中读取文本和数字 - Java: How can I read text and numbers from a file 如何使我的Java程序从文本文件读取日语(Unicode编码)? - How do I get my java program to read Japanese (Unicode encoding) from a text file? 如何从文本文件中读取Java中的二维数组 - How do I read from a text file into a 2 dimensional array in Java 如何从Java中的文本文档中读取整数变量? - How do I read an integer variable from a text document in Java? 如何从Java中的文本文件读取列表 - How to read a list from a text file in java 如何从文本文件java中读取数字和字母的组合 - How can I read a combination of numbers and letters from a text file java JAVA 如何从一个表达式中读取包含数学运算符和数字的用户输入? - JAVA How do I read an input from the user containing math operators and numbers in one expression? Java:我如何从文件的每一行中读取4个数字。 并将它们放入变量中? - Java: How do I read 4 numbers from each line in a file. And put them into variables? 如何从Selenium Webdriver Java获取只读文本的文本 - How to do get text of read only text from Selenium webdriver java 如何在Java中获得要打印的五个随机数而不重复的列表? - How do I get a list of five random numbers to be printed without duplicates in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM