...

View Full Version : Array Help!!!!



miss_jess
08-26-2006, 11:25 AM
Hey,

Basically how my program works is this:
Each user enters a velocity and angle and the distance the target travels is calculated using these values and is stored in the variable distanceTravel.

At the end of the program, i want it to display how far each persons target travelled. I have no idea how to do this. I'm thinking an array. I've started using one but it isn't working..please help!!

I've attached the code as a .txt:

4715
[/COLOR]

Aradon
08-26-2006, 03:45 PM
Hi.

I don't want to be mean but...I'm going to take it you didn't code the array traversals that the algorithm uses. The problem is simple.

When you access an array you have to access an element in the array such as :



String[] ar = new String[1];
ar[0] = "something"
System.out.println(ar[0]);


So in your case you have:


for(int h = 0; h < list.length; h++) {
System.out.println(list[]);
}

Do YOU see what's missing with the array? Compare my example to yours.


Remember that you created the for loop to go through all the elements in the array and so there is a variable that the for loop uses and that increments each time it goes through the loop.

Hope this helps. Please Remember the rules on homeworks as well.

-Aradon

miss_jess
08-27-2006, 05:59 AM
I've tried that before but it didn't work. I think the problem maybe adding the values from distanceTravel into the array. I have no idea how to do that!

Thanks for your help btw!
Jess

Aradon
08-27-2006, 04:27 PM
well one problem is there is a logic error in the code. Specifically on line 106.



for(int k = 0; k <= 20; k++){

list[k] = distanceTravel;
}



I've tried that before but it didn't work. I think the problem maybe adding the values from distanceTravel into the array. I have no idea how to do that!

If you want to add values to any array all you have to do is an assignment statement just like you would add an int or anything to that matter.

So if you wanted to add to the first element you would do.


ar[0] = distanceTraveled;

If you wanted to add to the second element:


ar[1] = distanceTraveled;


And so forth and so forth. If you wanted to add to the next empty element in the array then you would have to keep track of which one that is. Starting at zero and incrementing the number up each time you enter something into the array.

As far as beginning java code goes that's the best solution. Later you'll learn how to enter an unlimited (well virtually unlimited) amount of data into a list. But that is for a later date.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum