Results 1 to 12 of 12
Like Tree4Likes
  • 1 Post By PhHein
  • 1 Post By PhHein
  • 1 Post By PhHein
  • 1 Post By XNOViiCE

Thread: Getting data across classes question

  1. #1
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Getting data across classes question

    Hello all! I have been having a lot of trouble with this. Is there any way to get the input from ProcessInput() to add(Golfer g)? Thanks in advanced!

    Java Code:
    import java.util.Scanner;
    
    public class Prog2 
    {
       static String cmd;
       static String name;
       static int score;
       static GolfLeague dm;
       static Golfer g;
       static Scanner stdin;
       public static void main( String [] args )
       {       
          cmd = stdin.next();
          while (!cmd.equals("Exit"))
          {
             ProcessInput(); 
             cmd = stdin.next();
          }
       }
       static private void ProcessInput()
       {
          boolean check;
          if(cmd.equals("Add"))
          {
             name = stdin.next();
             score = stdin.nextInt();
             check = dm.add(g);
             if(check = true)
                System.out.println("Golfer " + name + " was added." );
             else
                System.out.println("Golfer " + name + " was not added.");
          }
    ...
    
    
    
    public class GolfLeague 
    {
       static private final int MAX_SIZE = 5;
       private Golfer[] list = new Golfer[MAX_SIZE];
       private int numplayers = 0;
       
       public boolean add( Golfer g )
       {
          String name;
          int score;  
          if(numplayers == 0 || numplayers != MAX_SIZE && find(g.getName()) == -1)
          {
             list[numplayers] = new Golfer(name,score);
             numplayers++;
             return true;
          }
          return false;
       }
       ...

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Getting data across classes question

    If they are both in the same package, simply reference those variables directly, otherwise add "getter" methods.

    Edit: But really, should not the name and score be part of the Golfer object, in the first place? Which, the "Golfer" object you're using, in what you've shown here, is null anyway.
    Last edited by masijade; 09-27-2013 at 09:09 AM.

  3. #3
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Getting data across classes question

    Thanks for your reply! I can't make a getter method (that type of homework where they up the difficulty) and the same for setting up "Prog2 p" in GolfLeague because I can't set up any more class variables as well. :( What do you mean it should be part of the Golfer object? I am afraid I don't understand what you mean. Also won't the Golfer object be fine when the data is added to it or do I have that all wrong?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Getting data across classes question

    You already have all the information you need, you just ignore it
    Java Code:
     public boolean add( Golfer g ) // <--------------------------------------- g has all you need
       {
          String name;  // <--------------------------------------- uninitialized variable
          int score;  // <--------------------------------------- uninitialized variable
          if(numplayers == 0 || numplayers != MAX_SIZE && find(g.getName()) == -1)
          {
             list[numplayers] = new Golfer(name,score);  // <-------------------------------------- new Golfer without any meaningful values
             numplayers++; 
             return true;
          }
    XNOViiCE likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Getting data across classes question

    If I initialize a scanner in this method and then someone types a command line such as "Add Joe 68" would the program work correctly?

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Getting data across classes question

    What happens if you try? You cannot break Java and your PC won't blow up.
    XNOViiCE likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Getting data across classes question

    Yeah true I changed where the inputs go and it just takes the "Add" part of the line. When it goes to the add method I initialize a new scanner and it needs the name and score again.


    New and Improved non crashing code! (For the null references at least)
    Java Code:
    import java.util.Scanner;
    
    public class Prog2 
    {
       static String cmd;
       static String name = "name";
       static int score = 0;
       static GolfLeague dm = new GolfLeague();
       static Golfer g = new Golfer(name, score);
       static Scanner stdin = new Scanner(System.in);
       public static void main( String [] args )
       {       
          cmd = stdin.next();
          while (!cmd.equals("Exit"))
          {
             ProcessInput(); 
             cmd = stdin.next();
          }
       }
       static private void ProcessInput()
       {
          boolean check;
          if(cmd.equals("Add"))
          {
             check = dm.add(g);
             if(check = true)
                System.out.println("Golfer " + g.getName() + " was added." );
             else
                System.out.print("Golfer " + g.getName() + " was not added.");
          }
    ...
    
    import java.util.Scanner;
    public class GolfLeague 
    {
       static private final int MAX_SIZE = 5;
       private Golfer[] list = new Golfer[MAX_SIZE];
       private int numplayers = 0;
       
       public boolean add( Golfer g )
       {
          String name;
          int score; 
          Scanner stdin = new Scanner(System.in);
             name = stdin.next();
             score = stdin.nextInt();  
          if(numplayers == 0 || numplayers != MAX_SIZE && find(g.getName()) == -1)
          {
             list[numplayers] = new Golfer(name,score);
             numplayers++;
             return true;
          }
          return false;
       }
    ...
    Last edited by XNOViiCE; 09-27-2013 at 10:32 AM.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Getting data across classes question

    What happens if you try it this way:
    Java Code:
    public boolean add( Golfer g )   {
           
          if(numplayers == 0 || numplayers != MAX_SIZE && find(g.getName()) == -1)
          {
             list[numplayers] = g;
             numplayers++;
             return true;
          }
          return false;
       }
    XNOViiCE likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Getting data across classes question

    That way specifically didn't work but I think I have a few good ideas on what to do now! Thanks for all your help!
    gimbal2 likes this.

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Getting data across classes question

    I just want to say that you use if (check = true){...} in your code. That is wrong.

  11. #11
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Getting data across classes question

    Cause it has to be
    Java Code:
    if (check == true)
    . Right?

  12. #12
    XNOViiCE's Avatar
    XNOViiCE is offline Member
    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Default Re: Getting data across classes question

    Just for reference for anyone that needs it ever!
    Java Code:
    import java.util.Scanner;
    
    public class Prog2 
    {
       static String cmd;
       static String name;
       static int score;
       static Golfer g;
       static GolfLeague dm = new GolfLeague();
       static Scanner stdin = new Scanner(System.in); 
       public static void main( String [] args )
       {       
          cmd = stdin.next();
          while (!cmd.equals("Exit"))
          {
             ProcessInput(); 
             cmd = stdin.next();
          }
          System.out.println("Normal termination of Prog2.");
       }
       static private void ProcessInput()
       {
          boolean check;
          if(cmd.equals("Add"))
          {
             name = stdin.next();
             score = stdin.nextInt();
             check = dm.add(new Golfer(name, score));
             if(check == true)
                System.out.println("Golfer " + name + " was added." );
             else
                System.out.println("Golfer " + name + " was not added.");
          }
    ...
    
    
    public class GolfLeague 
    {
       static private final int MAX_SIZE = 5;
       private Golfer[] list = new Golfer[MAX_SIZE];
       private int numplayers = 0;
       
       public boolean add( Golfer g ) //This method was possible with the help of PhHein!
       { 
          if(numplayers == 0 || numplayers != MAX_SIZE && find(g.getName()) == -1)
          {
             list[numplayers] = g;
             numplayers++;
             return true;
          }
          return false;
       }
    ...

Similar Threads

  1. variables/data not in classes?
    By mike1127 in forum New To Java
    Replies: 6
    Last Post: 03-15-2013, 09:52 AM
  2. Data Between Classes
    By GrumpyBum in forum New To Java
    Replies: 3
    Last Post: 04-21-2012, 12:38 PM
  3. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 01:39 AM
  4. Passing data between 3 classes
    By tocakt in forum New To Java
    Replies: 3
    Last Post: 04-11-2011, 10:50 PM
  5. Need help passing data between classes
    By bri1547 in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 04:19 AM

Posting Permissions

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