I have a two dimensional array named "multiarray." The first is [7] , the second is initialized currently as [500]. I am reading in from a text file that has a random number of entries going to the second array. The array will never have 500 entries and I need to know how many entries there are.
I was thinking that y < multiarray[x].length would tell me what I needed to know but it seems to be looping.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;
public class DOW
{
public static void main(String[] args)
{
File file = new File ("/Users/***/RandomInts.txt") ;
try
{
Scanner scanner = new Scanner(file) ;
//Formatter formatter = new Formatter (new File ("outputfile.txt")) ;
int [][] multiarray = new int [7][500];
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
int counter7 = 0;
while (scanner.hasNext())
{
int dow = scanner.nextInt () ;
int temp = scanner.nextInt () ;
dow = dow -1;
if(dow == 0)
{
multiarray[dow][counter1] = temp;
counter1 ++;
}
if(dow == 1)
{
multiarray[dow][counter2] = temp;
counter2 ++;
}
if(dow == 2)
{
multiarray[dow][counter3] = temp;
counter3 ++;
}
if(dow == 3)
{
multiarray[dow][counter4] = temp;
counter4 ++;
}
if(dow == 4)
{
multiarray[dow][counter5] = temp;
counter5 ++;
}
if(dow == 5)
{
multiarray[dow][counter6] = temp;
counter6 ++;
}
if(dow == 6)
{
multiarray[dow][counter7] = temp;
counter7 ++;
}
}
for (int x = 0; x < 7; x++)
{
int hightemp = 0;
int lowtemp = 0;
int avetemp = 0;
for (int y = 0; y < multiarray[x].length ; y++)
{
if (multiarray[x][y] > hightemp)
{
hightemp = multiarray[x][y];
}
if (multiarray[x][y] < lowtemp)
{
lowtemp = multiarray[x][y];
}
avetemp = avetemp + multiarray[x][y];
}
//avetemp = avetemp /
}
//formatter.format (" %d %d \n " , dow , temp ) ;
//formatter.flush () ;
//formatter.close () ;
//int[] dow;
//dow = new int [7];
//int[] temp;
//temp = new int [100];
//int x = 0;
//while ( x < temp.length)
//{
//System.out.println (temp[x]);
//++x;
//temp[x]=0;
//}
}
catch (FileNotFoundException e)
{}
}
}
The reason I want to know the array length is because I want to call the number in for some math.
avetemp = avetemp / multiarray[x].length
I have a counter already for [x] as its being read in from the file but I hoped to not use it here so that I didn't have to manually write out everything.
A sample of what the input text file looks like :
5 67
2 -15
1 -40
7 32
6 -24
7 33
5 -32
3 57
3 41
6 51
Basically the first column is the day of the week and the second is a temperature.
Integerboxed type. You can just count all the non-null Integers in the array to get the data length (the array length will still be 500).