Java String read from console

Introduction

To read a string from the console, invoke the next() method on a Scanner object.

For example, the following code reads three strings from the keyboard:

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      System.out.print("Enter three words separated by spaces: ");
      String s1 = input.next();/*from w  w  w  .  j a va2  s. c o  m*/
      String s2 = input.next();
      String s3 = input.next();
      System.out.println("s1 is " + s1);
      System.out.println("s2 is " + s2);
      System.out.println("s3 is " + s3);

      input.close();
   }
}



PreviousNext

Related