Jump to content

  •  

Photo
- - - - -

Lesson 2: Variables and User Inputs

Java for Beginners

  • Please log in to reply
No replies to this topic

#1 Wilson18

Wilson18

    Administrator

  • root
  • 112 posts
  • LocationUniversity of Birmingham

Posted 28 October 2014 - 12:02 AM

Hi everyone and thank you for tuning in to Lesson 2. In this tutorial, I will be talking about variables and user inputs. As we have already talked about how to compile your code on various operating systems, I will not be covering it any further. If you have any issues then make sure to check out this tutorial. If that doesn’t work and you are still having issues then comment below and I shall do my best to assist you.

 

So, lets begin. As always, we will have to create the outline of our class.

public class Lesson2{
  public static void main(String[] args){

  }

}

Now that we have the outline of our class set up, we can start adding some code. The code we will be adding will out put a message to the user and then allow them to respond to us. To do this, we will have to import a Class called Scanner. The way we can do this is by adding the following line at the top of the file:

import java.util.Scanner;

Now that this is included, we can actually start to use it. We do this by creating an object, which we can use. This is in the format of:

ClassName variableName = new ClassName(parameters);

It is important to note however that not all classes require parameters.
 

The actual code that we will be using to use the Scanner class is

Scanner in = new Scanner(System.in);

Now that we have a Scanner, we can use this to read the response from a user and store it in a variable. If we are wanting to store some text then we can save this to a variable. To do this, we type

String str = in.nextLine();

So to see an example of this in action, we can ask a user their name and then send them a greetings.

System.out.print("What is your name? ");
//create a scanner
Scanner in = new Scanner(System.in);
//store the users response to a variable called name
String name = in.nextLine();
//greet the user
System.out.println("Hello " + name +", nice to meet you! ");

If we are wanting to read a number that the user has put, we can do so like this

int num = in.nextInt();

Now that we have some variables, we can actually start using them! For this example, we are going to write a program to greet the user and find out some information about them. We are interesting in finding their age because only people above 19 will be allowed to join this super secret club we have running. To do this, we will have to learn to use If statements. This will allow us to test if something is true or not. The standard format for this is

if(something==true){
    //do something
}else{
   //do something else
}

Just to let you know, the else statement is not mandatory! Only use it if you need an else condition.

 

From the above, you should be able to put something together which works. If you are curious about how I went about it then here is my code. Feel free to download the attached source code and run the code if you like!

import java.util.Scanner;
public class Lesson2{
  public static void main(String[] args){

      System.out.print("What is your name? ");
      Scanner in = new Scanner(System.in);
      String name = in.nextLine();

      System.out.println("Hello " + name +", nice to meet you! ");
      System.out.print("How old are you " +name+"? ");
      int age = in.nextInt();
      int minAge=19;
      if(age>minAge){
        System.out.println("Welcome to the super secret 20's only club!");
      }else{
        int yearsToJoin=minAge-age;
        System.out.println("You are to young... Why dont you try again in "+yearsToJoin+ "years time?");
      }
  }

}

If you have forgotten how to compile and run your file you can do so by navigating to your file by the terminal/cmd and running the following command

javac Lesson2.java
java Lesson2

Attached File  RunningTheProgram.png   116.08KB   0 downloads

Attached Files


List what you read?

Check me out on: YouTube, Twitter, Facebook, Google+






Also tagged with one or more of these keywords: Java for Beginners