KAKAK
KAKAK

Reputation: 899

Having problems with storing variable in classes

Please see ifmy logic is correct i do not know how to implement what i am trying to do. I am a beginner in java.

Game is a class which stores the Name and the Steps of that user after the game is complete.

[ First Method ]

private game[] gameArray = new game[10];

for(int i = 0; i <gameArray.length;i++){
         gameArray[i].setName(nameTxt.getText());
         gameArray[i].setpSteps(stepsCount);
    }

By Clicking History Button they will get the previous 10 people's name and steps.

JOptionPane.showMessageDialog(null,
       "Player's Name No. of Steps"+
       "\n"+gameArray[i].toString());

-The Problem:

1) This code limits to only previous 10 results is there anyway i can get all the previous results

2) This code is not working!

[ Second Method - This is also not working! ]

private game[] gameArray = new game[10];

// Storing the name of player[i]

gameArray[i].setName(nameTxt.getName());

// Storing the number of steps player[i] took.

gameArray[i].setpSteps(stepsCount);

//Displaying previous 10 players name and steps

JOptionPane.showMessageDialog(null,
       "Player's Name No. of Steps"+
       "\n"+gameArray[i].toString());

Upvotes: 0

Views: 73

Answers (1)

Jack
Jack

Reputation: 133567

Some points:

  • you can't directly convert an array to a String through toString() because it isn't supported in Java, you should use Arrays.toString(..)
  • Java arrays have static size, if you want to store the whole history you will need a dynamic data structure as an ArrayList<Game> or a LinkedList<Game>
  • name of classes should be capitalized (Game, not game)
  • without knowing the internals of your Game class it's impossible to tell what is not working and why it is not working, we don't even know what it is supposed to do exactly

Upvotes: 1

Related Questions