File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes why am I getting an infinite loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why am I getting an infinite loop?" Watch "why am I getting an infinite loop?" New topic

why am I getting an infinite loop?

Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
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;

William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

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.

Please ignore post, I have no idea what I am talking about.
Rene Marot

Joined: Jun 27, 2001
Posts: 9
You use i as an index and to read your char. Use i only as the loop index.
I agree. Here's the link:
subject: why am I getting an infinite loop?
jQuery in Action, 3rd edition