Dear Stephen, I don't think you have understood variables clearly. Hope this helps:
when u say: name1="peter"; It stores the value 'peter' in the variable 'name1'. And, when u say: name2=name1; //notice that the word 'name1' isn't inside quotation marks. It doesn't store the value 'name1', but stores the value that the variable name1 contains, i.e. the value 'peter', into name2. So, in the program we are talking about, the variable 'longestname' contains the first name, in this case, 'peter'; not any variable that represents peter. (By the way, the actual code you sent first and the code I sent you do the same function; my code is not a correction to the one you sent; but, only a shorter way, as you requested.) Regards, Manu. On Sat, Sep 20, 2008 at 7:56 PM, Stephen Hunter <[EMAIL PROTECTED]>wrote: > Oh, I understand it completely. > So you are telling me that longestname holds the longest First Name; > Example: Peter. It holds Peter's name, not some variable that represents > Peter? > > That is what is confusing me. How you have gone through four or five > different variable assignments to get to the longestname piece in the code, > then just simply print longestname when I can't see how in the world it > contains an actual name. I believe when you run your code and the final > print statement is ran, it will print a variable name like name1 or > something. > > If I am wrong, please paste your code here so I can learn what you did and > learn something here. > http://paste.pocoo.org/ > > Thanks, > Stephen > > > > > > On Sat, Sep 20, 2008 at 3:32 AM, Manu? <[EMAIL PROTECTED]> wrote: > >> How does this below work? I know about the ?: comparison operator, I mean >> how did you get it to print the original name? See below: >> >> You most probably haven't understood the code: >> >> Before you go on to the part of the code, you'll have to split the name >> into first name and second name. >> Then, u'll have to assign the first names to the variables name1, name2 & >> name3. >> >> //now, the length of the first name is compared to the length of the >> second name. >> //the name that is the longest is stored in the variable 'longestname'. >> longestname=(lengthofname1> lengthofname2)?name1:name2; >> >> //now, the variable 'longestname' is compared to the third first name. >> //the longer of the two names is again stored in the variable >> 'longestname'. >> longestname=(longestname>lengthofname3)?longestname:name3; >> >> //Here, the content of the variable 'longestname' is printed. >> System.out.println(longestname + " has the longest first name!"); >> >> No looping is necessary! >> >> Regards, >> Manu. >> >> >> On Sat, Sep 20, 2008 at 5:19 AM, Stephen Hunter <[EMAIL PROTECTED]>wrote: >> >>> How does this below work? I know about the ?: comparison operator, I mean >>> how did you get it to print the original name? See below: >>> >>> longestname=(lengthofname1> lengthofname2)?name1:name2; >>> longestname=(longestname>lengthofname3)?longestname:name3; >>> >>> System.out.println(longestname + " has the longest first name!"); >>> >>> Hope I helped. >>> >>> Regards, >>> Manu. >>> >>> >>> I too was going to try this, but doing it this way is even longer. You >>> are going to end up with longestname = name1 2 or 3 >>> How do you know which one it is assigned too? Wouldn't you have to loop >>> through them to compare them to a boolean and print the statement if so? >>> I would really like to see the code you used for the Homework, because I >>> used the one that Rene used. >>> I am sure that you have name1 and so on assigned to the split name >>> indexed at zero, then you probably have another variable assigned to the >>> array which has the full name before the split. >>> >>> Thanks, >>> Stephen >>> >>> >>> On Fri, Sep 19, 2008 at 1:40 PM, Manu? <[EMAIL PROTECTED]> wrote: >>> >>>> String longestname; >>>> >>>> longestname=(lengthofname1>lengthofname2)?name1:name2; >>>> longestname=(longestname>lengthofname3)?longestname:name3; >>>> >>>> System.out.println(longestname + " has the longest first name!"); >>>> >>>> Hope I helped. >>>> >>>> Regards, >>>> Manu. >>>> >>>> >>>> On Fri, Sep 19, 2008 at 3:14 PM, Rene Erwee <[EMAIL PROTECTED]> wrote: >>>> >>>>> Hi all >>>>> >>>>> >>>>> >>>>> Thank you David for your good explanation of String Arrays which >>>>> finally enabled me to finish the homework. However, I am wondering if >>>>> there >>>>> is a more condensed version to find and print the longest name. I can >>>>> imagine If-else-else if statements will not work too well when one has a >>>>> whole lot of names to compare. >>>>> >>>>> >>>>> >>>>> // Print the longest name >>>>> >>>>> if((lengthOfFirstNameOfPerson1>=lengthOfFirstNameOfPerson2) >>>>> && >>>>> >>>>> (lengthOfFirstNameOfPerson1>=lengthOfFirstNameOfPerson3)){ >>>>> >>>>> System.out.println(firstNameOfPerson1[0] + " has the >>>>> longest first name"); >>>>> >>>>> } >>>>> >>>>> else if >>>>> ((lengthOfFirstNameOfPerson2>=lengthOfFirstNameOfPerson1) && >>>>> >>>>> (lengthOfFirstNameOfPerson2>=lengthOfFirstNameOfPerson3)){ >>>>> >>>>> System.out.println(firstNameOfPerson2[0] + " has the >>>>> longest first name"); >>>>> >>>>> } >>>>> >>>>> else { >>>>> >>>>> System.out.println(firstNameOfPerson3[0] + " has the >>>>> longest first name"); >>>>> >>>>> } >>>>> >>>>> } >>>>> >>>>> } >>>>> >>>>> >>>>> >>>>> Thanks >>>>> >>>>> * * >>>>> >>>>> *Rene Erwee* >>>>> >>>>> >>>>> >>>> >>>> >>>> -- >>>> This email may contain confidential and privileged proprietary material >>>> for the sole use of the intended recipient. Any review, use, or >>>> distribution >>>> or disclosure by others is strictly prohibited. If you are not the intended >>>> recipient, or authorized to receive the information from the recipient, >>>> please contact the sender by reply email and delete all copies of this >>>> message. >>>> >>>> >>>> >>>> >>> >> >> >> -- >> This email may contain confidential and privileged proprietary material >> for the sole use of the intended recipient. Any review, use, or distribution >> or disclosure by others is strictly prohibited. If you are not the intended >> recipient, or authorized to receive the information from the recipient, >> please contact the sender by reply email and delete all copies of this >> message. >> > > -- This email may contain confidential and privileged proprietary material for the sole use of the intended recipient. Any review, use, or distribution or disclosure by others is strictly prohibited. If you are not the intended recipient, or authorized to receive the information from the recipient, please contact the sender by reply email and delete all copies of this message. --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
