Tuesday 27 October 2015

Java | Reading Input From The Computer Console


I can see us progressing well. Can't you? Yes You Can. lol

When I say reading input from the computer console. What do I mean?

I mean trying to tell the computer to give me a chance to give it a value which it would take do some computations and give me the output. 
WOW! That sound's cool right? YES!

In order for a computer console to give you a chance to give it a value there is something you must really really know and that thing is called SCANNER.

Yes! Scanner

Do you still remember specific import I talked about in one of my previous posts?
If you don't please review that post. It would enhance your understanding of what we are about to do here a little bit, OK?

For us to send data to the computer there is something we must import into our program.and this Java statement is written right under the package statement OK?

This statement is:


import java.util.Scanner;

Once you have done that there is one more thing you need to do. This next thing you need to do has to do with Objects, Classes and Methods and we are not there yet.

Do not worry I will try and explain a little not to confuse you but you must have to have faith this time till we reach that topic, OK?

We are going to use a scanner class from our importation to create a new object that has a method to receive data from the computer.

Hmmm... I know you are confused but just keep reading.

After declaring the main method this is the next Java statement to write:

Scanner input = new Scanner(System.in);

Yes! I vividly remember that I have told you something about System.out in my previous posts.
So now you are seeing System.in which is a method used to enable us input data to the computer console and pass it on for computation.

So at this moment here is what the code looks like:

package nicholas;
import java.util.Scanner;
public class ReadInput{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
}
}

Now the next step is to tell the computer to display something that should prompt the user to enter a value.
This makes the code look like this:

package nicholas;
import java.util.Scanner;
public class ReadInput{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the value you want to display: ");
}
}

The next step is to get the value entered by the user and to do that you must know the type of datatype to be entered because that is what determines the method you will used to get the value.

For example if the value the user is to enter is an integer
You make use of the method - nextInt()
If the value the user is to enter is a floating point number
You make use of the method - nextDouble()
If the value the user is to enter is a string that ends before a whitespace character
You make use of the method - next()
If the value the user is to enter is a line of text
You make use of the method - nextLine()

How do I make use of this methods?

Remember this statement:
Scanner input = new Scanner(System.in);

Link the variable input to the method in any of these ways:

input.nextInt()
input.nextDouble()
input.next()
input.nextLine()

Now we are going to declare a variable and assign these method above to the variable appropriately.
Therefore, to get the value entered by the user we make use of any of these statements depending on the one needed:

double length = input.nextDouble();
int age = input.nextInt();
String intials = input.next();
String name = input.nextLine();

Integrating this statements into the code, It would be:

package nicholas;
import java.util.Scanner;
public class ReadInput{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Your Name: ");
String name = input.nextLine();
System.out.print("Enter Your Initials: ");
String initials = input.next();
System.out.print("Enter Your Current Age: ");
int age = input.nextInt();
System.out.print("Enter Your Present Height: ");
double height = input.nextDouble();
// Displaying The Values You Entered
System.out.println("Name: "+name+"");
System.out.println("Initials: "+initials+"");
System.out.println("Age: "+age+" years");
System.out.println("Height: "+height+" ft");
}
}

Here is the screenshot of the code:

Here is the output:

That's All About Reading Input From Your Computer Console For Now.

I HOPE YOU GOT THE GIST.

I would like us to try some exercises

1.   Write a program that prompts the user to enter four numbers and find the average of the four numbers.

Solution

package nicholas;
import java.util.Scanner;
public class ComputeAverage{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the four numbers: ");
double num1 = input.nextDouble();
double num2 = input.nextDouble();
double num3 = input.nextDouble();
double num4 = input.nextDouble();
double average = (num1 + num2 + num3 + num4) / 4;
System.out.println("The average of the four numbers is "+average+"");
}
}

Here is the screenshot of the code:

Here is the output:

2.   Write a program that prompts the user to enter his or her weight in kilograms and find the weight in pounds.
Hint: 1 Pound = 0.454Kilograms

Solution

package nicholas;
import java.util.Scanner;
public class KiloToPound{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Your Weight In Kilograms: ");
double kiloWt = input.nextDouble();
double poundWt = kiloWt / 0.454;
System.out.println("You are weighing "+poundWt+" pounds.");
}
}

Here is the screenshot of the code:

Here is the output:


Assignment:

Try to solve three problems that come to your mind that requires the user to enter one or more values.
I would be very pleased to see you comment on the problems you solved.
In other words, you are free to comment the three topics of the problems you solved.

Also, if you have any problem at all, please do well to comment.

Peace!!

1 comment:

  1. i wanted to say this but i dont know which post to drop this comment; if i run a program successfully following your instructions in the first class i created after installing the NETBEANS and want to run another in another class it gives me result of the previous program i ran then i have to go back to that first class and re-write the new program all over again........ WHY?

    ReplyDelete