java解析文本文件并以不同方式处理每一行 [英] java parsing text file and handling each line differently

查看:65
本文介绍了java解析文本文件并以不同方式处理每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to read integers from a text file, but I want to handle the first line differently from the rest, and take the following lines and loop some calculations. Below is my file reader, my question is about the next part, however this may provide some context.

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

    }

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

The first text file look like this:

200 345 
36

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

The second text file look like this:

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....
    }

该方法解析整数,并将其放入result []数组.

The method parses the integers, puts them into the results[] array.

可以说文本文件的前两个数字是汽车的健康状况和功率.每个程序编号是比赛之间的间隔天数.每场比赛汽车和引擎的性能都会下降,而下降的程度则是每场比赛之间间隔几天的时间.

Lets say the first two numbers of the text file are health and power of a car. Each proceeding number are days between races. Each race there is deterioration of the of the car and engine, the amount of deterioration is a factor of days in between each race.

我已经使用了result [3] [4]& [5],并对恶化情况进行了硬编码,并打印了结果,并且效果很好,但是效果很差.我将如何改善这种方法?我正在复制并粘贴计算".如何选择第一行,然后将以下几行放在一个单独的循环中?

I have used results[3][4]&[5] and hard code the deterioration and print the results and it works, but its pretty crap. How would I improve this method? I'm copy and pasting the 'calculations'. How do I take the first line, then put the following lines in a separate loop?

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

ideally, the number of days in the text file could vary. ie, there could be 5 races, or there could be 3 and the program will handle both cases.

推荐答案

尝试类似

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解析文本文件并以不同方式处理每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆