0

Can anyone please tell me what is wrong with my code, i was just trying to add scanner in java and it was showing me an error on package line

import java.util.Scanner;

package scaner;

public static void main(String[] args) {
    // TODO Auto-generated method stub
   Scanner in = new Scanner (System.in);
   System.out.println("enter your number");
   int number;
   number = in.nextInt();
   System.out.println("your number is" + " " + number);
}

}

2

1 Answer 1

1

First of all, the main method must be inside a class. Also, delete the "package scaner;" line. If you want, you can also simplify the code:

import java.util.Scanner;

public class className{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = sc.nextInt();
        System.out.println("Your number is: " + number);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Good! But let's also stick to naming convention, while we're at it. The class should be called ClassName, with a capiital "C".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.