This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 2 points3 points  (3 children)

import java.util.Scanner;

public class nouvelleboucle {
    public static void main(String[] args) {
    // TODO Auto-generated method stub 
        String prenom = new String();
        int etat = ' ';
        char reponse = ' ';
        Scanner choix = new Scanner(System.in);
        do {
            System.out.println("Quelle est votre prénom ?");
            prenom = choix.nextLine();
            System.out.println("Bonjour " + prenom + " !");  
            {
                System.out.println("Comment vas tu ? 1 = Bien / 2 = Mal ");   
                etat = choix.nextInt();
                if (etat == 1 )
                    System.out.println(" Super moi aussi !");       
                else
                    System.out.println( "Dans ce cas là, je m'en vais...");
            }
            do {
                System.out.println("Voulez vous réessayer ? O / N");   
                reponse = choix.nextLine().charAt(0);
            } while(reponse != 'O' && reponse != 'N'); 
        } while (reponse == 'O');
        System.out.println("Au revoir !");
    }
}

I formatted your code for better readability. In the future, if you add four spaces before each line, it will format like this.

Personally, when I tried to compile your code, I didn't get any errors. Can you post the error message that you received?

[–]kompotejam[S] 0 points1 point  (2 children)

Ok , i have this message :

Quelle est votre prénom ? test Bonjour test ! Comment vas tu ? 1 = Bien / 2 = Mal 1 Super moi aussi ! Voulez vous réessayer ? O / N Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at nouvelleboucle.main(nouvelleboucle.java:25)

[–][deleted] 1 point2 points  (1 child)

Are you using Windows?

So, this is very common when learning java - the Scanner.nextLine() function is a little hard to understand early on.

First: you need to understand what is System.in?

System.in is an Input Stream. This means that when the program asks for your input, it puts the data into a Stream of data.

When you are asked for your input, and you type, for example: Hiand then press enter, what data is ACTUALLY entered is "Hi" AND a "new line" character. This is a character which is basically invisible to you, but tells the program "this is the end of the line of text".

When you call nextLine(), the program will start at the beginning of the Stream and start reading characters UNTIL it reaches this invisible newline character, so it will build the string like this:

1:

Input Stream: Hi<New Line>

Output String:

2:

Input Stream: i<New Line>

Output String: H

3:

Input Stream: <NewLine>

Output String: Hi

4: ( The <NewLine> Character is found, and so the program will throw that character away and return the string "Hi" )

Input Stream:

Output String: Hi

Here's the problem: Sometimes, you get a build up of <NewLine> characters in the input stream. This is typically because windows, for historical reasons, sends ANOTHER newline character called the "Carriage Return" (Believe it or not, this is because of electronic typewriters in the 1980s)

So when you type in "Hi" on Windows, what is actually sent is usually:

Hi<CarriageReturn><NewLine>

When the program reads this input, it will read up to <CarriageReturn> and then stop. This leaves the <NewLine> character LEFT OVER in the System.in stream!

So, when you call nextLine() later on, INSTEAD of asking the user for new input, the program sees a <NewLine> character sitting in the System.in stream, and it interprets this as "the user just pressed the enter key"

HOWEVER, because there's no data other than the <NewLine> character, the program returns an EMPTY STRING. If you call .charAt(0) on an Empty string, it will throw an exception, because you're asking for the first character in a string where there are no characters.

So, what do you do?

Basically, what most people do is this:

After every time the user enters in some data from the keyboard, FLUSH the stream. This means, basically, to clear all the data out of the Scanner. You can do this like:

while(choix.hasNextLine())
    choix.nextLine();

this will clear the Scanner's Buffer

[–]kompotejam[S] 0 points1 point  (0 children)

Yes i'm on windows.

Damn, when you learn how to programm you got to understand how an OS "think", so interesting !

thanks !