2 Replies - 307 Views - Last Post: 25 April 2012 - 02:04 AM Rate Topic: -----

#1 solarmythos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-April 12

*Having trouble accessing array from a different method*

Posted 25 April 2012 - 12:05 AM

I nearly have my program complete, but now I am running into trouble accessing an array(arrays actually) from a method different than the one they were created in. I have been on google for the past half hour or more researching on this issue but still have not fixed my problem.

For starters here is one of my methods which is called within the project class ( the name of .java is project)

This method retrieves data from user and loads it into the array which I cannot access from the other method following this one.


// getItem Method defintion **************

public static void getItemData() {
Scanner input = new Scanner(System.in);			// create scanner
System.out.println("enter number of items");
int amtOfitms = input.nextInt();				// get # of items
String[] item = new String[amtOfitms];			// initialize arrays
float[] cost = new float[amtOfitms];
float[] weight = new float[amtOfitms];
int i = 0;
int j = 0;
int k = 0;     
                                 
// begin data collection loop *******
for(i = 0; i < amtOfitms; i++,j++,k++){
	System.out.println("Please enter item "+(i+1)+"'s name. Press 0 to exit");
	item[i] = input.nextLine();
	System.out.println("Please enter item "+(i+1)+"'s weight.");
	weight[j] = input.nextFloat();
	System.out.println("Please enter item "+(i+1)+"'s cost.");
	cost[k] = input.nextFloat();
	}
	
// print data in table ******	
for(i = 0, j = 0, k = 0; i < item.length; i++,j++,k++)
System.out.println("\t|"+item[i]+"|-----|"+weight[j]+"|-----|"+cost[k]+"|");

sumData();    // call the other method to calculate the data
}




Now when I call this method, it "cannot find symbol" and is referring to the array variable.


public static void sumData(){

String[] item = getItemData.item;	// initialize arrays
float[] cost = getItemData.cost;
float[] weight = getItemData.weight;

final float TAXES = .07f;
final float SHIP_RATE = .05f;
float weightSum = 0;
float costSum = 0;
int j=0;
int k=0;
//sum numerical arrays

for(j = 0, k = 0; j < item.length; j++,k++){
	weightSum += weight[j];
	costSum += cost[k];
    }
	
// calculate + echo data
float invoiceTax = costSum * TAXES;
float invoiceShip = (weightSum * SHIP_RATE) + 5;
float grandTotal = costSum + invoiceTax + invoiceShip;
System.out.printf( "\t|Subtotal: %-6.2f|\n\t|Taxes: %-6.2f|\n\t|Shipping: %-6.2f|\n",
					costSum, invoiceTax, invoiceShip);
System.out.printf("\t|Grand Total: %-6.2f|\n\n", grandTotal);

runningTotal(invoiceTax, invoiceShip, grandTotal);

}



The error message I receive is:
project.java:53: error: cannot find symbol
String[] item = getItemData.item;
symbol: variable getItemData
location: class project

and it repeats for the other two variables that I attempt to retrieve in the same manner


I tried not using the Method.arrayvar and it can't find the array.(in this example I use the methods name as MyClass)
Also I tried using MyClass.arrayvar but still no luck.

Any ideas what I am doing wrong? I have been at this all day long and I have made some serious head way but now I seem to be in a rut.

I have attached the full program code ( 4 methods or so ) if anyone would like to check out the full code.

All comments, ideas, tips are VERY MUCH appreciated! Thanks in advance!!

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: *Having trouble accessing array from a different method*

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1980
  • View blog
  • Posts: 4,824
  • Joined: 10-September 10

Re: *Having trouble accessing array from a different method*

Posted 25 April 2012 - 12:26 AM

The array variables you declared inside the method getItemData() are local variables to that method. In other words, they are only 'visible' to that method, but you can change that by declaring the array variables outside the method. Here's one way:

public class MyClass
{
   protected static int[] myArray = new int[20];

   public static void main( String[] args )
   {
      // fill the array myArray with data
      getData();

      // print the data stored in myArray()
      printData();

      // the rest of main()
   }

   // method to get data and store in myArray()
   protected static void getData()
   {
      // get data from the user and store in myArray()
   ]

   // another method that uses myArray()
   protected static void printData()
   {
      // collect data from myArray() and print it
   }

}

Was This Post Helpful? 1
  • +
  • -

#3 solarmythos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-April 12

Re: *Having trouble accessing array from a different method*

Posted 25 April 2012 - 02:03 AM

EUREKA! I've got it! Thanks a TON! :rockon:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1