-1

Please test my code please. I am trying to terminate my program when user press enter key or not texting anything.But my program is not terminate.Help me please!

Scanner s = new Scanner(System.in);
        String a =s.nextLine();
        
        while(s.hasNext())
        {
            System.out.println(s.nextLine());
        }
Ma Moe
  • 1
  • 2

2 Answers2

1

You want it to echo whatever the user types until they hit enter without typing anything in?

Scanner s = new Scanner(System.in);
String a = s.nextLine();
while(!a.equals("")) {
    System.out.println(a);
    a = s.nextLine();
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

This thread might help you. Java Scanner hasNext() doesn't return false

your s.hasNext() is pending in your code because the Scanner is on System.in

pref
  • 1,651
  • 14
  • 24