-1

I'm a highschooler who's pretty new to Java. I'm struggling a bit with object oriented programming. I'm not sure how to take an input from a scanner and implement the input into my code. I'm sorry if my question is bad, I'm just not sure how to approach this problem because the textbook I'm using is too complicated.

This is my main file:

import java.io.*;
import java.util.*;

//for best results play in code screen
public class Main {
  public static void main(String[] args) {
    // calls on the gameStructure file automaticlly so the player wont have to do it
    // manually
    new GameStructure();

    Scanner reader = new Scanner(System.in);
    System.out.println("How long do you want your snake to be? For best results make it less then 7 :)?");
    public int rad = reader.nextInt();
  }
}

This is my second file called GamePanel.java

int bodyParts = parts.rad;

This is the line in which I try to call on the input of the scanner This is the error I'm getting:

    int bodyParts = parts.rad;
                       ^
  symbol:   variable rad
  location: variable parts of type Main
1 error
exit status 1
 ^C
 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    There are many free java tutorials on the internet. If your textbook is too complicated, I suggest starting with a simple inline course. – tgdavies Oct 24 '21 at 04:11
  • Does your code compile? Variable `rad` (in method `main` of class `Main`) is referred to as a [local variable](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html) since it is declared inside a method. You cannot use the word `public` when you declare a local variable. So this line of your code should **not** compile: `public int rad = reader.nextInt();` The [partial] error that you posted in your question is a [compiler error](https://stackoverflow.com/questions/3179504/java-when-is-it-a-compiler-error-and-when-is-it-a-runtime-exception). – Abra Oct 24 '21 at 11:10

1 Answers1

0

You need to pass it as a parameter to a method in another class, or put it as a static class attribute.

E_net4
  • 27,810
  • 13
  • 101
  • 139