Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-26-2006, 11:25 AM   PM User | #1
miss_jess
New to the CF scene

 
Join Date: Mar 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
miss_jess is an unknown quantity at this point
Unhappy Array Help!!!!

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:

Scorched.txt
[/COLOR]
miss_jess is offline   Reply With Quote
Old 08-26-2006, 03:45 PM   PM User | #2
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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 :

Code:
   String[] ar = new String[1];
   ar[0] = "something"
   System.out.println(ar[0]);
So in your case you have:

Code:
        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
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 08-27-2006, 05:59 AM   PM User | #3
miss_jess
New to the CF scene

 
Join Date: Mar 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
miss_jess is an unknown quantity at this point
Unhappy Array Help!!!!

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
miss_jess is offline   Reply With Quote
Old 08-27-2006, 04:27 PM   PM User | #4
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
well one problem is there is a logic error in the code. Specifically on line 106.

Code:
                                    for(int k = 0; k <= 20; k++){
                                        
                                        list[k] = distanceTravel;
                                    }
Quote:
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.
Code:
ar[0] = distanceTraveled;
If you wanted to add to the second element:
Code:
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.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:58 AM.


Advertisement
Log in to turn off these ads.