User Input in Java

  • In this chapter, you'll learn how to interact with your Java programs by taking user input. This allows your programs to be flexible and respond based on what the user provides.

  • Here's the basic syntax :
  • user_input =Scanner.nextLine()

    This code displays the prompt "Enter your name: " on the screen. The user types their name and presses Enter. The program then stores the entered name in the variable user_input.


  • Examples of User Input:
  • 1. Greeting the User:
    System.out.println("Enter yours name:");
    string name=sc.nextLine()
    System.out.println("Hello, "+ name +"!");

    This program asks the user for their name and then prints a personalized greeting.


    2. Getting Numbers from the User:
    age =sc.nextInt()
    System.out.println("You are"+age+"old");

    Here, the program asks for the user's age. However, input() always returns a string. We use int() to convert the entered string to an integer before storing it in the age variable.


  • Examples: Basic Problems & Solutions
  • Example-1Write a program to accept the marks of student in Physics, Chemistry and Biology. Display the total marks and average marks.

    Java code
    import java.util.*; class Student { public static void main(String args[]) { Scanner sc= new Scanner(System.in); int p,c,b,total; double avg; System.out.println("Enter the marks of student"); System.out.println("Physics Marks-"); p=sc.nextInt(); System.out.println("Chemistry Marks-"); c=sc.nextInt(); System.out.println("Biology Marks-"); b=sc.nextInt(); total=p+c+b; avg=total/3; System.out.println("Total Marks="+total); System.out.println("Average Marks="+avg); } }

    OUTPUT:

    Enter the marks of student
    Physics Marks-
    60
    Chemistry Marks-
    70
    Biology Marks-
    95
    Total Marks=225
    Average Marks=75.0

    Example-2Write a program to accept the number of days and display the result after converting into number of years, number of months and the remaining number of days.

    Java code
    import java.util.*; class days { public static void main(String args[]) { Scanner sc= new Scanner(System.in); int n,y,r,m,d; System.out.println("Enter the no of days:"); n=sc.nextInt(); y=n/365; r=n%365; m=r/30; d=r%30; System.out.println("Years "+y+" Months "+m+" Days "+d); } }

    OUTPUT:

    Enter the no of days:
    366
    Years 1 Months 0 Days 1

    Key Points to Remember:
  • input() always returns a string, even if the user enters a number.
  • You might need to convert the user input to a different data type (like integer or float) using functions like int(), float(), etc., depending on how you plan to use it in your program.
  • You can provide informative prompts within the input() function to guide the user on what kind of input is expected.

  • Practice Exercises:
  • 1. Write a program that asks the user for their favorite color and then prints a message like "Your favorite color is blue!".
  • 2. Write a program that asks the user for two numbers and then prints their sum.