Results 1 to 10 of 10
  1. #1
    SalParadise is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Using variable as array index

    Hello,
    I am having a spot of trouble using a vaiable as the address to an array.

    I have an array which holds a number of hours & minutes for a race time. The array represents the days of the week and i want to have the day number (0 monday, 1 tuesday etc) as the index of the address. How would i implement this, so far i have just got the array itself
    Java Code:
    public class RunWeek {
        
    private RunTime RunTimeList[] = new RunTime[5];
    
    public RunWeek(){
        
        this.RunTimeList[0] = null;
        this.RunTimeList[1] = null;
        this.RunTimeList[2] = null;
        this.RunTimeList[3] = null;
        this.RunTimeList[4] = null;
    }
        
    public void addDay(int dayNo, int hours, int mins){
    
    ...
    so it will add the hours/minutes to the array index specified by the dayNo

    Any help would be greatly appreciated

    Sal

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Would it not be simpler just to pass minutes to the method as a single variable, instead of hours and minutes? That way, you could just have an array of ints instead of RunTimes.

  3. #3
    SalParadise is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    i can add the minutes to the hours and have an array of doubles but it needs to be returning hours

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    As far as your program's concerned, working with a single variable for minutes is preferable to two. For human readability, to get output in the form of "1 hour and 4 minutes" from an input of 64 minutes, try a method like:

    Java Code:
    public void minutesToHours(int minutes) {
        System.out.println(minutes/60 + 
        (minutes >= 120 || minutes < 60) ? " hours" : " hour" + //Prints "hour" for 1 hour, or "hours" for 0 or >=2
        " and " + minutes%60 + " minutes");
    }
    (I'm pretty new myself so that might not be exactly right, and there might be a method to do something similar in one of the standard classes, but hopefully it should help)
    Last edited by Iron Lion; 11-16-2010 at 05:57 PM.

  5. #5
    SalParadise is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    ahh thankyou thats much easier to handle. Do you know how to use a variable as the index of the array? there is an input of days as a number then this will be the array index but im unsure how to code it.

    Many Thanks

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Using a modified version of the method you provided:

    Java Code:
    public void addDay(int dayNo, int mins) {
        runTimeList[dayNo] = mins;
    }

  7. #7
    SalParadise is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    thats throwing an 'incompatible types' error saying requires classes.runtime found int
    :S

  8. #8
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    It's still expecting a RunTime instead of an int. You'll need to amend the array declaration to:

    Java Code:
    private int runTimeList[] = new int[5];

  9. #9
    SalParadise is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    will that not remove the connection to the runTime class?

  10. #10
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    What does your RunTime class do? I'm assuming it just acted as a container for your hours and minutes variables, in which case it's no longer needed.

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM
  3. Array Index problems
    By ragnor2004 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 07:53 PM
  4. Replies: 5
    Last Post: 10-07-2008, 05:10 PM
  5. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:30 PM

Posting Permissions

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