Tutorial: Accepting String Input in Java

ยท

3 min read

Some Java programs need input from the user. To accept input from the user, use the Scanner class. The Scanner class has methods that accept different types of input. This tutorial describes string input.

First, import the Scanner class at the top of your program:

import java.util.Scanner;  //NEW

public class Example {
  public static void main(String[] args) {







  } 
}

Next, create a Scanner object:

import java.util.Scanner;

public class Example {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);  //NEW





  } 
}

This Scanner object accepts input from System.in, or the console. The variable name input refers to the Scanner object.

Now prompt the user to enter a string:

import java.util.Scanner;

public class Example {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("What's your name? ");  //NEW




  } 
}

Since System.out.print() does not output a newline character, the user will input the string on the same line as the question.

Next, use the Scanner input variable to accept one string from the console:

import java.util.Scanner;

public class Example {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("What's your name? ");

    String s = input.next();  //NEW


  } 
}

The next() method accepts the next string from the console. If the user enters multiple string values, separated by whitespace, the next() method captures the first string value only.

Finally, output the string value that the user entered:

import java.util.Scanner;

public class Example {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("What's your name? ");

    String s = input.next();
    System.out.println("Hello " + s); //NEW

  } 
}

That is the entire program. Here is a sample run of the program:

What's your name? Edwin
Hello Edwin

To accept multiple string values as input, execute the next() statement multiple times:

import java.util.Scanner;

public class Example {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("What are your first and last names? ");

    String first = input.next();
    String last = input.next();
    System.out.println("Hello " + first + " " + last);

  } 
}

The first next() statement accepts the first input string. The second next() statement accepts the second input string.

Here is a sample run of the program:

What are your first and last names? Edwin Torres
Hello Edwin Torres

Finally, to accept the entire line of input as a string, use the nextLine() method:

import java.util.Scanner;

public class Example {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("What are the colors of the rainbow? ");

    String colors = input.nextLine();
    System.out.println("The colors are: " + colors);

  } 
}

Here is a sample run of the program:

What are the colors of the rainbow? red orange yellow green blue indigo violet
The colors are: red orange yellow green blue indigo violet

That's it! The Scanner class is useful for accepting input from the user. This tutorial discusses string input and the different ways to accept string values from the user.

Thanks for reading. ๐Ÿ˜ƒ

Follow me on Twitter @realEdwinTorres for more programming tips and help.

ย