From the course: Advanced Java Programming

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Reading console input with a scanner

Reading console input with a scanner - Java Tutorial

From the course: Advanced Java Programming

Start my 1-month free trial

Reading console input with a scanner

- [Instructor] Sometimes, there is a need to read simple inputs from the console with Java. For example, a test program that is console-driven or some kind of program that requires the user to input numbers will require data to be read in. The quick and simple way to do this is using the scanner class. The scanner class is useful for reading small amounts of input and is relatively easy to use. Scanners work with primitive types, i.e. all of the built-in data types in Java. They can be used to pass ints, longs, floats, and so on. In this example, I have a class called person which has three fields called name, age, and phoneNumber. I want a user to be able to create a person by inputting their information to the console. To do this, I have a class called PersonCreator with a main method. The first thing I will need to do is create a scanner object which I will put in a main method, and I will call scanner. I will then put = new.Scanner, and I need to pass in System.in. System.in is an…

Contents