ULOG: Java Programming Basics 10 - Scanner Class SolutionsteemCreated with Sketch.

in #ulog6 years ago (edited)

Lecture 10: Scanner Class Solution


Scanner coding exercise

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");
    }
}

Scanner exercise solution code

Scanner scanner1 = new Scanner(System.in);
double height = scanner1.nextDouble();

Scanner exercise output

What is your height? 1,65 
You are 1.65 m

L10.png

Hi there, This is Marius from AlefTav Coding. Today, I will look at the Scanner class coding exercise. Your homework was to Implement a ‘Scanner’ class data type, ‘scanner1’, so that Java asks for your height in metres

import java.util.Scanner;
Scanner scanner1 = new Scanner(System.in);

I want my program to read information typed in at my keyboard. Therefore I have to declare a Scanner object with the name, scanner1. This scanner1 holds a reference to a new instance of Scanner. Also, Java needs the Scanner import, which is the statement: import java.util.Scanner

double height = scanner1.nextDouble();

The information read in is a decimal number. Hence, my declaration of data type, double, which is named, height. I take the value scanned in, and make a call to the method, nextDouble(). The value returned by nextDouble() is then stored at the location, height.

Complete solution

import java.util.Scanner;
public class Lecture10 {
    public static void main(String[] args) {
        System.out.print(“What is your height? ”);
        Scanner scanner1 = new Scanner(System.in);
        double height = scanner1.nextDouble();
        System.out.print("You are " + height + " m");
    }
}

I have a class, here, Lecture10 with a main() method. Then I do a print statement, asking the user to enter their height as a decimal value. This is followed by an implementation of the Scanner class, named, scanner1. Thereafter, I have an implementation of the primitive data type, double, named, height.

System.out.print("You are " + height + " m");

Next, I confirm that the details typed in by the user is correct by doing the printout, System.out.print("You are " + height + " m"); The first part of the String is the text, You are. Then, I add the user's height with the plus (+) symbol. I also add the string, m, which is the shortened form of metres.

What is your height? 1,65 
You are 1.65 m

With the code being correct, the program is executed. The output is the question being asked, What is your height?. I provide an answer, 1,65. Java then responds with the string, You are 1.65 m.

In this lecture you learnt the solution to the Scanner coding exercise. Next time I will show you how to implement the conditional statement known as the Ternary operator.

I am Marius, from AlefTav Coding. Till the next time, KEEP CODING.


Join me on Trybe.one (tokenized information-sharing platform) to earn 100 tokens for signing up and 10 tokens for logging in daily:

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

Coin Marketplace

STEEM 0.30
TRX 0.11
JST 0.033
BTC 63968.82
ETH 3136.80
USDT 1.00
SBD 4.28