2

I am trying to create two different arrays from separate columns in a csv file. The csv file has one column for 'month's' and one column for 'temperatures'. This is what I have so far but I can't figure out how to turn these values into arrays.

public class InputOutput
{
    private double temperature;

    public InputOutput()
    {
        List<String> temperatures = new ArrayList<String>();
        Iterator<String> tempIterator = temperatures.iterator();

    }

    public static double getTemp()
    {
        double tempReading = 0.0;
        try
        {
            String fileName = "C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv";                 //location of MNMINPLS.csv
            File file = new File(fileName);
            Scanner scan = new Scanner(file);                                               //scanner reads file
            while(scan.hasNext())                           //iterator
            {
                String tempData = scan.next();
                String[] nums = tempData.split(",");
                tempReading = Double.parseDouble(nums[3]);

                System.out.println(tempReading);
            }//end while
            scan.close();
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }//end try/catch
        return tempReading;
    }//end method getTemp

    public static int getMonth() 
    {
        int m = 0;
        try
        {
            String fileName = "C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv";                 //location of MNMINPLS.csv
            File file = new File(fileName);
            Scanner scan = new Scanner(file);                                               //scanner reads file
            while(scan.hasNext())                       //iterator
            {
                String month = scan.next();
                    if(month == month)
                        m++;
                String[] timeOfYear = month.split(",");
                m = Integer.parseInt(timeOfYear[0]);

                System.out.println(m);
            }
            scan.close();
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }//end try/catch
        return m;
    }
}       
Joseph Kraemer
  • 111
  • 1
  • 2
  • 10
  • You want arrays or array lists (seeing as you have an array list for temperatures)? Whenever you don't know how to use a standard Java class, it's a good idea to start by consulting the [documentation](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html). You'll find that you can answer your own question most of the time after doing so. HINT: ArrayList has an .add() method. – MarsAtomic Apr 02 '15 at 19:26

4 Answers4

0
temperatures.add(Double.toString(tempReading));

Just add the value to the ArrayList.

Aruna Karunarathna
  • 901
  • 1
  • 14
  • 54
0

If I understand correctly, you have <date>,temp rows like this:

4,12,2014,70.3 //12 Apr 2014 weather
5,1,2014,74.5 //1 May 2014 weather
6,27,2014,85.8 //27 June 2014 weather

And what you want in the end is an int array monthsArr with the value {4, 5, 6}, plus a parallel double array tempsArr with the value {70.3, 74.5, 85.8}

First, make your temperatures ArrayList a member of the class (move it up to where private double temperature is), and add an additional months ArrayList. Make temperatures an ArrayList<Double> and months an ArrayList<Integer>

Then, you can add the parsed value to the appropriate ArrayList using the add() method.

Finally, when done parsing, the easiest way to convert to an Array is to use the toArray() method like so:

double[] tempsArr = temperatures.toArray(new double[]);
int[] monthsArr = months.toArray(new int[]);

The ArrayList<E> API spec (Java SE7) is here

AntsySysHack
  • 129
  • 1
  • 7
0

you need to read your file only once

double[] temperatures=new double[maxsize];
 String[] months=new String[maxsize];
 int index=0;
    while(scan.hasNext())                           //iterator
            {
                String tempData = scan.next();
                String[] nums = tempData.split(",");
                tempReading = Double.parseDouble(nums[3]);
                monthReading = nums[2]; //or whichever index
                temperatures[index]=tempReading;
                months[index]=monthReading;
                index++
          }
bl3e
  • 1,188
  • 12
  • 32
0

How do I read and parse a CSV file? I would use third party CSV reader say CSV reader. The "why not write own CSV parser" list can be found here. Hope this will help :) :

import com.opencsv.CSVReader;

import java.io.FileReader;
import java.util.List;
import java.util.ArrayList;

CSVReader reader = new CSVReader(
                        new FileReader("C:\\Users\\Joseph\\Downloads\\MNMINPLS.csv"));
String [] nextLine;

// If you want lists
List<Integer> months       = new ArrayList<Integer>();
List<Double>  temperatures = new ArrayList<Double>();
while (null != (nextLine = reader.readNext())) 
{
    months.add(
                Integer.valueOf(nextLine[0]));
    temperatures.add(
                Double.valueOf(nextLine[1]));
}

// or if you want your data as an array see http://docs.oracle.com/javase/7/docs/api/java/util/List.html#toArray(T[])
Integer[] monthsArr = months.toArray(new Integer[0]);
Double[]  tempsArr  = temperatures.toArray(new Double[0]);
Community
  • 1
  • 1