Term 1: Lesson 3 - User Input and Variables
Variable
A name for a spot in the computer's memory; this value can change while the program runs
What is Input?
Input is pulling the information into the computer
The Scanner class
Scanner scan = new Scanner(System.in); This class adds input to your Java programs
What is wrong with the following code? String s = nextLine();
Should be: String s = scan.nextLine(); because you have to call the nextLine() method from a Scanner type object.
Declare
To create the variable in memory; necessary to let Java know what types of information it will store
What happens when we declare a variable?
We tell the computer to allocate some space in memory for something. We tell it what the name of the thing will be, so it knows how to find it later, and what the type of the thing will be, so it knows how much space to set aside:
concatenation
When we glue a literal String and a String variable together using a plus sign
What is wrong with this code? String s; System.out.println(s);
You cannot print a String without setting its value. The string, s, needs to be initialized with a value before it can be printed.
For Input to work you need what?
You have to have a spot in memory to hold this value
Which character do you use to print to the next line?
\n
Which is used to insert a tab character?
\t
Variables can ________ in programming, as they are just containers.
change
We can think of variables as
containers in memory.
variable name rules
they cannot contain spaces, cannot start with numbers, and cannot share the names of commands, such as "print".
Scanner allows what?
us to take user input and incorporate it into our programs.
While n.1, int, 1test, and a name are illegal, what is a legal variable name in Java?
value because it is not a special (aka "reserved") word in Java and can be used in variable names.
What do we do when we want to print things out?
we can combine literal Strings (things in quotation marks) and variables
What happens when we use a Scanner to take input?
we read a String from the keyboard and store it as a variable.