Problems importing a scanner class

I am trying to execute the following code, but keep getting this error:

Error: the main method was not found in the ScannerDemo class, please define the main method as: public static void main(String[] args)

 import java.util.Scanner; class ScannerDemo public class Main { public static void main (String [] args) { Scanner sc = new Scanner(System.in); String userName; System.out.println("Enter a number"); username = sc.nextLine(); System.out.println("your number is" + username + "enter your next number"); username2 = sc.nextline(); System.out.println("your total is" + username2 ); } } 

I think I should import the Scanner class incorrectly, I tried different methods, but so far nothing has worked.

+4
source share
5 answers

You have two class declarations in the line above! This is not true. Your import is in order. Try:

 import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { ... } } 
+4
source

The definition of your class is incorrect. You are trying to define two classes: ScannerDemo and Main . Replace:

 class ScannerDemo public class Main 

Using only:

 public class ScannerDemo 

In addition, in your Main method, you must reference the variable userName instead of userName , and you do not define username2 . Please note that Java identifiers are case sensitive:

 public static void main (String [] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); String userName = sc.nextLine(); System.out.println("your number is" + username + "enter your next number"); String username2 = sc.nextline(); System.out.println("your total is" + username2 ); } 
+3
source
 import java.util.Scanner; public class ScannerDemo { public static void main (String [] args) { Scanner sc = new Scanner(System.in); String userName; System.out.println("Enter a number"); username = sc.nextLine(); System.out.println("your number is" + username + "enter your next number"); username2 = sc.nextline(); System.out.println("your total is" + username2 ); } } 
-1
source
  import java.util.Scanner; public class ScannerDemo { public static void main (String [] args) { Scanner sc = new Scanner(System.in); String userName; System.out.println("Enter a number"); int userName = sc.nextInt(); System.out.println("your number is " + userName); System.out.println("enter your next number"); int userName2 = sc.nextInt(); System.out.println("your total is " + (userName2 + userName)); } } 

Hope this helps

-1
source
 import java.util.Scanner; public class ScannerDemo { public static void main (String [] args) { Scanner sc = new Scanner(System.in); String userName; System.out.println("Enter a number"); int username = sc.nextInt(); System.out.println("your number is " + username); System.out.println("enter your next number"); int username2 = sc.nextInt(); System.out.println("your total is " + (username2 + username)); } } 
-3
source

Source: https://habr.com/ru/post/1434513/


All Articles