DEV Community

Cover image for JAVA Basics #10 - User Inputs
Chathumi Kumarapeli
Chathumi Kumarapeli

Posted on • Updated on

JAVA Basics #10 - User Inputs

In this article we are going to learn about how to read user inputs in java.

Read user inputs

To read user inputs first you need to import java.util.Scanner. Here scanner is a predefined java class. Let's see how we can read inputs from the terminal.

package com.company;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.print("Age: "); // line 01
        Scanner inputAge = new Scanner(System.in); // line 02
    }
}
Enter fullscreen mode Exit fullscreen mode

Line 01 prints 'Age: ' in the terminal for the user to input his age. Line 02 has created an object named inputAge which belongs to Scanner class. You can see that I have used new to create the object. Then within the parenthesis I have entered System.in. This is to get inputs into the system. Think it in the same way as System.out. So line 02 reads the user input (age that user enters) and assigns it to inputAge.
Now try to print inoutAge. If you run the following command,

System.out.println(inputAge);
Enter fullscreen mode Exit fullscreen mode

you will see that the program does not print the inputted age, but a long sentence. So how can you print the age? For this you have to use nextByte() predefined method from Scanner class. It scans the next token of the input as a byte. Check the following code.

package com.company;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.print("Age: "); // line 01
        Scanner inputAge = new Scanner(System.in); // line 02
        byte age = inputAge.nextByte(); // line 03
        System.out.println("You are " + age); // line 04
    }
}
Enter fullscreen mode Exit fullscreen mode

Here in line 03 we have declared a new variable age which is of type byte. Then we have applied nextByte() method to the inputAge. Then when you print the age in line 04 you can see the output as expected. Also in line 04 'implicit casting' takes place and age is converted into a string by java itself.

Try to enter a number like 10.5 (with decimal digits) and check whether the code will run smoothly.
No right? You will definitely get an error. This is because we have declared age as a byte type variable. So it does not accept any floating point numbers. For that there is another predefined method nextFloat() in the Scanner class. By using it you can take numbers with floating points as user inputs. Go through the code given below.

package com.company;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.print("Height in meters: ");
        Scanner inputHeight = new Scanner(System.in);
        float height = inputHeight.nextFloat();
        System.out.println("You are " +  height + "m tall.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Now let's see how can we ask the user to input a string and then to print that string in the terminal. Look at the code given below.

package com.company;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        System.out.print("Enter your name: "); // line 01
        Scanner name = new Scanner(System.in); // line 02
        String userName = name.next(); // line 03
        System.out.println("Hello " + userName); // line 04
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, in line 01 we are asking the user to input his name. Probably he will enter his full name (which means more than one word). Let's assume that he has entered as "Edward Cullen". This name is read by line 02. Now we have to convert this Scanner object name into a string. For that here I have used the predefined method next(). However, when the line 04 prints the output you will only see 'Hello Edward'. Why is that? That is because the next() method reads only one token. Which means it does not read the whole line. If you want to read the full line use method nextLine() as shown in the below code.

package com.company;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner inputName = new Scanner(System.in);
        System.out.print("Full Name: ");
        String fullName = inputName.nextLine().trim();
        System.out.println("Oh hey " + fullName);
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, nextLine() method reads the full name and assign it to the string fullName. We have used trim() method just to eliminate any unnecessary white spaces that might be there in the user input. Using trim() is not mandatory. If you input name as ' Jacob Black ', you will get the output as 'Oh hey Jacob Black' without any unnecessary spaces.

Task

Ask the user to enter his name (empName) and salary (salary). Then add a 20,000 bonus to his salary (salWithBonus) and print the line "Hi your total salary with bonus is ".
Ex:
Input-:
Full Name: Charlie Swan
Basic Salary: 100000
Expected output-:
Hi Charlie Swan your total salary with bonus is 120000.0

With that we can wrap up this article. Head to the next article to learn more about java :P

Top comments (0)