How to import the Java Scanner |Video upload date:  · Duration: PT5M41S  · Language: EN

Quick guide to import and use the Java Scanner for reading console input with example code and common pitfalls

So you need to accept user input in Java and you heard Scanner is the popular kid in town. Good call. Scanner is simple, standard, and irritatingly specific about newlines. This short guide shows how to import Scanner set it up read different types of input and avoid the common newline trap while keeping things tidy and mildly amusing.

Import the Scanner class

Put the Scanner import near the top of your Java file so the compiler knows what you mean when you type Scanner. A typical line looks like this

import java.util.Scanner

Friendly reminder, when you paste into a real Java file remember to end the import with a semicolon character so the compiler stops complaining.

Create a Scanner that reads console input

Instantiate a Scanner that reads from System.in so your program can accept what the user types. Use a clear name so you do not accidentally shadow other classes named Scanner elsewhere.

Scanner scanner = new Scanner(System.in)

If you prefer automatic cleanup use try with resources which closes the Scanner for you when the block ends.

try (Scanner scanner = new Scanner(System.in)) {
    // use scanner here
}

Why closing matters but also does not matter

Closing a Scanner frees resources and is polite in long running applications. For tiny demo programs you can skip it and nobody will write angry Stack Overflow posts. If your code is a library avoid closing System.in because callers may want it open. Try with resources is a neat compromise for short apps.

Read strings and primitives with the right method

Scanner has a handful of convenient methods. Pick the one that matches the data you expect.

  • nextLine() reads a whole line including spaces and then returns it
  • next() grabs the next token which is good for single words
  • nextInt() reads an integer value
  • nextDouble() reads a floating point number

The newline gotcha and how to survive it

nextInt and nextDouble do not consume the trailing newline after the number. That means a subsequent call to nextLine can return an empty string and you will stare at your screen wondering why your program hates you. Two simple fixes work well.

  • After reading a number call scanner.nextLine() to consume the leftover newline before you read the next full line
  • Or read full lines with nextLine() and parse them with Integer.parseInt or Double.parseDouble to avoid mixing token and line methods

Example pattern to avoid surprises

int age = scanner.nextInt()
scanner.nextLine() // consume leftover newline
String name = scanner.nextLine()

Quick checklist for beginners

  • Add import java.util.Scanner at the top of the file
  • Create a Scanner that reads from System.in
  • Choose the right method for the input type
  • Handle the extra newline when mixing token and line reads
  • Use try with resources for short programs and avoid closing System.in in libraries

There you go. You can now import Scanner read console input and handle the newline gremlins like a pro. If you want a slightly more robust approach consider reading lines and parsing them which saves you from debugging phantom empty strings at midnight.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.