File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loops" Watch "loops" New topic
Author

loops

Paul Baker
Greenhorn

Joined: Feb 05, 2002
Posts: 6
I'm having some trouble with my loops, they are only running once and then crashing my program.
for (int k = 0; k < (upper.length()); k ++) {
currentChar = "" + upper.charAt(k);
System.out.println(currentChar);
for (int j = 0; j <(upper.length()); j ++) {
nextChar = "" + upper.charAt(j+1);
if (currentChar.equals(nextChar)){
count++;
}
else
continue;
System.out.println(count);
}
}
Does anybody see anything wrong?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
What do you mean "crashing"? If you're getting an exception, that's not a crash, it's an important diagnostic message that is probably telling you exactly why it can't continue.
Can you post the stack trace? Also, can you post the code that this loop is contained within? It references variables like upper and currentchar that aren't declared in your loop.
Thanks!
Rob


Rob
SCJP 1.4
Paul Baker
Greenhorn

Joined: Feb 05, 2002
Posts: 6
I'm sorry, I meant causing an exception here is the code and the exception.
public static void charCount(String just){
String currentChar = "";
String upper = just.toUpperCase();
String nextChar = "";
int count = 1;
System.out.println(upper);
for (int k = 0; k < (upper.length()); k ++) {
//System.out.println(upper.length());
currentChar = "" + upper.charAt(k);
System.out.println(currentChar);
System.out.println(k);
for (int j = 0; j <(upper.length()); j ++) {
nextChar = "" + upper.charAt(j + 1);
//System.out.print(j);
if (currentChar.equals(nextChar)){
count++;
}
else
continue;
}
System.out.print(count);
count = 0;
}
}//charCount
the exception is:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 31
at java.lang.String.charAt(String.java:516)
at A2Q3.charCount(A2Q3.java:94)
at A2Q3.getInformation(A2Q3.java:59)
at A2Q3.main(A2Q3.java:113)
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
This code will always give you an error:
for (int j = 0; j <(upper.length()); j ++) {
nextChar = "" + upper.charAt(j + 1);

You are iterating over the number of characters in the string "upper", but the last loop iteration, where j=upper.length()-1, you try to access a character at a position that doesn't exist (j+1).
Say upper = "word". When j = 3, the expression "upper.charAt(3+1)" will throw the exception you are seeing, because there is no character at position 4. (String character positions are zero-based, like arrays).

Rob
aymen esawey
Ranch Hand

Joined: Jan 29, 2002
Posts: 61



just remove +1 from(j+1)
hope i'm right.


Aymen Esawey<br />SCJP <img src="smile.gif" border="0"> <br /><a href="http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=32&t=001968" target="_blank" rel="nofollow">how to nuke the SUN </a>
 
 
subject: loops