gpt4 book ai didi

java解析文本文件并以不同的方式处理每一行

转载 作者:太空宇宙 更新时间:2023-11-04 10:28:50 25 4
gpt4 key购买 nike

我正在尝试从文本文件中读取整数,但我想以与其余行不同的方式处理第一行,并采用以下行并循环一些计算。下面是我的文件阅读器,我的问题是关于下一部分的,但这可能提供一些上下文。

public class main
{
BufferedReader in;
public main() throws FileNotFoundException
{
System.out.println("please enter the name of the text file you wish you import. Choose either inputs or lotsainputs. Nothing else");
Scanner keyboard = new Scanner(System.in);
String filename = keyboard.nextLine();
File file = new File(filename);

System.out.println("You have loaded file: \t\t"+filename);
in = new BufferedReader(new FileReader(file));

Scanner inputFile = new Scanner(file);
String element1 = inputFile.nextLine().toUpperCase();
try
{
while ((element1 = in.readLine()) != null)
{
System.out.println("Line to process:\t\t"+element1);
myCalculations(element1);
}
}
catch (Exception e)
{
e.printStackTrace();
}

}

第一个文本文件如下所示:

200 345 
36

第二个文本文件如下所示:

200 345 
36
45
36
21

这里是调用的方法:

public static void myCalculations(String s)
{
String[] items = s.split(" ");
int[] results = new int[100];
String errorMessage = "that's a wrap!";
for (int i = 0; i < items.length; i++)
{
try {
int stuff = Integer.parseInt(items[i]);
results[i] = stuff;
}
catch (NumberFormatException nfe) {
System.out.println(results);
System.out.println(errorMessage);
}
}
int health = result[0];
int power = result[1];
int days = result[2];
some calculations....
returns new health and power of the car.
same calculations but results[3] as the time period
returns new health and power of car
etc....
}

该方法解析整数,将它们放入 results[] 数组中。

假设文本文件的前两个数字是汽车的健康状况和功率。每个进行的数字都是比赛之间的天数。每场比赛都会有汽车和发动机的老化,老化程度是每场比赛之间的天数。

我已经使用了结果[3][4]和[5]并对恶化进行了硬编码并打印结果,它可以工作,但它非常糟糕。我该如何改进这个方法?我正在复制并粘贴“计算”。如何获取第一行,然后将以下行放入单独的循环中?

理想情况下,文本文件中的天数可以有所不同。即,可能有 5 场比赛,也可能有 3 场比赛,程序将处理这两种情况。

最佳答案

尝试类似的事情

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Scanner;

public class Main {


public static void main(String[] args) throws FileNotFoundException {

System.out.println(
"please enter the name of the text file you wish you import. Choose either inputs or lotsainputs. Nothing else");
Scanner keyboard = new Scanner(System.in);
String filename = keyboard.nextLine();
File file = new File(filename);


System.out.println("You have loaded file: \t\t" + filename);
BufferedReader in = new BufferedReader(new FileReader(file));

String element1 = null;
try {
element1 = in.readLine();
}catch (Exception e) {
// TODO: handle exception
}

String[] firstLine = element1.split(" ");

Arrays.stream(firstLine).forEach(fl -> {
System.out.println("First line element: " + fl);
});


//Do the staff
String otherElement = null;


try {
while ((otherElement = in.readLine()) != null) {
System.out.println("Line to process:\t\t" + otherElement);

}
} catch (Exception e) {
e.printStackTrace();
}

}

}

文件结果:

1 2
3
5
7

You have loaded file:       
First line element: 1
First line element: 2
Line to process: 3
Line to process: 5
Line to process: 7

关于java解析文本文件并以不同的方式处理每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50280453/

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