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.
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
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.
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 indexThe "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.
if (new Character(theTime.charAt(counter)).equals(":"))
It's never finding the colon
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.
How come that didn't work when I put the colon in "" before?
Now I am getting a problem with the Integer.parseInt
So,
("Text".equals("Text")) //True
('c' == 'c') //True
("c" == 'c') //False
("c".equals('c')) //False
ProphetofDark fucked around with this message on 10-11-2004 at 09:03 PM.
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.
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
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.
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