Test the input string in the while condition. : Scanner « Development Class « Java






Test the input string in the while condition.

  

import java.util.Scanner;

public class MainClass {
  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    int number = 2;
    String input = "Y";
    while (input.equalsIgnoreCase("Y")) {
      System.out.println(number + " ");
      System.out.print("Do you want to keep counting?" + " (Y or N)");
      input = sc.next();
      number += 2;
    }
  }
}

   
  








Related examples in the same category

1.Read int by using Scanner Class
2.Use Scanner to read user input
3.Count all vowels inputed from keyboard
4.Use Scanner to compute an average of the values
5.Use Scanner to compute an average of the values in a file
6.Use Scanner to read various types of data from a file
7.Use Scanner to compute an average a list of comma-separated values
8.Demonstrate findInLine()
9.use Scanner to read input that contains several different types of data
10.Letting the user decide when to quit
11.This program demonstrates console input.
12.Reading double value from console with ScannerReading double value from console with Scanner