Tutorial: Accepting String Input in Java
Tags: java, beginners, tutorial, programming
Published: 2021-10-31 23:17:05

---
title: Tutorial: Accepting String Input in Java
published: true
description: How to accept String input in Java
tags: java, beginners, tutorial, programming
//cover_image: https://direct_url_to_image.jpg
---
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:

```java
import java.util.Scanner;  //NEW

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







  } 
}
```

Next, create a `Scanner` object:

```java
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:

```java
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:

```java
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:
```java
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:

```bash
What's your name? Edwin
Hello Edwin
```
To accept multiple string values as input, execute the `next()` statement multiple times:

```java
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:

```bash
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:

```java
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:

```bash
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`](https://twitter.com/realEdwinTorres) for more programming tips and help.*