Pregunta

I'm making a class called Sphere and I want to prompt the user to input the Diameter and I keep getting this error in Eclipse for the part of code that is asking for input.

Error:

Multiple Markers at this line

  • Syntax error on token(s), misplaced constructs(s)

  • Syntax error on token ""Please enter diameter"", delete this token

My code so far:

import java.util.Scanner;
import java.lang.Math;
public class Sphere {
public static void main(String[] args) {
}
    public int diam;
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter diameter"); //error on this line

}
¿Fue útil?

Solución

Remove the extra } from your code:

public class Sphere {
    public static void main(String[] args) {
       // } //Remove this
       // public int diam; // get rid of the public modifier here
       int diam;
       Scanner input = new Scanner(System.in);
       System.out.println("Please enter diameter"); //error on this line

    }  // add curly brace here
 } 

Also you cannot declare variable as public inside a method. public int diam; is an illegal statement. Remove that public modifier.

Otros consejos

Your error is that you're code is not inside of the main method as you think it might be. Put it in the main method, and get rid of the public modifier on int.

Please understand that you can declare variables outside of methods and constructors, but you can't call methods that don't do an assignment, here your System.out.println(...) call. But having said that, it really doesn't make sense to declare any of those lines outside of the main method, so put them inside of it, get rid of the erroneous public modifier, and your code should be happy.

You have a } that closes your main method before your code.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top