getArrayValues from an Array without using ArrayList

Creating a new Class to get Array Values and methods

Page 1 of 1

1 Replies - 1173 Views - Last Post: 05 December 2009 - 07:31 PM Rate Topic: -----

#1 happyhound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-December 09

getArrayValues from an Array without using ArrayList

Posted 05 December 2009 - 03:41 AM

Hello i am a begginner starting out creating Classes, constructors, and objects. I have a file of 12 decimals 10.,12.3 etc. I am able to get the numbers from the file however I am not able to send these values to the Class file I created. I would like to take these values and pull them into a Class which contains a method to calculate the total of all the values in the Array. I do not want to use and ArrayList.
This is what I have so far. I keep getting "This Array from class0.0" "Year Total 0.0" when ever I getTotal and getArrayValues. I am thinking its not reading my Array and I dont know what Im doing wrong. I apologize if the code is not aligned its hard to check indentions .

/** My program to test the class*/
import java.util.Scanner;
import java.io.*;
import java.util.*;



public class MonthRain
{
	public static void main(String[] args) throws IOException
	{
	
			//declare variables to open file
			  final int drops=12;
			  double [] rainfall= new double [drops];
	double rain;
	int i=0;

	//open the file.
	File file = new File("C:\\Java/Extra Credit/rain.text");
	Scanner inputFile= new Scanner(file);


	//read the file contents into the array
	System.out.println(" The Array of rain is:");

	for( i=0; i< rainfall.length;i++){//begin for loop
		 rainfall[i]= inputFile.nextDouble(); //to input array values
					  
				//test print to verify file is extracting
				System.out.println("Rainfall for Month "+(i+1)+ "= "
					  +rainfall[  i]); //print array from file
	}//end for loop

	

		   //create object yearly  total and array values
			 Rainfall total= new Rainfall(rainfall);
		 
			 //getArray Values

	total.getArrayValues();

				  //print yearly total
	System.out.println("Year Total "+total.getTotal());

  }
}
/** End of  program to test the class*/



/** Beginning of class*/



public class Rainfall


{
	private double[] rainArray;
	public static final int number = 12;
   int count;


		  Rainfall()
		 {
		  }


			public Rainfall(double[] rain)
			 {
			   rainArray = new double[number];
			   }

			public void setArrayValues( double r_Array)
			   {
	for(count=0;count<r_Array;count++)
				   rainArray[count] = r_Array;

				 System.out.println("The Array is"+ rainArray[count]);
			   }

			  public double getArrayValues()
	{
				  System.out.println("This Array from Class"+ rainArray[count]);
				   
					return rainArray[count];
				}



				public double getTotal()
				 {
				   double total = 0;
					
					  for ( int i = 0; i < number; i++ ){
						  total += rainArray[i];
					  }

						return total;
				 }
}


Is This A Good Question/Topic? 0
  • +

Replies To: getArrayValues from an Array without using ArrayList

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: getArrayValues from an Array without using ArrayList

Posted 05 December 2009 - 07:31 PM

Why don't you just replace

			public Rainfall(double[] rain)
			 {
			   rainArray = new double[number];
			   }



by
			public Rainfall(double[] rain)
			 {
			   rainArray = rain;
			   }



Then you getTotal() should work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1