-2

I'm trying to get a program together that reads integers that a user inputs. I've been reading about the scanner class which seems to be the most common way to do this in java. However when I copy+paste the examples given on sites like this one I get some kind of error that I have no idea how to fix. Which is frustrating because all the stuff posted is supposed to be completed code that shouldn't have problems!

An example of some code that's supposed to work:

import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] arguments){
        Scanner input = new Scanner(System.in);

        String username;
        double age;
        String gender;
        String marital_status;
        int telephone_number;

        // Allows a person to enter his/her name   
        Scanner one = new Scanner(System.in);
        System.out.println("Enter Name:" );  
        username = one.next();
        System.out.println("Name accepted " + username);

        // Allows a person to enter his/her age   
        Scanner two = new Scanner(System.in);
        System.out.println("Enter Age:" );  
        age = two.nextDouble();
        System.out.println("Age accepted " + age);

        // Allows a person to enter his/her gender  
        Scanner three = new Scanner(System.in);
        System.out.println("Enter Gender:" );  
        gender = three.next();
        System.out.println("Gender accepted " + gender);

        // Allows a person to enter his/her marital status
        Scanner four = new Scanner(System.in);
        System.out.println("Enter Marital status:" );  
        marital_status = four.next();
        System.out.println("Marital status accepted " + marital_status);

        // Allows a person to enter his/her telephone number
        Scanner five = new Scanner(System.in);
        System.out.println("Enter Telephone number:" );  
        telephone_number = five.nextInt();
        System.out.println("Telephone number accepted " + telephone_number);
    }
}

Instead of the program running, it gives me two errors.

On the the line public class ScannerDemo { it gives me this error:

Illegal modifier for the local class ScannerDemo; only abstract or final is permitted

On the next line public static void main(String[] arguments){ I get this error:

The method main can not be declared static; static methods can only be declared in a static or top level type.

I have tried this with many different forms of scanners that are supposed to be all ready to go and get errors every time. What am I doing wrong? How can I fix it?

I am using Processing 3.

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346

3 Answers3

0

Please understand the difference between Java and Processing. Processing is its own language and editor with its own syntax rules, and you can't just copy-paste random Java code and expect it to work. You have to understand how Processing works, and then add code in a way that works in Processing.

Assuming you're using the Processing editor, then your main sketch file should not contain just a class, and it definitely shouldn't contain a main() method. It should contain a setup() and draw() function instead.

If you really want to use a class, then get rid of your main() method, encapsulate your logic in a function inside your class, and then add a setup() or draw() function that uses the class.

Or better yet, stop using a class and just use Scanner in your Processing code.

If you still can't get it working, please post a MCVE in a new question post, and we'll go from there. Good luck.

Kevin Workman
  • 39,413
  • 8
  • 61
  • 94
-1

I believe as @Hovercraft has mentioned you just need this code in a file called ScannerDemo.java, Were guessing you have it in different file name.

RedKlouds
  • 79
  • 2
  • 5
  • I am using Processing 3 (added that to the post) and it gives its files a .pde tag. It will not let you give the files a different tag. Naming it ScannerDemo.pde gave me this error: The class "ScannerDemo cannot have the same name as your sketch or its enclosing class". – THE FACT THAT Dec 02 '17 at 22:21
-1

Your class name and file name must be same. This is decision to first error.

public static void main(String[] arguments){

isn`t working because first error.

Andrik007
  • 57
  • 6