File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loops

 
Paul Baker
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Baker
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



just remove +1 from(j+1)
hope i'm right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic