User Input

We can get user input by printing a prompt to the command-line, and then having the user type the information right after the prompt.

Basic user input instructions

Here’s how to get user input:

  • Add the following to the top of your file:
import java.util.*;
  • Create a Scanner to help you read in the input. For now, just add this line ONCE, before any user input. (Don’t do this again even if you want more input.)
Scanner s = new Scanner(System.in);
  • Print out a descriptive prompt (use System.out.print so it will be on the same line as the input):
System.out.print("Enter name: ");
  • Read in the input with the command s.nextLine(). This command returns the String that was typed by the user.
String name = s.nextLine();

Here’s the full program:

import java.util.*;
public class ConsoleInput {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter name: ");
        String name = s.nextLine();
        //now the name variable holds the name typed by the user
    }
}

Conversions

We don’t always want to read in strings from the user. We may also want to read in numbers. To do that, we need a way to convert from a string to a type like an int or double. We cannot convert from strings to ints or doubles by using type casting. We will discuss this more later on, but strings are object variables while other types of variables have primitive types. For now, just remember that strings are treated differently.

There are special parsing methods available to turn a string into a double or int. Here’s how to use them:

String str1 = "345";
String str2 = "3.76";
String str3 = "A";

//converts from a string to an int

int num1 = Integer.parseInt(str1);

//converts from a string to a double
double num2 = Double.parseDouble(str2);

Now that we know how to convert strings to different types, we can read in things like numbers and characters. Here is an example:

Scanner s = new Scanner(System.in);
System.out.print("Enter your age: ");
String str = s.nextLine();
int age = Integer.parseInt(str);

At the end of this example, the age variable holds the number typed by the user.

We can also read in the value typed by the user and convert it to the appropriate type all in one line:

Scanner s = new Scaner(System.in);
System.out.print("Enter your age: ");
int age = Integer.parseInt(s.nextLine());

Reading numbers without parsing

We can also use the nextInt() and nextDouble() commands for reading in ints and doubles from the user without doing a separate parsing step.

Here is an example reading in an int from the user:

Scanner s = new Scaner(System.in);
System.out.print("Enter your age: ");
int age = s.nextInt();

And here is an example reading in a double from the user:

Scanner s = new Scaner(System.in);
System.out.print("Enter your GPA: ");
double gpa = s.nextDouble();

NOTE: When you get a number from the user using either nextInt() or nextDouble() and then get a string from the user with nextLine(), the program’s behavior can be unexpected.

When you type a user input number like “4”, you really type the 4 and then hit Enter to submit the input. The “4” gets processed by the nextInt() command, but the newline character that came from hitting Enter stays in the input buffer. When you next do the nextLine() command, the program reads what was left in the input buffer (the newline character)…WITHOUT waiting for you to type any input.

You can avoid this behavior by always using the nextLine() command together with parsing to get your user input, as this will not leave anything in the input buffer.