Nerses Kazarian
Nerses Kazarian

Reputation: 1

delay when typing input while using scanner

when i type what is stored inside the string variables b and p, it does not respond until I have clicked the enter key several times.

After this point the program only prints out what is inside the else statement. The only string variable that the program is responding to is j.

The code:

package legit;

import java.util.Scanner;

public class Gamee {


   public static void main(String args[]){

       Scanner sc = new Scanner(System.in);
        String j = "good, how are you?";
        String b = "good";
        String p = "bad";

        System.out.println("Hello, my name is Julie the Robot");

        System.out.println("How Are You?");


        if (j.equals(sc.nextLine())) {
        System.out.println("Im Doing Great!");


        }else if (b.equals(sc.nextLine())) {                
        System.out.println("Thats Great! :)");


        }else if (p.equals(sc.nextLine())){             
        System.out.println("Thats not good");

        }else {
        System.out.println("I see...");
        }

Upvotes: 0

Views: 532

Answers (1)

Pshemo
Pshemo

Reputation: 124255

Do you know that every time you use sc.nextLine() you are asking user for new input?

Try maybe using it once before your ifs and store received input in value, then use that value in conditions.

Upvotes: 1

Related Questions