Hey,

I made this code with the bits and pieces of knowledge i have, but i don't know how to convert it into a while loop.

it is currently in a for loop.

the program counts how many certain letters there are in a sentence/ word.

    String word;
    String letter;
    int index=0;
    char singleLetter;


    Scanner keys = new Scanner (System.in);

    System.out.println("What is your word?");
    word= keys.nextLine();
    System.out.println("What is your letter?");
    letter=keys.nextLine();
    singleLetter = letter.charAt(0);



    for( int i = 0; i < word.length(); i++ ) 
    {
            if( word.charAt(i) == singleLetter ) 
    {
            index++;
    }
    }
            System.out.println( "The letter " + letter + " appeared " + index + " times in the word." );
    }
    }

any type of help would be appreciated.

thank you.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hello,

Are you looking for a replacement of the for loop?

If yes:

String word;
String letter;
int index=0;
char singleLetter;


Scanner keys = new Scanner (System.in);

System.out.println("What is your word?");
word= keys.nextLine();
System.out.println("What is your letter?");
letter=keys.nextLine();
singleLetter = letter.charAt(0);



int i = 0;
while(i<word.length()){
	if( word.charAt(i) == singleLetter ) {
		index++;
	}
	i++;
}

System.out.println( "The letter " + letter + " appeared " + index + " times in the word." );

or are you looking for a "Would you like to repeat? (y/n)" at the end of each run?

if yes:

boolean running = true;
while(running){
	String word;
	String letter;
	int index=0;
	char singleLetter;


	Scanner keys = new Scanner (System.in);

	System.out.println("What is your word?");
	word= keys.nextLine();
	System.out.println("What is your letter?");
	letter=keys.nextLine();
	singleLetter = letter.charAt(0);



	for( int i = 0; i < word.length(); i++ ) {
		if( word.charAt(i) == singleLetter ) {
			index++;
		}
	}
	System.out.println( "The letter " + letter + " appeared " + index + " times in the word." );
	
	System.out.print("Try another word? (y/n) ");
	String answer = keys.nextLine();
	if(answer.toLowerCase().equals("n")){
		running = false;
	} //Else it keeps running
}

If i'm wrong with everything so far, would you mind expanding?

-- Turt2live

There are all sorts of different loops which can be used for the same purpose but some loops are better at doing things than others. A For loop is mainly used when you want to know how many times you will require to loop e.g. you want to loop 10 times to check 10 different names a user has entered.

Where as something like a while loop is used when you really don't know how many times the loop is going to happen, when it depends on what the user does with your application.

The above post shows you how to convert the for loop to a while loop which I think is what your looking for and it's a good answer. But remember the syntax for for loops and while loops is different and if your new to Java I suggest you follow some tutorials on how to makes different types of loops and once your confident then you should try using different types of loops in your own test programs.

If you get stuck again feel free to e-mail me on "giganticknowledge@live.co.uk" lol. I'm sure I can get in touch to help you out, or you could post another topic here.

This article has been dead for over six months. Start a new discussion instead.