简体   繁体   中英

Reading Doubles from .txt File into Array

I'm just getting started with java and I am trying to read the data from a .txt file into an Array or ArrayList . The file has 52 lines (one line for each week of the year) that say the date the week started and the average cost of gas that week. Here are the file's contents:

Jan 03, 1994    0.992
Jan 10, 1994    0.995
Jan 17, 1994    1.001
Jan 24, 1994    0.999
Jan 31, 1994    1.005
Feb 07, 1994    1.007
Feb 14, 1994    1.016
Feb 21, 1994    1.009
Feb 28, 1994    1.004
Mar 07, 1994    1.007
Mar 14, 1994    1.005
Mar 21, 1994    1.007
Mar 28, 1994    1.012
Apr 04, 1994    1.011
Apr 11, 1994    1.028
Apr 18, 1994    1.033
Apr 25, 1994    1.037
May 02, 1994    1.04
May 09, 1994    1.045
May 16, 1994    1.046
May 23, 1994    1.05
May 30, 1994    1.056
Jun 06, 1994    1.065
Jun 13, 1994    1.073
Jun 20, 1994    1.079
Jun 27, 1994    1.095
Jul 04, 1994    1.097
Jul 11, 1994    1.103
Jul 18, 1994    1.109
Jul 25, 1994    1.114
Aug 01, 1994    1.13
Aug 08, 1994    1.157
Aug 15, 1994    1.161
Aug 22, 1994    1.165
Aug 29, 1994    1.161
Sep 05, 1994    1.156
Sep 12, 1994    1.15
Sep 19, 1994    1.14
Sep 26, 1994    1.129
Oct 03, 1994    1.12
Oct 10, 1994    1.114
Oct 17, 1994    1.106
Oct 24, 1994    1.107
Oct 31, 1994    1.121
Nov 07, 1994    1.123
Nov 14, 1994    1.122
Nov 21, 1994    1.113
Nov 28, 1994    1.117
Dec 05, 1994    1.127
Dec 12, 1994    1.131
Dec 19, 1994    1.134
Dec 26, 1994    1.125

I'm having trouble reading the file into the ArrayList. This is what I have so far:

import java.io.*;
import java.util.ArrayList;

public class GasPrices 
{
    public static void main(String[] args) throws IOException
    {
        String fileName = "1994_weekly_gas_averages.txt";

        ArrayList<Double> gas = new ArrayList<>();

        try  
        {
            FileReader reader = new FileReader(fileName);
            BufferedReader bReader = new BufferedReader(reader);
            String line;

            while ((line = bReader.readLine()) !=null)
            {
                String g = String.valueOf(line);
                gas.add(Double.parseDouble(g));
                System.out.println(gas);
             }
         } catch (IOException e)
        {
            System.out.println("Exception: " + e);
        }
    }
 }

I need to read those lines into an array that I can use to calculate the lowest average gas price and the highest average gas price while displayhing the week number and month related to those prices, and the average gas price for each month. I'm not sure what the best setup for this array is, factoring in all the information I need to display in one program.

I've searched around and found ways to read doubles into arrays if that's all that contained in the file, but I don't know how to pick out the double from each line without conflicting type errors or whatever else I screw up.

您尝试将所有行解析为Double ...您需要获取子字符串或拆分当前行或使用regexp

As per my understanding of your post, you want to store the double value from each line in the txt file and store in arraylist and here is the code for the same. You can use it further how ever you need.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class GasPrices {

    public static void main(String[] args) throws IOException
    {
        String fileName = "1994_weekly_gas_averages.txt";

        ArrayList<Double> gas = new ArrayList<>();

        try  
        {
            FileReader reader = new FileReader(fileName);
            BufferedReader bReader = new BufferedReader(reader);
            String line;

            while ((line = bReader.readLine()) !=null)
            {
                String g = String.valueOf(line);
                String dateStr = g.substring(0, 12);
                String doubleStr = g.substring(16);
                System.out.println("dateStr>>"+dateStr);
                System.out.println("doubleStr>>"+doubleStr);
                gas.add(Double.valueOf(doubleStr));
                System.out.println();


//                gas.add(Double.parseDouble(g));
//                System.out.println(gas);
             }
            System.out.println("gas.size()>>"+gas.size());
         } catch (IOException e)
        {
            System.out.println("Exception: " + e);
        }
    }
}

As it seems each line is fixed in your text file, you could put them into a HashMap and work from there:

File file = new File("1994_weekly_gas_averages.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
    String s = sc.nextLine();
    myMap.put(s.substring(0, 12), s.substring(15));
}

You'll then have to get all keys that match, and their values, to calculate the average.

For example, January's total could be gained by:

double total = 0;
for (String key : myMap.keySet()) {
    if (key.startsWith("Jan")) {
        total += Double.parseDouble(myMap.get(key));
    }
}
   BufferedReader br = null;
    try {
        br = new 
            BufferedReader(newFileReader("1994_weekly_gas_averages.txt"));
        String line;
        while ((line = br.readLine()) != null) {
            if(isDouble(line))
            System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
       }

     }

  static boolean isDouble(String str) {
     try {
        Double.parseDouble(str);
        return true;
     } catch (NumberFormatException e) {
        return false;
     }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM