ULOG: Java Programming Basics 9 - Scanner Class Example

in #ulog2 years ago (edited)

Lecture 9: Scanner Class Example


Scanner class problem statement
Implement a ‘Scanner’ class data type, ‘input’, so that Java asks for your profession.

Scanner class solution code
Scanner input = new Scanner(System.in);
String job = input.nextLine();

Scanner class output
What is your profession? Java teacher
Your profession: Java teacher

L09.png

Hi there, I am Marius from AlefTav Coding. Today I will look at an example of letting your java program get user input. You will learn how to 'Implement a ‘Scanner’ class data type, ‘input’, so that Java asks for your profession.'

Lecture starting code

import java.util.Scanner;
public class Lecture9 {
    public static void main(String[] args) {
        System.out.print(“What is your profession? ”);
        // TODO: Implement 'Scanner' as variable name, 'input'
        // TODO: Implement 'String' as variable name, 'job'
        System.out.print(“Your profession: ” + job);
    }
}

Now, replacing the first TODO comment with the solution as follows:

Scanner input = new Scanner(System.in);

Java's Scanner class lets the user interact directly with the program. Here, I have a Scanner declaration. The name I have given it is input. Then, I create what is known as an object with the expression new Scanner. Next I initialize input to this new object. With this statement Java is able to accept information typed by the user on their keyboard.

Notice the word in. With Java output, the computer System prints something out. But with Java input, the computer System reads something in.

import java.util.Scanner;

To use the Scanner data type Java requires extra information. This information is obtained from the java.util sub-folder by means of an import statement that must appear before the start of the program.

String job = input.nextLine();

Java needs to store the information typed in by the user. The job information that the user provides is a piece of text. So, Java stores it as a String data type. And job is initialized with the line of text provided through the method, nextLine()

Complete solution

import java.util.Scanner;
public class Lecture9 {
    public static void main(String[] args) {
        System.out.print(“What is your profession? ”);
        Scanner input = new Scanner(System.in);
        String job = input.nextLine();
        System.out.print(“Your profession: ” + job);
    }
}

I hava a class, here, Lecture9 with the method, main(). Then, I print some text on the screen. This text is a question asked by Java. In this case, what my profession is. At this point, Java waits for the user to type in an answer to the question. In the next two lines I implement the Scanner class and the String class.

System.out.print(“Your profession: ” + job);

Finally, Java prints the information provided by the user. This is to confirm that Java correctly read in the information. This text is printed and then I add the profession name stored at the location, job.

What is your profession? Java teacher

After my code is complete and I run the program, Java is asking what my profession is. I reply with, Java teacher. And Java repeats these details below:

Your profession is Java teacher    

Scanner coding exercise

In this lecture you learnt how to implement the Scanner class. Next, you can go ahead to do this coding exercise, using this starter code:

import java.util.Scanner;
public class Lecture10 {
    public static void main(String[] args) {
        System.out.print(“What is your height in metres? ”);
        // TODO: Implement a 'Scanner' class data type, 'scanner1'
        // TODO: Implement the primitive 'double' as 'height'
        System.out.print(“You are " + height + " m");
    }
}

My name is Marius from AlefTav Coding. Till next time, KEEP CODING.

If you want to earn tokens on EVERY post you create use the referral URL below:

http://trybe.one/ref/2859/

This post was made from https://ulogs.org

Sort:  

This user is on the @buildawhale blacklist for one or more of the following reasons:

  • Spam
  • Plagiarism
  • Scam or Fraud