gpt4 book ai didi

java - 如何在Java中解析文件中的整数?

转载 作者:行者123 更新时间:2023-12-01 19:04:01 25 4
gpt4 key购买 nike

我在文本文件 n.txt 中包含整数,其格式为 2 列整数,并且各列之间用空格分隔。

我想读取这些整数并转义它们之间的空格,并将这两个整数输入到 2 个单独的整数中。下面的代码用于解析得到的String。我想知道是否有任何 String 方法可以将 String 拆分为单独的数组并使用空格作为分隔符?

String URL="C:\\User\\Nimit\\Desktop\\n.txt";
File f = new File(URL);
FileReader inputF = new FileReader(f);
BufferedReader in = new BufferedReader(inputF);

int[] a= new int [1000];
int[] b= new int [1000];

String s =in.readLine();

while(s!=null)
{
int i = 0;
a[i] = Integer.parseInt(s,b[i]); //this is line 19

//*not important*// System.out.println(a[i]);
s = in.readLine();
}

in.close();

System.out.println("the output of the file is " +f);

最佳答案

我建议您使用扫描仪:

Scanner s = new Scanner(new File(fileName));

int[] a = new int[1000];
int[] b = new int[1000];

int count = 0;
while (s.hasNextInt()) {
a[count] = s.nextInt();
b[count] = s.nextInt();
count++;
}

s.close();

关于java - 如何在Java中解析文件中的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10947281/

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