code:
public double Scores [] = new double [1];
public String Judges [] = new String [1];
int next = 0; //This holds the next available array index
The following is the code that is giving me the errors. The error I get is an array out of bounds error. It goes through 'check 1' twice (once because my first value can be stored in, and the second time because it hasn't hit the error yet) and 'check 2' once. The only thing I can see wrong is with my Resize functions.
code:
public boolean addScore(String judge, double score)
{if (score > highest || score < lowest)
{
return false;
}
////////////////////////////////////////
System.out.println("Check 1");
////////////////////////////////////////
if (next == Scores.length)
{
////////////////////////////////////////
System.out.println("Check 2");
////////////////////////////////////////
ResizeDouble (Scores, 5);
ResizeString (Judges, 5);
}Scores[next] = score;
Judges[next] = judge;
next++;
return true;
}
code:
private static double [] ResizeDouble (double [] myArray, int howMany)
{
double temp [] = myArray;
myArray = new double [temp.length + howMany];
System.arraycopy(temp,0,myArray,0,temp.length);
return myArray;
}private static String [] ResizeString (String [] myArray, int howMany)
{
String temp [] = myArray;
myArray = new String [temp.length + howMany];
System.arraycopy(temp,0,myArray,0,temp.length);
return myArray;
}
Here is my tester:
code:
public class scoretester
{
public static void main (String [] Args)
{
score event = new score(0,10);//TOTAL = 55, AVG = 5.5, fAVG = 4.4
event.addScore("CAD", 1);
event.addScore("RUS", 2);
event.addScore("FRA", 3);
event.addScore("SPN", 4);
event.addScore("GER", 5);
event.addScore("CUB", 6);
event.addScore("IRQ", 7);
event.addScore("KOR", 8);
event.addScore("JPN", 9);
event.addScore("CHI", 10);System.out.println("\n \n" + event.averageScore());
System.out.println("\n \n" + event.finalScore());
}
}
The Russians are giving me problems. Fucking Russians.
quote:
Everyone wondered WTF when Waisz wrote:
Aren't the "[]" brackets supposed to touch the STRING, DOUBLE, variable name etc?
Also, which array is going out of bounds? The stack dump should say. Alidane fucked around with this message on 10-21-2004 at 05:31 PM.
quote:
Waisz attempted to be funny by writing:
Aren't the "[]" brackets supposed to touch the STRING, DOUBLE, variable name etc?
It doesn't matter. It works either way.
And Alidane, how do I check that? I am just compiling it in a command prompt. The error I get is just an outof bounds array exception at line 57 or something.
Delidgamond fucked around with this message on 10-21-2004 at 06:15 PM.
edit: and the array that is going out of bounds is the Scores because the length of the array isn't changing because something is wrong with Resize.
public boolean addScore(String judge, double score) { Iulius Czar fucked around with this message on 10-21-2004 at 07:58 PM.
if (score > highest || score < lowest) { return false; }
if (next == Scores.length) {
Scores = ResizeDouble (Scores, 5);
Judges = ResizeString (Judges, 5);
}
Scores[next] = score;
Judges[next] = judge;
next++;
return true;
}
Scores = ResizeDouble (Scores, 5);
Judges = ResizeString (Judges, 5);
(see my stealthedit for explanation)
thanks
Without brute forcing my way through this...
How would I cleanly make a function that takes one date (you have day, month and year) and save the difference in DAYS into a variable (account for leap years, 28 days in February, and the 30 and 31 day months).
1)Create two calendars.
2)Set up one with the start date and the other with the end date.
3)Convert both dates to milliseconds from a common third date.
4)Subtract
5)Convert the interval to days.
//Create two instances of Calendar. Why they there's no normal constructor, I have no idea. //Initialize the date and time for both calendars. Replace the numbers with your own variables. end.set(Calendar.YEAR, 2004); //Convert both dates to the number of miliseconds from Jan 1, 1970. //Subtract the end from the start to get the interval in ms, then convert ms to days. Note that the work of
import java.util.Calendar;
import java.util.Date;
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
//Since you're probably not dealing with times, leaving those as midnight (all zeroes) is fine.
start.set(Calendar.YEAR, 2004);
start.set(Calendar.MONTH, 8);
start.set(Calendar.DAY_OF_MONTH, 12);
start.set(Calendar.HOUR_OF_DAY, 0);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
end.set(Calendar.MONTH, 8);
end.set(Calendar.DAY_OF_MONTH, 13);
end.set(Calendar.HOUR_OF_DAY, 0);
end.set(Calendar.MINUTE, 0);
end.set(Calendar.SECOND, 0);
end.set(Calendar.MILLISECOND, 0);
//getTime() is called twice intentionally. The first call returns an instance of Date. Date has a getTime()
// method too, but it returns a long value, which is what we're after..
long startMilliseconds = start.getTime().getTime();
long startMilliseconds = end.getTime().getTime();
// handling leap-years and things was done by getTime() previously so we can use straight arithmetic.
long interval = (end-start) / 1000 / 60 / 60 / 24;
1)Create two calendars.
2)Set up one with the start date and the other with the end date.
3)Convert both dates to milliseconds from a common third date.
4)Subtract
5)Convert the interval to days.
//Create two instances of Calendar. Why they there's no normal constructor, I have no idea. //Initialize the date and time for both calendars. Replace the numbers with your own variables. end.set(Calendar.YEAR, 2004); //Convert both dates to the number of miliseconds from Jan 1, 1970. //Subtract the end from the start to get the interval in ms, then convert ms to days. Note that the work of
import java.util.Calendar;
import java.util.Date;
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
//Since you're probably not dealing with times, leaving those as midnight (all zeroes) is fine.
start.set(Calendar.YEAR, 2004);
start.set(Calendar.MONTH, 8);
start.set(Calendar.DAY_OF_MONTH, 12);
start.set(Calendar.HOUR_OF_DAY, 0);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
end.set(Calendar.MONTH, 8);
end.set(Calendar.DAY_OF_MONTH, 13);
end.set(Calendar.HOUR_OF_DAY, 0);
end.set(Calendar.MINUTE, 0);
end.set(Calendar.SECOND, 0);
end.set(Calendar.MILLISECOND, 0);
//getTime() is called twice intentionally. The first call returns an instance of Date. Date has a getTime()
// method too, but it returns a long value, which is what we're after..
long startMilliseconds = start.getTime().getTime();
long endMilliseconds = end.getTime().getTime();
// handling leap-years and things was done by getTime() previously so we can use straight arithmetic.
long interval = (end-start) / 1000 / 60 / 60 / 24;