Results 1 to 4 of 4
  1. #1
    thesinter is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default 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;
        }
    }
    And my Tester
    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("");
            }                                 
        }  
    }
    Thank you for helping!

    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.

  2. #2
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

    Default

    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.

  3. #3
    thesinter is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    justin1980 is offline Member
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 10:12 AM
  2. [SOLVED] printing address of current instance of object?
    By emceenugget in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 10:36 PM
  3. Problem in printing JTable values
    By shanssat in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2009, 09:15 AM
  4. Printing default Swing values
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:09 PM
  5. Printing ASCII values of characters
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •