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

0 votes
answered Nov 10, 2023 by Aml (220 points)
Dear User,
    You can us a class named Scanner.Here is the example of Scanner class in code. For taking input from user you ought to take package using  import java.util.Scanner;  before difining class and than make a Scanner object using  Scanner sc = new Scanner(System.in);   after that after that difine the variable put its value sc.nextInt();  you can change Int by your variables data type.And remember that here S is capital in Scanner and System
import java.util.Scanner;
class takeinput
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int ans = (a + b);
        {
        System.out.println("Addition of " + a + " and " + b + " is " + ans);
        }
    }
}
I hope you under stand that how to take input in java.for further any query ask question.And keep exploring.
                                                                                  ~AML
0 votes
answered Nov 24, 2023 by Arpit Baranwal (150 points)

import java.util.*;

class Arpit

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

int n = sc.nextInt();           //for integer input

String str = sc.next();         //for One Word input

String sent = sc.nextLine();        //for a sentence input

float num = sc.nextFloat();        //for floating input

}

}

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.
...