0

This program compiles correctly, but doesn't run when I attempt to input values for width and height, instead giving me the error message "Exception in thread "main" java.lang.IllegalArgumentException: width and height must be positive". How do I correctly declare static variables that I define using Scanner outside the main method? (beginner programmer, sorry if this is obvious!)

public class Animation {  
static int width;
static int height;
static double x0;
static double y0;

public static void main ( String[] args ) {
    getInputs();
    initStdDraw(width, height);
    drawFace(x0, y0);
}

public static void initStdDraw(int width, int height) {
    StdDraw.setCanvasSize(width, height);
    StdDraw.setXscale(0, width);
    StdDraw.setYscale(0, height);
    StdDraw.rectangle(width/2, height/2, width/2, height/2);
}

public static void getInputs() {
    Scanner console = new Scanner(System.in);    
    System.out.println("Please provide a canvas width and height: ");
    int width = console.nextInt();
    int height = console.nextInt();
    System.out.println("Please provide a starting position: ");
    double x0 = console.nextDouble();
    double y0 = console.nextDouble();
4
  • Please tell use the exact line where you get this error. Also please print the values of width/height. Commented Feb 28, 2017 at 8:48
  • Do you mean x0 = console.nextDouble(); instead of double x0 = console.nextDouble();? (Same for the other 3 variables) As in, are you intending to assign the static variable or to define a new, unrelated local variable? Commented Feb 28, 2017 at 8:49
  • Yes! This fixed my problem, thank you so much. Would you mind explaining why my previous version gave me an error? Commented Feb 28, 2017 at 8:52
  • There are no 'static variables defined inside a method' here. There can't be. They are not legal in Java. Unclear what you're asking. Commented Feb 28, 2017 at 9:18

1 Answer 1

1

You declare these fields:

static int width;
static int height;
static double x0;
static double y0;

But you declare these local variables with the same name :

int width = console.nextInt();
int height = console.nextInt();
System.out.println("Please provide a starting position: ");
double x0 = console.nextDouble();
double y0 = console.nextDouble();

So you don't assign the value to the fields in your method but to local variables.
These are two distinct variables and local variables shadow field variables with the same name as they have a priority scope in a method.

Besides local variables exist only during the getInputs() execution.

You should remove the local variables :

width = console.nextInt();
height = console.nextInt();
System.out.println("Please provide a starting position: ");
x0 = console.nextDouble();
y0 = console.nextDouble();
Sign up to request clarification or add additional context in comments.

Comments

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.