Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why am I getting an infinite loop?

 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still doing the letter to number conversion thing.
I tried to loop through the name using charAt() and I got an infinite loop. Why is this happening?
import java.util.*;
import java.lang.*;
public class Name {
public static void main(String[] args) {
String name = "Brian Candido";
String s = name.toLowerCase();
for (int i = 0; i < s.length(); i++){
i = ((s.charAt(i) - 'a') % 9) + 1;
System.out.print(i);
}

 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simplify.
Have one variable be your counter and another variable be your character that you want to print.
There is no reason to try and use one variable to do two jobs.
 
Rene Marot
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use i as an index and to read your char. Use i only as the loop index.
Bye.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic