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.

What is wrong with this code in java

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

class Main
{
    public static void main(String[]args) {
    inPut = Input.nextLine();
    }
}

5 Answers

+1 vote
answered Mar 17, 2025 by Peter Minarik (101,420 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();
    }
}
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.
...