逐个java获取int数字0到9

时间:2013-02-15 13:09:11

标签: java digits

您好我想知道从txt文件中扫描int数的最佳和最简单的方法 一个接一个的数字 我能找到的数字是0到9 不是 例如:

24351235312531135

我希望每次都能扫描这些输入

2 
4
3
5
1
2

编辑:

My input in txt file is something like this
 13241235135135135
 15635613513513531
 13513513513513513
 13251351351351353
 13245135135135315
 13513513513513531

具有已知位数的6行     .... 我找到了这段代码但是没有用

import java.util.Scanner;


public class ScannerReadFile {

public static void main(String[] args) {

    // Location of file to read
    File file = new File("data.txt");

    try {

        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            System.out.println(line);
        }
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

}

我无法找到面对

的确切查询

4 个答案:

答案 0 :(得分:2)

试试这个:

public static void main(String[] args) {
    File file = new File("data.txt");
    try {

        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            for (int i = 0; i < line.length(); i++) {
                System.out.println(line.charAt(i));
            }
        }
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

答案 1 :(得分:1)

你需要一个for循环:

for(int i=0;i<line.length();i++)
System.out.println(Integer.parseInt(line.charAt(i)));

答案 2 :(得分:0)

检查此示例,了解如何在Java中打开和读取文件: http://alvinalexander.com/blog/post/java/how-open-read-file-java-string-array-list

同时检查此问题:Java File - Open A File And Write To It

答案 3 :(得分:0)

您可以迭代每行的字符:

while (scanner.hasNextLine()) {
   String line = scanner.nextLine();
      for (char c: line.toCharArray()) {
        System.out.println(c);
      }
}