0

I'm working on a Guessing Game that will uses arrays to store both the names of all the players and their guesses. I'm fairly new to arrays, so my plan to get user input into the array was to get them to enter the amount of people playing, set that up as a variable and then use a loop to keep asking for names until I reached the necessary amount of names for the stated number of players. However, I am running into what probably is a very simple problem with the loop. Here's a small bit of my code thus far:

public class GuessGame {
int w = 0;
int[] Players = new int[100];
String[] PlayerNames = new String[100];
String numStart = JOptionPane.showInputDialog("How many players?");
int j = Integer.parseInt(numStart);
while (w <= j)
{
    String Name = JOptionPane.showInputDialog("What is your name?");
    PlayerNames[w] = Name;
    w++;
}

The problem is, I'm getting an error regarding the variables in my loop, w and j. The error statement says something to the effect of it cannot find the symbols for class w or class j. I don't intend for them to be classes, and I've run similar code in other projects without a hitch, so I really don't know what's going wrong here. I'm sure it's something stupidly simple, but *'ve been stuck at this wall for some time now and can't really progress until I get this sorted. This is part of a project with three separate classes. The class posted here, a Player class, and a Tester class, which is my main method. I had the whole thing working in a more simplified form earlier, but now I need to adjust it for actual player input and the arrays. Regardless, the tester class is supposed to be my main class. I am using Netbeans if it matters. Thank you. Here are the other two classes for reference:

package GuessGame;


public class GameLauncher {
public static void main(String[] args) {
    GuessGame game = new GuessGame();
    game.startGame();
}
}

and

package GuessGame;
import java.util.Random;

public class Player {
int number = 0; //where guess goes
String name;
public void guess() {
    Random r = new Random();
    number = 1 + r.nextInt(21);
    System.out.println("I'm guessing " + number);

}

}
Kavik
  • 13
  • 4
  • post you error here.. – chinna_82 Apr 29 '15 at 04:25
  • The things is, this is part of a project with three separate classes. The class posted here, a Player class, and a Tester class, which is my main method. I had the whole thing working in a more simplified form earlier, but now I need to adjust it for actual player input and the arrays. Regardless, the tester class is supposed to be my main class. I'll update the original post with this info. – Kavik Apr 29 '15 at 04:29
  • Does code inside GuessGame class are inside method? Did you trim the content in copying? – Mohan Raj Apr 29 '15 at 06:24
  • There is more to the GuessGame class, but I doubt it's really relevant to the error I'm getting and I guess I am a bit hesitant to post everything here. Besides, the code following it is going to be altered very soon anyway. The portion of the code I've posted is the very beginning of the class. – Kavik Apr 30 '15 at 02:29

3 Answers3

1

All your code needs to be in a method. You cannot have anything except variable declarations at the class level. Move all this into a method, for example public static void main(String[] args) main method.

public class GuessGame {

public static void main (String[] args)
{    
    int w = 0;
    int[] Players = new int[100];
    String[] PlayerNames = new String[100];
    String numStart = JOptionPane.showInputDialog("How many players?");
    int j = Integer.parseInt(numStart);
    while (w <= j)
    {
        String Name = JOptionPane.showInputDialog("What is your name?");
        PlayerNames[w] = Name;
        w++;
    }
}
}
Kon
  • 10,702
  • 6
  • 41
  • 58
  • The thing is, this is part of a project with three separate classes. The class posted here, a Player class, and a Tester class, which is my main method. I had the whole thing working in a more simplified form earlier, but now I need to adjust it for actual player input and the arrays. Regardless, the tester class is supposed to be my main class. I've update the original post with this info – Kavik Apr 29 '15 at 05:02
1
public class GuessGame {
 public void getPlayerName()
  {
   int w = 0;
   int[] Players = new int[100];
   String[] PlayerNames = new String[100];
   String numStart = JOptionPane.showInputDialog("How many players?");
   int j = Integer.parseInt(numStart);
   while (w <= j)
    {
     String Name = JOptionPane.showInputDialog("What is your name?");
     PlayerNames[w] = Name;
     w++;
    }
   }
 public static void main (String[] args)
 {
  GuessGame gg = new GuessGame();
  g.getPlayerName();
  }}

You can put in the methos as well and execute in the main method. But, if you declaring any variable inside the method (local variable), the variable must be initialised. Refer here for more details.

Community
  • 1
  • 1
chinna_82
  • 6,353
  • 17
  • 79
  • 134
  • The thing is, this is part of a project with three separate classes. The class posted here, a Player class, and a Tester class, which is my main method. I had the whole thing working in a more simplified form earlier, but now I need to adjust it for actual player input and the arrays. Regardless, the tester class is supposed to be my main class. I've update the original post with this info. – Kavik Apr 29 '15 at 05:02
0
public class GuessGame 
{
    static int w = 0;
    int[] Players = new int[100];
    static String[] PlayerNames = new String[100];
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String numStart = JOptionPane.showInputDialog("How many players?");
        int j = Integer.parseInt(numStart);
        while (w <= j)
        {
            String Name = JOptionPane.showInputDialog("What is your name?");
            PlayerNames[w] = Name;
            w++;
        }
    }
}

Everything should be in a method except variables.Class is template for variables and methods !!

chinna_82
  • 6,353
  • 17
  • 79
  • 134
Priya
  • 1
  • 1
  • The thing is, this is part of a project with three separate classes. The class posted here, a Player class, and a Tester class, which is my main method. I had the whole thing working in a more simplified form earlier, but now I need to adjust it for actual player input and the arrays. Regardless, the tester class is supposed to be my main class. I've update the original post with this info. – Kavik Apr 29 '15 at 05:03