EverCrest Message Forums
You are not logged in. Login or Register.
Author
Topic: Hey guys I need help with Java again :cool:
Aaron (the good one)
posted 10-21-2004 04:51:21 PM
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.

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Aaron (the good one)
posted 10-21-2004 05:12:09 PM
It is DEFINATLY the resize command. I outputted the length of the array right before and right after ResizeDouble and it came out '1' each time.
Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Snugglits
I LIKE TO ABUSE THE ALERT MOD BUTTON AND I ENJOY THE FLAVOR OF SWEET SWEET COCK.
posted 10-21-2004 05:18:51 PM
Aren't the "[]" brackets supposed to touch the STRING, DOUBLE, variable name etc?
[b].sig removed by Mr. Parcelan[/b]
Alidane
Urinary Tract Infection
posted 10-21-2004 05:30:43 PM
quote:
Everyone wondered WTF when Waisz wrote:
Aren't the "[]" brackets supposed to touch the STRING, DOUBLE, variable name etc?

I think this is the problem. Nothing jumps out at me that's excessively wrong, although System.arraycopy is kinda retarded.

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.

Aaron (the good one)
posted 10-21-2004 06:14:51 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.


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.

Delidgamond fucked around with this message on 10-21-2004 at 06:15 PM.

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Iulius Czar
Pancake
posted 10-21-2004 07:56:04 PM
[edit] Oops, blew away my comment by mistake. Here we go again:
Your ResizeString() and ResizeDouble() methods return arrays, as they should, but the addScore() method doesn't do anything with the return. Let them overwrite the old value, like so:

public boolean addScore(String judge, double score) {
    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;
}

Iulius Czar fucked around with this message on 10-21-2004 at 07:58 PM.

Aaron (the good one)
posted 10-21-2004 07:57:13 PM
That's the exact same thing I wrote...
Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Iulius Czar
Pancake
posted 10-21-2004 08:16:35 PM
The difference is the reassignment, here:

Scores = ResizeDouble (Scores, 5);
Judges = ResizeString (Judges, 5);

(see my stealthedit for explanation)

Aaron (the good one)
posted 10-21-2004 08:23:00 PM
aha

thanks

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Aaron (the good one)
posted 10-21-2004 08:31:55 PM
ok new question.

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).

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Iulius Czar
Pancake
posted 10-21-2004 09:16:58 PM
The Calendar class will do the work for you, although it's a rather unwieldy thing to deal with. Here is the basic idea:

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.

 

import java.util.Calendar;
import java.util.Date;

//Create two instances of Calendar. Why they there's no normal constructor, I have no idea.
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();

//Initialize the date and time for both calendars. Replace the numbers with your own variables.
//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.YEAR, 2004);
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);

//Convert both dates to the number of miliseconds from Jan 1, 1970.
//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();

//Subtract the end from the start to get the interval in ms, then convert ms to days. Note that the work of
// handling leap-years and things was done by getTime() previously so we can use straight arithmetic.
long interval = (end-start) / 1000 / 60 / 60 / 24;

Iulius Czar
Pancake
posted 10-21-2004 09:21:32 PM
The Calendar class will do the work for you, although it's a rather unwieldy thing to deal with. Here is the basic idea:

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.

 

import java.util.Calendar;
import java.util.Date;

//Create two instances of Calendar. Why they there's no normal constructor, I have no idea.
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();

//Initialize the date and time for both calendars. Replace the numbers with your own variables.
//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.YEAR, 2004);
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);

//Convert both dates to the number of miliseconds from Jan 1, 1970.
//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();

//Subtract the end from the start to get the interval in ms, then convert ms to days. Note that the work of
// handling leap-years and things was done by getTime() previously so we can use straight arithmetic.
long interval = (end-start) / 1000 / 60 / 60 / 24;

Aaron (the good one)
posted 10-21-2004 09:40:10 PM
Thanks. That helps me abit but I won't use it. I'll go to my teacher tomorrow and ask him about what he wants me to do. That Calender method is pretty nifty Though you could have done your sets with just straight values instead of (field, value) and saved yourself alot of typing
Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
All times are US/Eastern
Hop To: