Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Array Help!!!!

  1. #1
    New to the CF scene
    Join Date
    Mar 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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]

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Mar 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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;
                                        }
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •