#7 | User input with the Java Scanner | Java tutorial for beginners

Опубликовано: 09 Апрель 2025
на канале: Neil Gillies
129
3

In this video we'll start learning about using the Java scanner to capture some basic user input.

Here is the code used in this lesson:

import java.util.Scanner;
public class AddUp {
public static void main(String[] args) {
// simple program to add two integers and display the result
int firstNum, secondNum, result; // variables
// initialise the scanner
Scanner scanner = new Scanner(System.in);
// ask the user for first number
System.out.println("Please enter your first number");
firstNum = scanner.nextInt();
// ask the user for the second number
System.out.println("Please enter your second number");
secondNum = scanner.nextInt();
// calculate the result
result = firstNum + secondNum;
// display the result to console
System.out.println(firstNum + " plus " + secondNum + " equals " + result);
}
}

Here are the lesson notes:

// The Java Scanner Class can be used for very basic text input
// It can be used alongside the console for basic input and output
// The scanner is an add-on utility that needs to be imported before use
// The scanner is used when learning programming and for testing code without the need for a full UI