Java Lesson 03 – User input / Ifs

User input

Now that we had some of the basics lets do some ‘real’ programming. We’ll start with something simple: accepting user input. As with everything there are multiple ways of doing this. We’ll do it with the Scanner class. The Scanner class is a java class used to get user input lines in the console. The constructor of the Scanner class needs an InputStream. This means that to create a Scanner object an InputStream object is needed. In our case to ‘listen’ to what the user is typing we will use the System.in. This will give us the InputStream of the system, which is in the console.

Lets try that with an example. Create a new project called Lesson 03, create a Main class and copy in the following code:

//Creates the Scanner object
Scanner scanner = new Scanner(System.in);

System.out.println("Firstname:");
//Listen for user input and get what was typed for the firstname
String firstname = scanner.nextLine();

System.out.println("Lastname:");
//Listen for user input and get what was typed for the lastname
String lastname = scanner.nextLine();

System.out.println("Fullname = " + firstname + " " + lastname);

//Close the stream to clean up memory
scanner.close();

When you copy this code in and press ctrl + s, to save, there will be a red line under Scanner meaning it has an error. This is because it doesnt recognize the class Scanner. To fix this we need to import it into our Main class. An easy way todo this is to stand with your cursor on the error and hit ctrl + 1 (or right click and press quick fix), this will bring up the quick fix menu. In this menu you should be able to choose to create your own Scanner class or to import the existing java.util.Scanner class. Choose to import and hit ctrl + s again to save. Now you shouldnt have any errors left. (I wont be telling you to save again, you need to save your code to run it and you need to save it for eclipse to compile and check if you have errors)

When you run this program it will say Firstname: and then pause. Now put your cursor in the console and type your first name. When you hit enter the program will continue and ask for your lastname. Fill it in and hit enter again and it will print out your full name.

As you might have guessed scanner.nextLine() will pause your program and wait for you input and when you have have typed something and have pressed enter it will put what you typed in the lastname String. How it does this I dont know. All I know is that it listens to the InputStream we gave in the Scanner constructor. Its kind of like using a tv. We know how to turn it on and we know how to change channels, but we dont know how it does this. Same goes with many java classes. You dont have to know how they do it, you only need to be able to work with them.

If Statements

The programming we have done so far is pretty liniar. Most of the time you will want certain pieces of code only to execute when a certain comdition is met. (.lenght() gives the number of characters in a String)

if(firstname.length() < 3){
	//less than 3 characters in the firstname
}
if(firstname.length() > 8){
	//more than 8 characters in the firstname
}

if(firstname.length() <= 3){
	//less than or equal to 3 characters in the firstname
}
if(firstname.length() >= 8){
	//more than or equal to 8 characters in the firstname
}

if(firstname.length() == 12){
	//equal to 12 characters in the firstname
}
if(firstname.length() != 12){
	//not equal to 12 characters in the firstname
}

Those are all the basic ifs. Now the firstname and lastname program we had and print out a message if they are shorter than 3 characters or longer than 16. Do this in 4 different ifs and try to use 4 different conditions.

Result SelectShow

Our current program has 4 ifs it will always check, which isnt really necessary. If firstname < 3 is true it will still check if firstname > 16 too. You can improve this with an if else construction.

if(firstname.length() == 12){
	//equal to 12 characters in the firstname
}else{
	// not equal to 12 characters in the firstname
}

if(firstname.length() < 3){
	//less than 3 characters in the firstname
}
else if(firstname.length() > 8){
	//more than 8 characters in the firstname
}
else{
	//lenght is between including 3 and 8
}

Assignment

Besides making it ask for lastname and firstname make it also ask for your adress, city and phonenumber. Also create an integer error that tracks how many things are filled in wrong. If there were no errors print out that everything was filled out succesfully.

Download

The examples and the solutions to the Assignment you can download here