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:
Code java:
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.
Re: Help with this names program
Quote:
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
Re: Help with this names program
Quote:
Originally Posted by
Norm
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?
Re: Help with this names program
Please explain and show your code.
Re: Help with this names program
Quote:
Originally Posted by
Norm
Please explain and show your code.
Code java:
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.
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.
Re: Help with this names program
Quote:
Originally Posted by
Norm
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.
Code java:
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.
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.
Re: Help with this names program
I got it working. Thanks for the help!
Re: Help with this names program