0

I have the following code of a class used to read a file (.txt that contains 3 lines of 180 numbers each) and it runs perfectly. The problem is that as you may see I am just returning the array "data1", and I would like to return 3 arrays (data1,data2,data3) and 3 integer (total1,total2,total3) into the same method. Any ideas of how could i do it?. Thank you so much ;)!

public int [] OpenFile() throws IOException
{
    FileReader reader = new FileReader(path);
    BufferedReader textReader = new BufferedReader(reader);

    int numberOfTimeZones = 3;
    int[]  data1 = new int[180];
    int[]  data2 = new int[180];
    int[]  data3 = new int[180];
    int total1 = 0;
    int total2 = 0;
    int total3 = 0;

    for (int i = 0; i < numberOfTimeZones; i++){
        if (i == 0)
        {
            String firstTimeZone = textReader.readLine();

            String[] val =  firstTimeZone.split ("\\s+");
            for (int u = 0; u < val.length; u++)
            {
                int stats =  (int)(Math.ceil(Math.abs(Double.parseDouble(val[u]))));
                total1 += stats;
                total1= total1/180;
                data1[u] = stats;
            }

        }
        else
        if (i == 1)
        {
            String secondTimeZone = textReader.readLine();
            String[] val =  secondTimeZone.split ("\\s+");
            for (int u = 0; u < val.length; u++)
            {
                int stats =  (int)(Math.ceil(Math.abs(Double.parseDouble(val[u]))));
                total2 += stats;
                total2= total2/180;
                data2[u] = stats;
            }

        }
        else
        {
            String thirdTimeZone = textReader.readLine();
            String[] val =  thirdTimeZone.split ("\\s+");
            for (int u = 0; u < val.length; u++)
            {
                int stats =  (int)(Math.ceil(Math.abs(Double.parseDouble(val[u]))));
                total3 += stats;
                total3= total3/180;
                data3[u] = stats;
            }
        }
    }
    textReader.close();
    return data1;
}

}

RedSkull
  • 47
  • 9
  • If you need to return multiple values from a method you have a following choices: array, Collection of primitives, object, Collection of objects. – PM 77-1 Sep 28 '14 at 00:57

1 Answers1

2

You can also return an Object. So for this purpose why don't you simply create a class that has your requested fields - the arrays and the integers.

If you don't want to use this simple approach, you can use Tuples. Here's an excellent example.

adhg
  • 10,437
  • 12
  • 58
  • 94