gpt4 book ai didi

java - java 获取 int 数字从零到九一一一

转载 作者:行者123 更新时间:2023-12-02 00:00:49 25 4
gpt4 key购买 nike

您好,我想知道从 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();
}

}

}

我无法找到我面临的确切查询

最佳答案

试试这个:

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();
}

}

关于java - java 获取 int 数字从零到九一一一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14895487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com