Udemy

Getting User Input from the Keyboard

A free video tutorial from Bryson Payne
Best-Selling Author & Professor w/students in 184 countries
Rating: 4.5 out of 5Instructor rating
5 courses
78,794 students
Getting user input from the keyboard

Lecture description

Take input from the user and turn it into a variable you can use in your code.

Learn more from the full course

Learn Java the Easy Way: Build Desktop & Android Mobile Apps

A Hands-On Introduction to Programming in Java on PC, Mac, Linux and Android Devices.

12:06:02 of on-demand video • Updated July 2017

Create fun, playable, interactive apps like the animated, multi-touch BubbleDraw app, a Secret Messages app, and a Guessing Game!
Code your own desktop and mobile apps in Java and Android.
Master concepts like variables, loops, functions, and object-oriented programming concepts in a fun way with Java!
Build progressively more advanced programs starting with simple program outlines.
English [Auto]
Now that we've let the computer pick a random number between 1 and 100 we need a variable for our users guess. So we're going to set up another whole number another integer and I'll call it guess and we'll start that number at 0 so that we can begin our game loop correctly. This game loop is going to allow the user to keep guessing while they haven't guessed the right number. So I might start that with a WHILE loop while I use parentheses for the condition or the tests that we're going to use to keep out while loop operating while the guess is not equal to and that is the exclamation mark equals. So wild guess is not equal to the number I guess will be the variable for our users guess the number will be our variable for the computer's random number and then we will start our while loop with an open curly brace and an eclipse as soon as I do that it will give me a closing curly brace and everything that I type inside these curly braces will be repeated as long as guess is not equal to the number as long as the users guess is not equal to the random number that we picked from zero to 100 or from 1 to 100. Now that we have a WHILE loop set up we need to ask the user for a guess. So we will do that in our command line by saying system not out of print line system dot out dot print line. This is going to print a line of text to the screen and we can tell the user something like we use double quotes for our strings that we want to print out guess a number between 1 and 100. And notice we end with a semi-colon there. So we've asked the user to guess a number between 1 and 100 spans and see that better than we're going to get the users guess to do this we're going to make use of that scanner object that we created right up here the scan. So I'm going to say Guess I'll variable for holding the users guess is going to depend on what the user typed into the keyboard. So we're going to scan the next integer any X T I N T with a capital Y. The next integer the command line input from the keyboard with this one command scan dot next. We've called the scanner object which is our keyboard input and we've asked it to look for the next whole number the next integer that the user entered at the keyboard and the next section we're going to see how to test to see if the user guessed the right number.