Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Help with this names program

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help with this names program

    I have to make a program that asks the user for a list of names, and then displays the number of names asked and the longest name in capital letters.
    So far, this is what I have:
    public class Names{
    public static void main(String[] args) {
    String phrase,name,name1,longest;
    int namelength,name1length;
    int c=0;
    Scanner stuff = new Scanner(System.in);
    System.out.print("Enter a first name(stop to quit):");
    name=stuff.nextLine();
    namelength=name.length();
    c=c+1;
    longest=name;
    System.out.print("Enter a first name(stop to quit):");
    name1=stuff.nextLine();
    name1length=name1.length();
    c=c+1;
     
    while (!name.equalsIgnoreCase("stop")){
    if (name1length>namelength){
    longest=name1;
    }
    if (namelength>name1length){
    longest=name;
    }
    System.out.print("Enter a first name(stop to quit):");
    name=stuff.nextLine();
    c=c+1;
    namelength=name1.length();
     
    }
    System.out.println(""+c+" names were entered.");
    System.out.println("The longest name was: "+longest+"");
    }
    }
    With that code, the working things are that it stops when stop is entered, and it displays the number of names entered. However, I can not figure out how to get it to display the longest name. I know I have some stuff in there that doesn't need to be there. Any help is appreciated.
    Last edited by RyanT; December 16th, 2011 at 06:42 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this names program

    can not figure out how to get it to display the longest name.
    You need a sort or a search to go through the list of names you have and find the longest one.
    Or you could save the longest one as you are reading them in. As you read the names, see if the new name is longer than the current saved longest name. If it is, replace it.

    Please Edit your post and wrap your code with
    [code=java]<YOUR CODE HERE>[/code] to get highlighting

  3. The Following User Says Thank You to Norm For This Useful Post:

    RyanT (December 17th, 2011)

  4. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this names program

    Quote Originally Posted by Norm View Post
    You need a sort or a search to go through the list of names you have and find the longest one.
    Or you could save the longest one as you are reading them in. As you read the names, see if the new name is longer than the current saved longest name. If it is, replace it.

    Please Edit your post and wrap your code with
    [code=java]<YOUR CODE HERE>[/code] to get highlighting
    I tried to save the longest one using the if statement, but it doesn't work so I guess I did something wrong with it. Is there any other way I could do it?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this names program

    it doesn't work
    Please explain and show your code.

  6. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this names program

    Quote Originally Posted by Norm View Post
    Please explain and show your code.
    if (name1length>namelength){
    longest=name1;
    }
    if (namelength>name1length){
    longest=name;
    }
    Those are my if statements, and I can't really explain why it doesn't work. It is like my program completely ignores them.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this names program

    You could use the length() method of String itself vs some variable that contains the length of the String.

    What values are in these variables: name1length & namelength
    and these: longest, name & name1

    Why two tests? You only need one test to compare the current name to the longest name found so far.

  8. The Following User Says Thank You to Norm For This Useful Post:

    RyanT (December 17th, 2011)

  9. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this names program

    Quote Originally Posted by Norm View Post
    You could use the length() method of String itself vs some variable that contains the length of the String.

    What values are in these variables: name1length & namelength
    and these: longest, name & name1
    name1length and namelength are both determined by the name entered. The same goes for name and name1, but I have longest being determined in those if statements. Which ever name is longer is supposed to go through those if statements, but it basically ignores them. I really have no idea why.
    System.out.print("Enter a first name(stop to quit):");
    name=stuff.nextLine();
    namelength=name.length();
    c=c+1;

    That code up there is how it determines the name and namelength. The program asks more than once and the second time is the same thing, just with name1 and name1length.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this names program

    You only need ONE test to see if the current name is longer than the longest one saved so far.
    Try rewriting your code to use only one test. Save the String itself not the length of the String.
    When the input loop ends, you should have saved the longest String that was entered.

  11. The Following User Says Thank You to Norm For This Useful Post:

    RyanT (December 17th, 2011)

  12. #9
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with this names program

    I got it working. Thanks for the help!

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with this names program

    You're welcome.

Similar Threads

  1. Can't figure out how to use Math.min() or return multiple names
    By Apocalypse in forum Java Theory & Questions
    Replies: 3
    Last Post: October 10th, 2011, 11:25 AM
  2. Listing file names in a JList
    By KILL3RTACO in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: October 8th, 2011, 12:52 PM
  3. array to store multiple names and data
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 6th, 2011, 05:03 AM
  4. Issue with code. Does not detect duplicate names in file
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 01:13 AM
  5. regular expressions, characters unallowed in file names
    By chopficaro in forum Java Theory & Questions
    Replies: 3
    Last Post: May 6th, 2010, 03:17 PM