I need java coding help!!!!!!!!!!!!?!
Question:
I'm suppose write a code to return the longest name (out of a first, middle and last name) what is the longest in length. If the name is "Richard John Lobb" than the longest name it would return is "Richard"
so far all i could ever come up with is
public string getLongestName(String name){
}
and now i'm completely lost. I know it isnt much but im a beginner in coding and really lost... SO PLEASE HELP ME!Q&A For PC
so far all i could ever come up with is
public string getLongestName(String name){
}
and now i'm completely lost. I know it isnt much but im a beginner in coding and really lost... SO PLEASE HELP ME!Q&A For PC
Answers:
Nitaoe's answer is very nice, but it should be middle.length()
It works by treating the string like an input file, then reading each of the three names into it's own String value, the same way you might read them from the console.
Here is an alternative approach that I like quite a bit, because you can have any number of substrings (ie "Richard Wilson John Lobb jr. III")
First, it splits the string into an array of Strings where each String in the array is one of the names.
For example, "Richard Wilson John Lobb jr. III" will become
[ "Richard" , "Wilson" , "John" , "Lobb" , "jr." , "III" ]
Then it looks at each of the names, tracking the index of the longest one as it goes. Once has looked at them all, it returns the name at the index with the longest String.
//PLACE IN FILE: LongestName.java
public class LongestName
{
public static void main( String[] argv )
{
System.out.println( getLongestName("Richard Wilson John Lobb jr. III"));
}
public static String getLongestName(String name)
{
//split on every space, to create an array of strings
String[] names = name.split(" ");
//find the index of the longest one
int longest = 0;
for( int i=0 ; i<names.length ; i++ )
if( names[i].length() > names[longest].length() )
longest = i;
return names[longest];
}
}Q&A For PC
It works by treating the string like an input file, then reading each of the three names into it's own String value, the same way you might read them from the console.
Here is an alternative approach that I like quite a bit, because you can have any number of substrings (ie "Richard Wilson John Lobb jr. III")
First, it splits the string into an array of Strings where each String in the array is one of the names.
For example, "Richard Wilson John Lobb jr. III" will become
[ "Richard" , "Wilson" , "John" , "Lobb" , "jr." , "III" ]
Then it looks at each of the names, tracking the index of the longest one as it goes. Once has looked at them all, it returns the name at the index with the longest String.
//PLACE IN FILE: LongestName.java
public class LongestName
{
public static void main( String[] argv )
{
System.out.println( getLongestName("Richard Wilson John Lobb jr. III"));
}
public static String getLongestName(String name)
{
//split on every space, to create an array of strings
String[] names = name.split(" ");
//find the index of the longest one
int longest = 0;
for( int i=0 ; i<names.length ; i++ )
if( names[i].length() > names[longest].length() )
longest = i;
return names[longest];
}
}Q&A For PC
Well then, you really haven't come up with anything, have you.?
You have three separate strings, and you just need to know which one is longest.? Just check the length of each one and compare them, this is pretty basic. To learn more, check your textbook under string operations.Q&A For PC
You have three separate strings, and you just need to know which one is longest.? Just check the length of each one and compare them, this is pretty basic. To learn more, check your textbook under string operations.Q&A For PC
import java.util.Scanner;
public String getLongestName(String name){
Scanner sc = new Scanner(name);
String first = sc.next();
String middle = sc.next();
String last = sc.next();
String ret = (first.length() > middle.length) .? first : middle;
return (ret.length() > last.length()) .? ret : last;
}Q&A For PC
public String getLongestName(String name){
Scanner sc = new Scanner(name);
String first = sc.next();
String middle = sc.next();
String last = sc.next();
String ret = (first.length() > middle.length) .? first : middle;
return (ret.length() > last.length()) .? ret : last;
}Q&A For PC