1 2 Previous Next 23 Replies Latest reply: Feb 28, 2008 1:31 PM by jschellSomeoneStoleMyAlias RSS

    Variable doesn't seem to use proper String

    807601
      I am working on a project for class (intro to java) and I have some problems with my code, the out put is displaying the correct child count, but it is not actually pulling the names from my second class as it is with the first name Fred1. The others are set already but for some reason I can not get it to pull the String cname for variables Child2, Child3, Child4, Child5.
      public class Family
      {
      // Private Strings
                  private String familyNames;
                  private String Partner;
                  private String Breakup;
                  private String Child1;
                  private String Child2;
                  private String Child3;
                  private String Child4;
                  private String Child5;
                  private int numChild;
                  
           public Family()
           {
               // Values for variables set to none or 0
                  familyNames = "None";
                  Partner = "None";
                  Child1 = "None";
                  Child2 = "None";
                  Child3 = "None";
                  Child4 = "None";
                  Child5 = "None";
                  numChild = 0;
              }
              
           public void familyNames()
              {
                  // Checks for Partner
                  if (Partner == "None")
                      System.out.println("No Partner");
                      else {
                      System.out.println("Partner: " + Partner);}
                      
                      // Checks for children, doesn't properly show child's names after First child.
                  if (numChild == 0)
                  System.out.println("No Children");
                  if (numChild >= 1)
                  System.out.println("First Child: " + Child1);
                  if (numChild >= 2)
                  System.out.println("Second Child: " + Child2);
                  if (numChild >= 3)
                  System.out.println("Third Child: " + Child3);
                  if (numChild >= 4)
                  System.out.println("Forth Child: " + Child4);
                  if (numChild >= 5)
                  System.out.println("Fifth Child: " + Child5);
                  System.out.println(numChild); // test purpose to show 5 kids
              }
              
              
           public void newPartner(String pname) // Method 2
           {
               Partner = pname;
              }
           public void newChild(String cname) // Method 3
           {
               // Adds +1 to numChild when a new child is found
               if (numChild == 0) 
               Child1 = cname;
               numChild ++;
               if (numChild == 1)
               Child2 = cname;
               numChild ++;
               if (numChild == 2)
               Child3 = cname;
               numChild ++;
               if (numChild == 3)
               Child4 = cname;
               numChild ++;
               if (numChild == 4)
               Child5 = cname;
               numChild ++;
              }  
      }
      is my code within newChild(String cname) or familyNames() wrong? if anyone can help me find my problem that would be great. Thanks in Advance!
        • 1. Re: Variable doesn't seem to use proper String
          807601
          You haven't made clear what your problem is, and you don't show all your code. However, one thing that's definitely wrong is that when comparing objects' states, including Strings' contents, you should use equals(), not ==.
          if (partner.equals("None"))
          • 2. Re: Variable doesn't seem to use proper String
            jschellSomeoneStoleMyAlias
            newChild(String cname)
            You realize that that method sets ALL of the variables to the same value when you call it the first time?

            And the second time you call it it will do nothing?
            • 3. Re: Variable doesn't seem to use proper String
              807601
              Okay I was affraid of that, I was hoping that it was setup more like Child1 string cname = Fred1, Child2 string cname = Jim2 and so forth, how should i go about making it assign cname different values for each variable?
              • 4. Re: Variable doesn't seem to use proper String
                807601
                if (condition)
                stuff
                more stuff
                still more stuff
                other stuff
                Tell me, how is Java to know which stuff gets executed only if the condition is true, and which stuff is always executed?
                • 5. Re: Variable doesn't seem to use proper String
                  807601
                  You might also want to look into arrays or collections.

                  http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
                  http://java.sun.com/docs/books/tutorial/collections/
                  • 6. Re: Variable doesn't seem to use proper String
                    807601
                    Sloppy programming I suppose... : (

                    I should change then to else if I am not correct.

                    However for some reason the code is actually counting the number of children found in my driver class i have setup
                    public class RelationshipDriver
                    {
                         public static void main(String[] args)
                            {       
                                String familyNames = "None";
                                String Partner = "None";
                                int Breakup = 0;
                                String Child1 = "None";
                                String Child2 = "None";
                                String Child3 = "None";
                                String Child4 = "None";
                                String Child5 = "None";
                                int numChild = 0;
                                
                                Family alice = new Family();
                                alice.familyNames();
                                System.out.println("");
                                alice.newPartner("John");
                                alice.newChild("Fred1");
                                System.out.println("");
                                alice.newPartner("John");
                                alice.newChild("Fred1");
                                
                                alice.familyNames();
                                
                                alice.newChild("Jim2"); // Doesnt properly Display name
                                alice.newChild("Sam3"); // Doesnt properly Display name
                                alice.newChild("bob4"); // Doesnt properly Display name
                                alice.newChild("Bob5"); // Doesnt properly Display name
                             }     
                    }
                    My problem is this is my output

                    No Partner
                    No Children

                    Partner: John
                    First Child: Jeff1
                    Second Child: Jeff1
                    Third Child: Jeff1
                    Forth Child: Jeff1
                    Fifth Child: Jeff1

                    It detects 5 children within the driver class, but the cname thing is where it's getting me. I been playing with it for two hours and not really sure where i need to go. Do i need to make a new module for each variable child1-child5? ex new cname1 cname2 ect.
                    • 7. Re: Variable doesn't seem to use proper String
                      807601
                      vanilla_lorax wrote:
                      if (condition)
                      stuff
                      more stuff
                      still more stuff
                      other stuff
                      Tell me, how is Java to know which stuff gets executed only if the condition is true, and which stuff is always executed?
                      You didn't answer my question.

                      http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html

                      Edited by: vanilla_lorax on Feb 27, 2008 4:49 PM
                      • 8. Re: Variable doesn't seem to use proper String
                        807601
                        This was my original code:
                        public class Family
                        {
                                    private String familyNames;
                                    private String Partner;
                                    private String Child1;
                                    private String Child2;
                                    private int numChild;
                                    
                             public Family()
                             {
                                    familyNames = "None";
                                    Partner = "None";
                                    Child1 = "None";
                                    Child2 = "None";
                                    numChild = 0;
                                }
                                
                             public void familyNames()
                                {
                                    
                                    if (Partner == "None")
                                        System.out.println("No Partner");
                                        else {
                                        System.out.println("Partner: " + Partner);}
                                    if (numChild == 0)
                                    System.out.println("No Children");
                                    if (numChild >= 1)
                                    System.out.println("First Child: " + Child1);
                                    if (numChild == 2)
                                    System.out.println("Second Child: " + Child2);
                                }
                                
                                
                             public void newPartner(String pname) // Method 2
                             {
                                 Partner = pname;
                                }
                             
                             public void newChild(String cname) // Method 3
                             {
                                 if (numChild == 0) 
                                 Child1 = cname;
                                 if (numChild == 1)
                                 Child2 = cname;
                                 numChild ++;
                                }
                                
                                
                                
                                
                        }
                        ]

                        And it displayed correctly Child1 and Child2's names as Fred and Jim.
                        • 9. Re: Variable doesn't seem to use proper String
                          807601
                          To reiterate...
                          vanilla_lorax wrote:
                          vanilla_lorax wrote:
                          if (condition)
                          stuff
                          more stuff
                          still more stuff
                          other stuff
                          Tell me, how is Java to know which stuff gets executed only if the condition is true, and which stuff is always executed?
                          You didn't answer my question.

                          http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html

                          Edited by: vanilla_lorax on Feb 27, 2008 4:49 PM
                          • 10. Re: Variable doesn't seem to use proper String
                            807601
                            The bad coding was my answer i thought, So then inside each If { } should I have an else statement?
                            • 11. Re: Variable doesn't seem to use proper String
                              807601
                              Sorry to be rude but obviously I completely do not understand, I been programming in java for about a week now. Not very familiar with it. please shed some useful light on what you mean. Reposting what you said is unfortunately not pounding it into my head that I am unsure of what your talking about.
                              • 12. Re: Variable doesn't seem to use proper String
                                jschellSomeoneStoleMyAlias
                                Sorry to be rude but obviously I completely do not understand...
                                Well that isn't terribly rude.

                                Look at the following code and notice what is different compared to your code.
                                   if (numChild == 0) 
                                         {
                                         Child1 = cname;
                                         numChild ++;
                                         }
                                • 13. Re: Variable doesn't seem to use proper String
                                  807601
                                  Thank you, i see I am missing the brackets.
                                  • 14. Re: Variable doesn't seem to use proper String
                                    807601
                                    midnighte wrote:
                                    Thank you, i see I am missing the brackets.
                                    Bingo!
                                    1 2 Previous Next