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 2 of 2

Thread: Display longest string on screen

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Display longest string on screen

    // 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();
    ^
    Last edited by helloworld922; January 29th, 2012 at 11:46 AM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default 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)

Similar Threads

  1. Placing user input to a array and to then display it as a string
    By LaliB in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2012, 11:41 AM
  2. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  3. String won't display in my window
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 24th, 2011, 04:23 PM
  4. [SOLVED] Painting Clock, Overlaps the current String display
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 5th, 2011, 07:54 AM
  5. Replies: 5
    Last Post: January 30th, 2009, 09:31 PM