String has a method length() to determine its length.
Call that on both Strings and compare the results.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Melotron,
Here is an example of the code you need:
import javax.swing.JOptionPane;
public class Test
{
public static void main(String[] a)
{
//get first string from user
String indata1 = JOptionPane.showInputDialog("Enter text");
//get second string from user
String indata2 = JOptionPane.showInputDialog("Enter text");
//check length
if( indata1.length() > indata2.length())
{
System.out.println("The longest is " + indata1);
}
else if( indata1.length() < indata2.length())
{
System.out.println("The longest is " + indata2);
}
else
{
System.out.println("They are the same length");
}
}
}
:?: For more help, www.NeedProgrammingHelp.com
NPH
Junior Poster in Training
55 posts since May 2005
Reputation Points: 10
Solved Threads: 1
Here is an example that uses max_length.
import javax.swing.JOptionPane;
public class Test
{
public static void main(String[] a)
{
//get first string from user
String indata1 = JOptionPane.showInputDialog("Enter text");
//get second string from user
String indata2 = JOptionPane.showInputDialog("Enter text");
int max_length=0;
//check length
if( indata1.length() >= indata2.length())
{
max_length = indata1.length();
}
else
{
max_length = indata2.length();
}
System.out.println("The longest string has length " + max_length);
}
}
To make the code better, you could replace the if statements above with one line:
max_length = Math.max(indata1.length(), indata2.length());
:?: For more help, www.NeedProgrammingHelp.com
NPH
Junior Poster in Training
55 posts since May 2005
Reputation Points: 10
Solved Threads: 1
And what did you learn from turning in something someone else made for you?
NOTHING.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337