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

Published: 01 September 2023
on channel: Neil Gillies | Coding & Music
306
4

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


On this page of the site you can watch the video online #7 | User input with the Java Scanner | Java tutorial for beginners with a duration of hours minute second in good quality, which was uploaded by the user Neil Gillies | Coding & Music 01 September 2023, share the link with friends and acquaintances, this video has already been watched 306 times on youtube and it was liked by 4 viewers. Enjoy your viewing!