Results 1 to 10 of 10
Thread: Using variable as array index
- 11-16-2010, 05:34 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
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){ ...
Any help would be greatly appreciated
Sal
- 11-16-2010, 06:17 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
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.
- 11-16-2010, 06:21 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
i can add the minutes to the hours and have an array of doubles but it needs to be returning hours
- 11-16-2010, 06:38 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
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"); }
Last edited by Iron Lion; 11-16-2010 at 06:57 PM.
- 11-16-2010, 07:21 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
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
- 11-16-2010, 07:24 PM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
Using a modified version of the method you provided:
Java Code:public void addDay(int dayNo, int mins) { runTimeList[dayNo] = mins; }
- 11-16-2010, 08:26 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
thats throwing an 'incompatible types' error saying requires classes.runtime found int
:S
- 11-16-2010, 08:29 PM #8
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
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];
- 11-16-2010, 10:43 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
will that not remove the connection to the runTime class?
- 11-16-2010, 10:56 PM #10
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
Similar Threads
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 09:12 PM -
Array Index out of bounds
By leapinlizard in forum New To JavaReplies: 5Last Post: 04-29-2009, 06:11 AM -
Array Index problems
By ragnor2004 in forum New To JavaReplies: 4Last Post: 03-26-2009, 08:53 PM -
[SOLVED] A direct way to get an index from an array?
By CJSLMAN in forum New To JavaReplies: 5Last Post: 10-07-2008, 06:10 PM -
problems with array index
By mary in forum New To JavaReplies: 2Last Post: 08-01-2007, 05:30 PM
Bookmarks