ADVERTISEMENT
ADVERTISEMENT

Java program to get input from user

Using Scanner Class:

Scanner class is used to get input from the user, to use this class, you will have to import java.util.scanner package.

Get Input from User program in Java

Common functions to get input

MethodDescription
next() to get string without space from the user
nextLine() to get string with spaces from the user
nextInt() to get integer value from the user
nextFloat() to get float value from the user
nextByte() to get byte value from the user
ADVERTISEMENT

In this program we will read Name, Gender, Age and Float and then print all inputted values.

//program to input int, float and string value
import java.util.Scanner;
 
public class GetIpAddress
{
    public static void main(String args[])  throws Exception
    {
        String name, gender;
        int age;
        float weight;
         
        Scanner SC=new Scanner(System.in);
         
        System.out.print("Enter name: ");
        name= SC.nextLine();
         
        System.out.print("Enter Gender (Male/Female): ");
        gender=SC.next();
 
        System.out.print("Enter age: ");
        age=SC.nextInt();
 
        System.out.print("Enter weight: ");
        weight=SC.nextFloat();
         
        System.out.println("Name: " + name);
        System.out.println("Gender: " + gender);
        System.out.println("Age: " + age);
        System.out.println("Weight: " + weight);
                                  
    }
}

Output

    Enter name: Mike the Expert
    Enter Gender (Male/Female): Male
    Enter age: 21
    Enter weight: 65.50
    Name: Mike the Expert
    Gender: Male
    Age: 21
    Weight: 65.5

Core Java Example Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.