2 Replies - 2046 Views - Last Post: 29 October 2008 - 08:41 PM Rate Topic: -----

#1 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

reading a file into multiple arrays

Posted 29 October 2008 - 08:32 PM

So i'm working on a project for school and getting an "out of bounds error" when I try to output the contents of the arrays after reading them form a file. The file format is such...

2006	1	1	8.1	-11.7
2006	1	2	5.2	-9.2
2006	1	3	6.5	-8.3
2006	1	4	6.8	-3.6
2006	1	5	12.1	-5
2006	1	6	7.3	-0.9
2006	1	7	6.3	-3.5
2006	1	8	0.8	-7.7
2006	1	9	7.9	-7.1
2006	1	10	2.2	-9.1



Here is my code....

import java.util.Scanner; //For the scanner for input and reading/writing to files
import java.io.*; // For the output and input of files


public class Assignment3 
{
	public static void main(String[] args) throws IOException
	{
		//Variables
		String FileName;
		final int ROWS = 365;
		int Counter = 0;

		
		//Create a new Scanner class for keyboard input from the user
		Scanner Keyboard = new Scanner(System.in);
		
			System.out.print("Please enter the file name: ");
			FileName = Keyboard.nextLine();
			
			File FileOpen = new File(FileName);
			if (!(FileOpen.exists()))
			{
				System.out.println("The File path you entered does not exist");
				System.out.println("Closing the program");
				
				System.exit(0);
			}
		
		//advise the user the the program is reading the file
		System.out.println("Reading File...");
		
		//create a scanner class for reading the file
		Scanner InputFile = new Scanner(FileOpen);
		
		//create the arrays for holding the data from the file
		int[] Year = new int[ROWS];
		int[] Month = new int[ROWS];
		int[] Number = new int[ROWS];
		float[] Highest = new float[ROWS];
		float[] Lowest = new float[ROWS];
		
		//use a while loop with hasNext to fill all the arrays with the data
		while(InputFile.hasNext())
		{
			Year[Counter] = InputFile.nextInt();
			Month[Counter] = InputFile.nextInt();
			Number[Counter] = InputFile.nextInt();
			Highest[Counter] = InputFile.nextFloat();
			Lowest[Counter] = InputFile.nextFloat();
			Counter++;
		}
		
		//transfer the Lowest[] array to the ColdestDay Method to calculate the coldest day
		ColdestDay(Lowest);
		
		
	}
	
	public static void ColdestDay(float[] Array)
	{
		float[] Coldest = new float[0];
		
		for(int Index = 0; Index < Array.length; Index++)
		{
			if(Array[Index] < Coldest[0])
			{
				Coldest[0] = Array[Index];
			}
		}
	}

}


cheers to anyone who helps in advance...

Is This A Good Question/Topic? 0
  • +

Replies To: reading a file into multiple arrays

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: reading a file into multiple arrays

Posted 29 October 2008 - 08:38 PM

You need to change the following if you absolutely have to use arrays...it seems pointless.

float[] Coldest = new float[0];

// change that to the next one.

float[] Coldest = new float[1];


That/those variable(s) are never initialized...
Was This Post Helpful? 0
  • +
  • -

#3 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: reading a file into multiple arrays

Posted 29 October 2008 - 08:41 PM

View PostLocke37, on 29 Oct, 2008 - 08:38 PM, said:

You need to change the following if you absolutely have to use arrays...it seems pointless.

float[] Coldest = new float[0];

// change that to the next one.

float[] Coldest = new float[1];


That/those variable(s) are never initialized...



the actual files are 365 lines long and were told to use arrays by our instructor... haha... thank you for the help though that worked perfectly... cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1