EverCrest Message Forums
You are not logged in. Login or Register.
Author
Topic: Java Help =(
Aaron (the good one)
posted 10-11-2004 08:07:27 PM
Edit: The code inbetween the //// is just debugging text

code:

public TimeOfDay (String theTime)
{
//////////////////////////////////////
System.out.println(theTime.length());
//////////////////////////////////////
for (int temp = 0, counter = 0; temp < 999; counter += 1)
{
///////////////////////////////////////
System.out.println(theTime.charAt(counter));
///////////////////////////////////////
if (new Character(theTime.charAt(counter)).equals(":"))
{
if (temp == 0)
{
mHours = Integer.parseInt(theTime.substring(temp, counter - 1));
temp = counter + 1;
}
else
{
mMinutes = Integer.parseInt(theTime.substring(temp, counter - 1));
mSeconds = Integer.parseInt(theTime.substring(counter + 1, theTime.length() - 1));
temp = 999;
}
}
}

}//END CONSTRUCTOR


I get this error

G:\Java\ques1006>java ques1
7 <-- the length of the string
4 <-- character 0
: <-- character 1
4 <-- character 2
5 <-- character 3
: <-- character 4
4 <-- character 5
5 <-- character 6
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
at java.lang.String.charAt(String.java:444)
at TimeOfDay.<init>(TimeOfDay.java:99)
at ques1.main(ques1.java:5)


How am I getting an out of bounds exception? The only line I see where it could happen is at the
mSeconds = Integer.parseInt(theTime.substring(counter + 1, theTime.length() - 1));

But I go to the length of the string - 1 so I should be safe. What's the deal?

Delidgamond fucked around with this message on 10-11-2004 at 08:11 PM.

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Tatsukaze
wants Kloie's mom OH SO BAD
posted 10-11-2004 08:26:41 PM
I think the counter + 1 is the thing that's messing up. Try changing it to counter.
ProphetofDark
Pancake
posted 10-11-2004 08:27:19 PM
Ugh, I can't quite figure out the error, but I can at least tell you that you shouldn't be getting the "4" and "5" outputs for the "mSeconds" values... For some reason or another, your for loop isn't ending after you set temp = 999, and your error is coming from it trying to output the theTime.charAt(counter) value when it's too high.
Aaron (the good one)
posted 10-11-2004 08:28:55 PM
quote:
That damn dirty ProphetofDark rudely interrupted me to say;
Ugh, I can't quite figure out the error, but I can at least tell you that you shouldn't be getting the "4" and "5" outputs for the "mSeconds" values... For some reason or another, your for loop isn't ending after you set temp = 999, and your error is coming from it trying to output the theTime.charAt(counter) value when it's too high.

You're correct....interesting

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Iulius Czar
Pancake
posted 10-11-2004 08:36:54 PM
quote:
Delidgamond attempted to be funny by writing:

How am I getting an out of bounds exception? The only line I see where it could happen is at the
mSeconds = Integer.parseInt(theTime.substring(counter + 1, theTime.length() - 1));

But I go to the length of the string - 1 so I should be safe. What's the deal?


It's a "counting from zero" problem. The string is 7 characters long, but the last valid index in the string is 6:


String -> 4:45:45
Index  -> 012345678
                 ^ Seven is not a valid index

The "out of bounds" exception happens when counter==6. You can tell that because the debug statement prints the last char in the string. When you ask for the character at (counter+1), it's out of range.

Aaron (the good one)
posted 10-11-2004 08:36:58 PM
The program is not even going into the if statements
Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Aaron (the good one)
posted 10-11-2004 08:38:23 PM
quote:
That damn dirty Iulius Czar rudely interrupted me to say;
It's a "counting from zero" problem. The string is 7 characters long, but the last valid index in the string is 6:


String -> 4:45:45
Index  -> 012345678
                 ^ Seven is not a valid index

The "out of bounds" exception happens when counter==6. You can tell that because the debug statement prints the last char in the string. When you ask for the character at (counter+1), it's out of range.



My counter should never go above 5 with what I have written though.

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Aaron (the good one)
posted 10-11-2004 08:39:12 PM
The problem is with this line

if (new Character(theTime.charAt(counter)).equals(":"))

It's never finding the colon

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Iulius Czar
Pancake
posted 10-11-2004 08:40:07 PM
code:
if (new Character(theTime.charAt(counter)).equals(":")) {

... consider the following instead ...

code:
if(theTime.charAt(counter) == ':')

You can compare two chars with a simple double-equals (as long as that's kosher in the assignment).

[Edit] Now that I've looked at it again, you're comparing an instance of Character to an instance of String. Those are probably not equal (using .equals()) to each other. I bet, however that the following would work:

code:
if(new String(theTime.charAt(counter)).equals(":"))

I don't know the API off the top of my head but I think String has a constructor that just takes in a char.

Iulius Czar fucked around with this message on 10-11-2004 at 08:44 PM.

Aaron (the good one)
posted 10-11-2004 08:46:24 PM
- if(theTime.charAt(counter) == ':') -

How come that didn't work when I put the colon in "" before?


Now I am getting a problem with the Integer.parseInt

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
ProphetofDark
Pancake
posted 10-11-2004 08:46:38 PM
Hrmmm... I've not done enough coding right now to remember, but it very well might be the double quotations around the ":" in that if statement... Should be single quotes, you're comparing a character and a character, not a character and a string.
Iulius Czar
Pancake
posted 10-11-2004 08:52:40 PM
Any text in double quotes is an instance of String.
Any single character in single quotes is a char primative.

So,
("Text".equals("Text")) //True
('c' == 'c') //True
("c" == 'c') //False
("c".equals('c')) //False

ProphetofDark
Pancake
posted 10-11-2004 08:55:31 PM
What's the new error message, Delid? I need the debugging practice.
ProphetofDark
Pancake
posted 10-11-2004 09:01:00 PM
Oooh, nevermind, found the error... For theTime.substring, you don't need the "counter - 1" in there, it should just be "counter". Substring truncates the string at the second value - 1 automatcially, thus, when it tries to parse "4" (The mHours value) it's actually just getting an empty string "".

ProphetofDark fucked around with this message on 10-11-2004 at 09:03 PM.

Iulius Czar
Pancake
posted 10-11-2004 09:07:29 PM
Loopless version

public TimeOfDay (String theTime) {
    int firstColon = theTime.indexOf(":");
    int secondColon = theTime.lastIndexOf(":");
    
    int hours = Integer.parseInt( theTime.substring(0, firstColon-1) );
    int minutes = Integer.parseInt( theTime.substring(firstColon+1, secondColon-1) )
    int seconds = Integer.parseInt( theTime.substring(secondColon+1, theString.length()-1 );
}

API=friend (so long as this isn't an assignment about loops)

Iulius Czar fucked around with this message on 10-11-2004 at 09:10 PM.

Aaron (the good one)
posted 10-11-2004 09:10:15 PM
What does API stand for!
Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Aaron (the good one)
posted 10-11-2004 09:11:46 PM
quote:
ProphetofDark stopped masturbating to porn to say;
Oooh, nevermind, found the error... For theTime.substring, you don't need the "counter - 1" in there, it should just be "counter". Substring truncates the string at the second value - 1 automatcially, thus, when it tries to parse "4" (The mHours value) it's actually just getting an empty string "".

See, I wish my teacher would have told us that. Now that I took out the - 1 to the counter it works perfectly now

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Iulius Czar
Pancake
posted 10-11-2004 09:13:47 PM
quote:
Delidgamond wrote, obviously thinking too hard:
What does API stand for!

Application Programming Interface, the documentation of all builtin classes and methods. (Here's the one for Java 1.4.2)

Iulius Czar fucked around with this message on 10-11-2004 at 09:17 PM.

Aaron (the good one)
posted 10-11-2004 09:26:12 PM
quote:
That damn dirty Iulius Czar rudely interrupted me to say;
Application Programming Interface, the documentation of all builtin classes and methods. (Here's the one for Java 1.4.2)

I already have a link to that. Alidane gave it to me earlier today. I just had no idea what API stood for

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
All times are US/Eastern
Hop To: