【问题标题】:java parsing text file and handling each line differentlyjava解析文本文件并以不同方式处理每一行
【发布时间】:2018-05-10 19:46:32
【问题描述】:

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

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[] 数组中。

假设文本文件的前两个数字是汽车的健康和动力。每个进行的数字是比赛之间的天数。每场比赛都会出现汽车和发动机的老化,老化的程度是每场比赛之间天数的一个因素。

我使用了 results[3][4]&[5] 并对恶化进行了硬编码并打印了结果,它可以工作,但它很垃圾。我将如何改进这种方法?我正在复制并粘贴“计算”。如何取第一行,然后将以下行放在单独的循环中?

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

【问题讨论】:

  • 您不是已经阅读第一行并分别处理其他行吗?
  • 我是吗?我的理解是我已经硬编码了文件中可以包含的行数。即,我可以通过调用数组中的每个元素来使用 36、45、36、21。但是如果有 6 天的迭代会发生什么?
  • while ((element1 = in.readLine()) != null) 读取所有行直到 EOF

标签: java loops parsing


【解决方案1】:

试试类似的东西

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

【讨论】:

  • 非常感谢您的解决方案。我喜欢简单
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
相关资源
最近更新 更多