Results 1 to 4 of 4
- 01-20-2010, 05:41 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Printing values from object in Array?
I am trying to create a program that lets users ENTER and store high scores, along with the players name, and game they achieved a high score on. I believe that I am getting close to finishing this project, but I am having a problem printing values from the objects in my array. It looks as if I am only printing references to my objects. I have searched online for proper ways of fixing my mistake, but none of the pages I have pulled up have helped me.
As always, I'm not asking for anyone to do my project for me, just to point me in the right direction or help me answer my own question. Please feel free to critique my code, it can only benefit me.
Java Code:public class Scores { // instance variables - replace the example below with your own private int score; private String game; private String name; /** * CONSTRUCTOR * This is my Scores object */ public Scores(String game, String name, int score) { this.game=game; this.name=name; this.score=score; } /** * METHODS * I was told that I could use accessors to print values * I'm not sure how to return specific values of my object in array */ public int getScore() { return score; } public String getGame() { return game; } public String getName() { return name; } }
Java Code:import java.util.Scanner; import java.util.Arrays; public class arrayTester { public static void main(String[] args) { Scanner in=new Scanner(System.in); //create array Scores[] topGames = new Scores[3]; //menu loop int loop=0; while(loop!=-1) { System.out.println("Commands: E to enter scores. V to view scores. Q to quit."); String option=in.next(); System.out.println(""); //Ask user to enter values for my Scores object, and place object in array. if(option.equalsIgnoreCase("E")) { for(int i=0; i<topGames.length;i++) { System.out.println("Game title:"); String g=in.next(); System.out.println("Player name:"); String p=in.next(); System.out.println("Score:"); int s=in.nextInt(); topGames[i]=new Scores(g, p, s); } } //Print values from my objects in the array. else if(option.equalsIgnoreCase("V")) { for(int i=0;i<topGames.length;i++) { System.out.println(Arrays.toString(topGames)); //System.out.print(topGames[i].toString()); This didn't work either. System.out.print("\n"); } } //Exit program else if(option.equalsIgnoreCase("Q")) loop=-1; //Used if user misstypes command else System.out.println(""); } } }
EDIT: Woops, almost forgot to post my output
Commands: E to enter scores. V to view scores. Q to quit.
V
[null, null, null]
[null, null, null]
[null, null, null]
Commands: E to enter scores. V to view scores. Q to quit.
E
Game title:
StarCraft
Player name:
Jim
Score:
3
Game title:
Halo
Player name:
Jake
Score:
2
Game title:
Sonic
Player name:
Sully
Score:
1
Commands: E to enter scores. V to view scores. Q to quit.
v
[Scores@1018f51f, Scores@5684ce7a, Scores@290fd7f6]
[Scores@1018f51f, Scores@5684ce7a, Scores@290fd7f6]
[Scores@1018f51f, Scores@5684ce7a, Scores@290fd7f6]
Commands: E to enter scores. V to view scores. Q to quit.
q
Last edited by thesinter; 01-20-2010 at 05:48 AM.
- 01-20-2010, 06:05 AM #2
Member
- Join Date
- Jan 2010
- Location
- Wisconsin
- Posts
- 20
- Rep Power
- 0
Ok here is your problem ? I am not supposed to give you the answer or the moderators get mad
so here is a hint. Are you forgetting your getters?
what are you trying to print out here?
the score or the name??
//System.out.print(topGames[i].toString());Last edited by justin1980; 01-20-2010 at 06:07 AM.
- 01-20-2010, 06:17 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Thanks so much Justin! I was able to make the connection of what I needed to do with your comment. My program is finally working the way I need it too, but more importantly, you helped me get a better understanding of accessing arrays.
- 01-20-2010, 06:19 AM #4
Member
- Join Date
- Jan 2010
- Location
- Wisconsin
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 10:12 AM -
[SOLVED] printing address of current instance of object?
By emceenugget in forum New To JavaReplies: 1Last Post: 02-09-2009, 10:36 PM -
Problem in printing JTable values
By shanssat in forum AWT / SwingReplies: 3Last Post: 02-04-2009, 09:15 AM -
Printing default Swing values
By Java Tip in forum Java TipReplies: 0Last Post: 03-12-2008, 12:09 PM -
Printing ASCII values of characters
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 05:36 PM
Bookmarks