1 2 Previous Next 23 Replies Latest reply: Feb 28, 2008 1:31 PM by jschellSomeoneStoleMyAlias Go to original post RSS
      • 15. Re: Variable doesn't seem to use proper String
        807601
        In addition, in case it's not clear
        if (numChild == 0) 
        Child1 = cname;
        numChild ++;
        is equivalent to
        if (numChild == 0) 
        {
          Child1 = cname;
        }
        numChild ++;
        • 16. Re: Variable doesn't seem to use proper String
          807601
          Since that change my output is now

          Partner: John
          No Children
          Total Children: 0

          Doesn't count the children now, i changed it to the follow code
               public void newChild(String cname) // Method 3
               {
                   // Adds +1 to numChild when a new child is found
                   if (numChild == 0) {
                   Child1 = cname; }
                   else if (numChild == 1) {
                   Child2 = cname;
                   numChild ++; }
                   else if (numChild == 2) {
                   Child3 = cname;
                  numChild ++; }
                   else if (numChild == 3) {
                  Child4 = cname;
                   numChild ++; }
                   else if (numChild == 4) {
                   Child5 = cname;
                   numChild ++; }
                  } 
          How do i go about giving each variable a different cname String.

          changed the code to add numChild ++; now it's displaying correctly, ty. I am sure i will have more questions

          Code added
                  if (numChild == 0) {
                   Child1 = cname;
                   numChild ++;}
          Edited by: midnighte on Feb 27, 2008 5:19 PM
          • 17. Re: Variable doesn't seem to use proper String
            jschellSomeoneStoleMyAlias
            How do i go about giving each variable a different cname String.
            Did you call newChild() somewhere?
            • 18. Re: Variable doesn't seem to use proper String
              807601
              I just have it in the method newChild (String cname)

              that the only time it appears in the coding.
              • 19. Re: Variable doesn't seem to use proper String
                jschellSomeoneStoleMyAlias
                You have to write code that looks something like the following somewhere.

                newChild("First child");
                • 20. Re: Variable doesn't seem to use proper String
                  807601
                  Can you explain why, I am not really sure.
                  • 21. Re: Variable doesn't seem to use proper String
                    jschellSomeoneStoleMyAlias
                    A method defines what happens when you call it.

                    You created it but you haven't called it. Until you call it it doesn't do anything.
                    • 22. Re: Variable doesn't seem to use proper String
                      807601
                      isn't that what below does:
                           public void newChild(String cname) // Method 3
                           {
                               // Adds +1 to numChild when a new child is found
                               if (numChild == 0) {
                               Child1 = cname;
                               numChild ++;}
                               else if (numChild == 1) {
                               Child2 = cname;
                               numChild ++; }
                               else if (numChild == 2) {
                               Child3 = cname;
                              numChild ++; }
                               else if (numChild == 3) {
                              Child4 = cname;
                               numChild ++; }
                               else if (numChild == 4) {
                               Child5 = cname;
                               numChild ++; }
                               else if (numChild == 5) {
                               Child6 = cname;
                               numChild ++; }
                              }  
                      it pulls data from alice.newChild("name of child")

                      below:
                      public class RelationshipDriver
                      {
                           public static void main(String[] args)
                              {       
                                  String familyNames = "None";
                                  String Partner = "None";
                                  String Child1 = "None";
                                  String Child2 = "None";
                                  String Child3 = "None";
                                  String Child4 = "None";
                                  String Child5 = "None";
                                  String Child6 = "None";
                                  int numChild = 0;
                                  inr numPartner = 0;
                                  
                                  Family alice = new Family();
                                  alice.newPartner("John");
                                  alice.newPartner("Sexy Tim");
                                  alice.newChild("Fred1");
                                  alice.newChild("Jim2");
                                  alice.newChild("Sam3");
                                  alice.newChild("Bob4");
                                  alice.newChild("SR5");
                                  alice.newChild("lost one"); // 6th child
                      
                                  alice.familyNames();
                               }     
                      }
                      or is that not right?
                      • 23. Re: Variable doesn't seem to use proper String
                        jschellSomeoneStoleMyAlias
                        or is that not right?
                        Getting closer
                        void main(String[] args)
                        ....
                        int numChild = 0;
                        inr numPartner = 0;
                        Those variables don't do anything because they are local to the method named "main'.
                        alice.newChild("lost one"); // 6th child
                        What value does 'numChild' have after the above statement?
                        alice.familyNames();
                        What value does 'numChild' have when you call that? What happens in familyNames when 'numChild' has that value?
                        1 2 Previous Next