• 1/23/14 11:16 pm
- 493 views • 0 today
- 3
- 0
- 2
64
So, we're back! This time I would like to teach you something a little more complex, getting user input. This will also lead into variables, which you have hopefully already done in math class at some point in your life. So let's start by writing the following line under package but above class:
import java.util.Scanner;
What this will do is it will allow us to use the "Scanner" feature in Java. This is a class in Java which allows you to read various values. We will be using this to get user input in the console. Now we will actually create our scanner. What we want to do first is delete whatever you have in your "main" method at the moment and at the top of it write:
Scanner inputScanner = new Scanner(System.in);
This is telling the program that we want a new Scanner called "inputScanner". Once we have written this line, we can start using it to read what the user has to say. Why don't we make this into a small program that asks for the user's name and then greets him/her with it. We can do this as follows:
System.out.println("What is your name?");
String name = inputScanner.nextLine();
System.out.println("Hello, " + name + "!");
Let's break this down. When we say System.out.println("a bunch of words"), the program will print "a bunch of words" into the console.
Now this is where we get into variables. When we write "String name = " what does that mean? Well, it means that we are creating a variable called name which is type String. The equals sign sets whatever is on it's left to whatever is on it's right. Therefor, what we are actually saying when we write "String name = inputScanner.nextLine();" is that we are going to make the variable name, whatever is returned by "inputScanner.nextLine". In other words we are making name whatever the user has typed into the console.
Now, using our knowledge of variables, System.out.println("a bunch of" + type + "words") means that if the variable type is set to "cool", the program will print "a bunch of cool words", while if type was equal to "rude", the program would print "a bunch of rude words". This applies to our program because we are doing System.out.println("Hello " + name + "!"). This means that if you write that your name is "Bob", it will print "Hello, Bob!" and if you type "I am a useless sentence", it will print "Hello, I am a useless sentence!".

So as you can see there are a bunch of fun things that you can do with setting variables and getting user input. In the next tutorial we will see if we can actually make this into a runnable application! You can find tutorial 4 here: http://www.planetminecraft.com/blog/coding-for-noobs-programming-tutorial-series-tutorial-4-exporting-our-program/!
import java.util.Scanner;
What this will do is it will allow us to use the "Scanner" feature in Java. This is a class in Java which allows you to read various values. We will be using this to get user input in the console. Now we will actually create our scanner. What we want to do first is delete whatever you have in your "main" method at the moment and at the top of it write:
Scanner inputScanner = new Scanner(System.in);
This is telling the program that we want a new Scanner called "inputScanner". Once we have written this line, we can start using it to read what the user has to say. Why don't we make this into a small program that asks for the user's name and then greets him/her with it. We can do this as follows:
System.out.println("What is your name?");
String name = inputScanner.nextLine();
System.out.println("Hello, " + name + "!");
Let's break this down. When we say System.out.println("a bunch of words"), the program will print "a bunch of words" into the console.
Now this is where we get into variables. When we write "String name = " what does that mean? Well, it means that we are creating a variable called name which is type String. The equals sign sets whatever is on it's left to whatever is on it's right. Therefor, what we are actually saying when we write "String name = inputScanner.nextLine();" is that we are going to make the variable name, whatever is returned by "inputScanner.nextLine". In other words we are making name whatever the user has typed into the console.
Now, using our knowledge of variables, System.out.println("a bunch of" + type + "words") means that if the variable type is set to "cool", the program will print "a bunch of cool words", while if type was equal to "rude", the program would print "a bunch of rude words". This applies to our program because we are doing System.out.println("Hello " + name + "!"). This means that if you write that your name is "Bob", it will print "Hello, Bob!" and if you type "I am a useless sentence", it will print "Hello, I am a useless sentence!".

So as you can see there are a bunch of fun things that you can do with setting variables and getting user input. In the next tutorial we will see if we can actually make this into a runnable application! You can find tutorial 4 here: http://www.planetminecraft.com/blog/coding-for-noobs-programming-tutorial-series-tutorial-4-exporting-our-program/!
2709683
6


Have something to say?
For the rest I look forward to the other tutorials, subbed for that reason to you.