Display longest string on screen
Code java:
// Practical 1B - Question 2
// Marcus Ward
// 27/01/2012
// Program asking user for two strings, and then it will display the longest string on screen.
import java.util.Scanner;
public class LongestString
{
public static void main(String[] args)
{
Scanner keyboardIn = new Scanner(System.in);
String one = new String(); // declare variables
String two = new String();
int num1, num2;
System.out.print("Please enter the first string: ");
one = keyboardIn.nextLine();
System.out.print("Please enter the second string: ");
two = keyboardIn.nextLine();
num1.length();
num2.length();
if(num1>num2)
{
System.out.println(one + " is the longest string.");
}
else
{
System.out.println(two + " is the longest string.");
}
}
}
2 Error Messages:
LongestString.java:24: int cannot be dereferenced
num1.length();
^
LongestString.java:25: int cannot be dereferenced
num2.length();
^
Re: Display longest string on screen
num1 is not an Object, let alone an object with the length() method, and thus the compiler is telling you it can't be dereferenced.
Note you can set num1/num2 to one/two.length() Besides that your code looks fine (Ignoring weird indenting)