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 What is wrong with this code in java

+16 votes
asked Mar 16, 2025 by ZA (220 points)
closed 4 days ago by Admin
I want to get input from user. What is wrong with this code?

class Main
{
    public static void main(String[]args) {
    inPut = Input.nextLine();
    }
}
closed with the note: answered

7 Answers

+1 vote
answered Mar 17, 2025 by Peter Minarik (101,530 points)
edited Mar 20, 2025 by Peter Minarik

Try this instead:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        System.out.print("? ");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        System.out.println("You entered: " + input);
        scanner.close(); // Close the scanner after you do not need it anymore.
    }
}
0 votes
answered Mar 18, 2025 by Preetha (140 points)
scanner class is not initialised
0 votes
answered Mar 18, 2025 by Paul Bourget Sack Ngamgna (140 points)
  1. Input is not defined – You need to create a Scanner object to take user input.
  2. inPut is not declared – You must declare a variable to store user input.
  3. Scanner class is missing – You need to import java.util.Scanner; before using it.
  4. No print statement – You should display the input to verify it works correctly.
0 votes
answered Mar 18, 2025 by PoisonTouch (140 points)
import java.util.Scanner;

public class Main {

public static void main(String[] args)

  {

       Scanner scanner = new Scanner(System.in);

       String input = scanner.nextLine();

       System.out.print("Your Input is: " + input);

    scanner.close();

   }

}
0 votes
answered Mar 18, 2025 by Ashish Rajput (140 points)
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String inPut = input.nextLine();
        input.close();
    }
}
0 votes
answered May 8, 2025 by Aditya Gaikwad (140 points)

Ok got it . You have done the following mistakes :

1. You have not imported java.util package with Scanner class

2.You have to create an object of that Scanner class from java.util package

  • nextLine() is the method from java.util.Scanner

import java.util.Scanner; //1

class Main
{
    public static void main(String[]args) {

    Scanner Input = new Scanner(System.in); //2
    inPut = Input.nextLine();
    }
}

0 votes
answered 4 days ago by santhiya (140 points)
Did not intialize Scannner Input=new Scanner(System.in);
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...