Java,将带有特殊符号的txt文件中的数字读入二维整数数组
所以,首先,我将试着解释一下我想要实现的目标。我是java新手,我愿意学习它。问题是: 让我们假设我有一个txt文件,里面填充了如下内容: 模式是:| a b |,当a是ID,b是实际数字时。我读过java中的Scanner、FileReader和BufferedReader类,我试图使用它们从文件中恢复整数,并将它们保存到我的二维整数数组([2][1000])。[0][0-999]将是ID,[0-999]将是实际数字。现在是我陷入困境的时刻——我能读懂前两个零,但我不知道如何跳过|符号 我现在的代码:Java,将带有特殊符号的txt文件中的数字读入二维整数数组,java,arrays,java.util.scanner,bufferedreader,filereader,Java,Arrays,Java.util.scanner,Bufferedreader,Filereader,所以,首先,我将试着解释一下我想要实现的目标。我是java新手,我愿意学习它。问题是: 让我们假设我有一个txt文件,里面填充了如下内容: 模式是:| a b |,当a是ID,b是实际数字时。我读过java中的Scanner、FileReader和BufferedReader类,我试图使用它们从文件中恢复整数,并将它们保存到我的二维整数数组([2][1000])。[0][0-999]将是ID,[0-999]将是实际数字。现在是我陷入困境的时刻——我能读懂前两个零,但我不知道如何跳过|符号 我现
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ShortSearching {
public static void main(String[] args) {
int[] tall = new int[100];
int i = 0, k = 0;
Tab tablica = new Tab();
File textFile = new File("byteObs.txt");
try (Scanner scanner = new Scanner(textFile)) {
while (scanner.hasNextInt()) {
k++;
tall[i++] = scanner.nextInt();
System.out.println(tall[i]);
if (k == 2) {
scanner.findInLine("|");
scanner.skip("|");
k = 0;
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + textFile.toString());
//e.printStackTrace();
}
/*String byteObs = "byteObs.txt";
File textFile = new File(byteObs);
Scanner input = new Scanner(textFile);
//short[] tabID,tabVal = new short[1000];
byte k = 0;
while (input.hasNextLine()) {
for (int i = 0; i <= 999; i++) {
for (int j = 0; j <= 999; j++) {
if (k != 2) {
if (input.hasNextShort()) {
//matrix[i][j] = input.nextShort();
//System.out.printf("%d ", matrix[i][j]);
k++;
}
}
else {
input.skip("|");
k=0;
}
}
}
}
input.close(); */
}}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类短搜索{
公共静态void main(字符串[]args){
int[]高=新int[100];
int i=0,k=0;
Tab tablica=new Tab();
File textFile=新文件(“byteObs.txt”);
尝试(扫描仪=新扫描仪(文本文件)){
while(scanner.hasNextInt()){
k++;
tall[i++]=scanner.nextInt();
系统输出println(高[i]);
如果(k==2){
scanner.findInLine(“|”);
scanner.skip(“|”);
k=0;
}
}
}catch(filenotfounde异常){
System.out.println(“未找到文件:+textFile.toString());
//e、 printStackTrace();
}
/*字符串byteObs=“byteObs.txt”;
文件textFile=新文件(字节对象);
扫描仪输入=新扫描仪(文本文件);
//short[]tabID,tabVal=newshort[1000];
字节k=0;
while(input.hasNextLine()){
for(int i=0;i扫描器在其许多方法中需要表示正则表达式(regex)的模式。在skip
方法中也会发生这种情况,但在regex中
是表示或运算符的元字符。要使其成为简单的文本,需要通过在其前面添加\
来转义它(请记住,要在字符串中创建\
文本,还需要使用另一个\
对其进行转义)
因此,表示
字符的正则表达式是\\\\\
,需要将其写成字符串“\\\\\\”
另一个问题是,skip
需要正则表达式,该正则表达式需要匹配需要跳过的整个部分,并且该部分必须正好出现在扫描仪光标的位置之后,因此在出现以下数据的情况下:
1 |
当我们调用nextInt()
时,光标将设置在1
之后(我将用I
标记它)
所以我们要跳过的模式是空格
+|
最后一个问题是,文件的所有行中都没有正确的值(它的行仅包含
|
我们将无法使用nextLine()
,因为下一个元素将不是int。在这种情况下,我们可以通过使用hasNextInt()
简单地测试此行是否以整数开头,如果它没有读取,则通过调用nextLine()
在不处理其内容的情况下使用它
所以你的代码看起来像
File textFile = new File("input.txt");
try (Scanner sc = new Scanner(textFile)) {
while (sc.hasNextLine()) {
if (sc.hasNextInt()) {
System.out.println("id = " + sc.nextInt());
System.out.println("value = " + sc.nextInt());
if (sc.hasNext("\\|"))
sc.skip(" \\|");
} else {
sc.nextLine(); // skip line if it doens't contain more integers
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + textFile);
}
你能回答你的问题,而不是用图像来发布文件中存储的实际文本数据,这样我们就可以在创建问题解决方案时使用它了吗?欢迎:)。无论如何,因为你是堆栈溢出的新手,你可能应该读一读。嗯,谢谢:我一看到这一点就接受了答案,很抱歉耽搁了。
File textFile = new File("input.txt");
try (Scanner sc = new Scanner(textFile)) {
while (sc.hasNextLine()) {
if (sc.hasNextInt()) {
System.out.println("id = " + sc.nextInt());
System.out.println("value = " + sc.nextInt());
if (sc.hasNext("\\|"))
sc.skip(" \\|");
} else {
sc.nextLine(); // skip line if it doens't contain more integers
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + textFile);
}