• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

loops

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




just remove +1 from(j+1)
hope i'm right.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic