Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed how to take user input in java

+17 votes
asked Sep 3, 2023 by Mohit shakya (220 points)
closed Nov 27, 2023 by Admin
closed with the note: answered

12 Answers

+1 vote
answered Sep 4, 2023 by Peter Minarik (89,230 points)

Use the Scanner class as shown in this article.

+2 votes
answered Sep 7, 2023 by MD FAIZAN ANWAR (180 points)
edited Sep 8, 2023 by MD FAIZAN ANWAR
import java.util.*;

class Array

{

public static void main(String args[]){

    Scanner sc=new Scanner(System.in);

      int n =sc.nextInt();

     System.out.println(n);

}

}
+1 vote
answered Sep 16, 2023 by 218A1A05B8 NUSUM SASI PREETHAM (560 points)
we use Scanner class to take user input in java as shown below example

incase text files we use BufferReader class  to take input from user

import java.util.*;

class InputByUser {

    public static void main(String[] args) {

        Scanner sc =new Scanner(System.in);

        int a=sc.nextInt();//for integer

        double b=sc.nextDouble();//for floating values

      System.out.println(a);
    }

}
+1 vote
answered Sep 17, 2023 by SURAJ PRATAP SINGH (160 points)
//For take input from user

import.java.util.Scanner;

class Input

{

     public static void main(String args[])

        {

             Scanner obj = new Scanner(System.in)

                 int n;

                 n=obj.nextInt();

                  System.out.println("User Inputting Number is "+n);

       }

}
0 votes
answered Sep 26, 2023 by Shreyash Singh (140 points)
Scanner sc=new Scanner (System.in);

int a=sc.next Int();
0 votes
answered Sep 27, 2023 by Arun (140 points)
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter first number- ");
int a= sc.nextInt();
}
}
0 votes
answered Oct 15, 2023 by Ayush Tiwari (140 points)
import java.util.*;//use to take input

public class Main{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();//for integer value

Double b = sc.nextDouble();//for decimal value

Boolean c = sc.nextBoolean(); // for true false

char d = sc.nextChar(); // for character

String s = sc.nextString(); // for string

System.out.println( b);

System.out.println( a);

System.out.println( c);

System.out.println( d);

System.out.println( s);

}

}
0 votes
answered Oct 25, 2023 by Gulshan Negi (1,580 points)

Let's understand it with the help of a simple code example

import java.util.Scanner;

public class UserInputExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int number = scanner.nextInt();

        System.out.println("You entered: " + number);

        scanner.close();
    }
}

Well, in the above code example, it takes a number as input from the user and will display that number as output.

Thanks

0 votes
answered Nov 6, 2023 by Piyush Pandey (140 points)

The Scanner class is used to get user input, and it is found in the java.util package.

0 votes
answered Nov 8, 2023 by N SATHYANARAYANA (140 points)
java.util.Scanner obj=new java.util.Scanner(System.in);

int n=obj.nextInt();
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...