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

时间:2018-05-10 19:46:32

标签: java loops parsing

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

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]&amp; [5]并且硬编码恶化并打印结果并且它可以工作,但它很糟糕。我该如何改进这种方法?我正在复制并粘贴'计算'。如何取第一行,然后将以下行放在一个单独的循环中?

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

1 个答案:

答案 0 :(得分:0)

尝试类似

的内容
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