DEV Community

Edwin Torres
Edwin Torres

Posted on

Tutorial: Accepting String Input in Java

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) {







  } 
}
Enter fullscreen mode Exit fullscreen mode

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





  } 
}
Enter fullscreen mode Exit fullscreen mode

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




  } 
}
Enter fullscreen mode Exit fullscreen mode

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


  } 
}
Enter fullscreen mode Exit fullscreen mode

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

  } 
}
Enter fullscreen mode Exit fullscreen mode

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

What's your name? Edwin
Hello Edwin
Enter fullscreen mode Exit fullscreen mode

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);

  } 
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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);

  } 
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)