Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Rafał Stasiak
Rafał Stasiak
3,763 Points

Java Scaner class with do while loop

Hello Team,

I started my first project of Hang Man and I get stock in the main class.

package com.teamtreehouse;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    System.out.println("Hello Guest!\n In this game you will have nine attempts to gues the word I think of :)\n Plese write a letter");
    Scanner scanner = new Scanner(System.in);
    char c = scanner.next(".").charAt(0);

    do {
        System.out.println("BUM!! You can enter only letters!\nPlease enter a letter");
        scanner.next(".").charAt(0);
    } while (Character.isLetter(c) == false);
}

}

Now, no metter if I put letter or not I receive BUM! message..

How to modify my loop to prevent multiple wrong input from user?

1 Answer

Steven Parker
Steven Parker
231,248 Points

A "do...while" loop is always executed at least one time, and the test is performed after it runs.

You probably want the more typical "while" loop where the test is performed first.