using a compareTo to get the longest string

open a GUI and enter 2 strings then display the longest using a compar

Page 1 of 1

10 Replies - 1707 Views - Last Post: 03 March 2009 - 09:36 PM Rate Topic: -----

#1 cgreazy2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-November 08

using a compareTo to get the longest string

Post icon  Posted 03 March 2009 - 08:07 PM

Ok I have the program running but it is sorting alphabetically. How can I get it to sort by length using a compareTo?

public void actionPerformed (ActionEvent e)

{
String firstStr;
String secondStr;

firstStr = input1TF.getText();
secondStr = input2TF.getText();

if (firstStr .compareTo (secondStr) > 0)
JOptionPane.showMessageDialog(null, firstStr);

else
JOptionPane.showMessageDialog(null, secondStr);
}

Is This A Good Question/Topic? 0
  • +

Replies To: using a compareTo to get the longest string

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 08:13 PM

Extend the String Class and override the compareTo() method to use length rather than character sequence to do the comparison.

This post has been edited by n8wxs: 03 March 2009 - 08:16 PM

Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 08:24 PM

View Postn8wxs, on 3 Mar, 2009 - 07:13 PM, said:

Extend the String Class and override the compareTo() method to use length rather than character sequence to do the comparison.

Ouf n8wxs you smoke good stuff tonight :^:
The only probelm with that approach is that you wont be able to do stuff like:
MyStringClass foo = "abcdef";
but will work with that:
firstStr = input1TF.getText();
secondStr = input2TF.getText();


cgreazy2 please :code:

This post has been edited by pbl: 03 March 2009 - 08:26 PM

Was This Post Helpful? 1
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 09:09 PM

Here's the catch, n8wxs...you can't extend String. It's a final class.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 09:18 PM

View PostLocke, on 3 Mar, 2009 - 08:09 PM, said:

Here's the catch, n8wxs...you can't extend String. It's a final class.

Really ? That makes my day
:^: :^: :^: :^: :^: :^:

n8wxs: -5
Locke: 10
PBL: 0

:D
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 09:19 PM

The only reason I know is because I've tried. ;)
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 09:29 PM

View Postpbl, on 3 Mar, 2009 - 08:18 PM, said:

View PostLocke, on 3 Mar, 2009 - 08:09 PM, said:

Here's the catch, n8wxs...you can't extend String. It's a final class.

Really ? That makes my day
:^: :^: :^: :^: :^: :^:

n8wxs: -5
Locke: 10
PBL: 0

:D

LOL!
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 09:31 PM

So Cgreazy2
if you really want to use the compareTo() method you will have to cheat a little bit

class MyString implements Comparable {
	String originalString;

	// constructor that receives a String as argument
   MyString(String str) {
	  originalString = str;
   }

   // compareTo
   public int compareTo(Object o) {
	  MyString other = (MyString) o;
	  return originalString.length() - other.originalString.length();
}



so before calling Arrays.sort or other

MyString[] array = new MyString[2];
array[0] = new MyString(str1);
array[1] = new MyString(str2);


now you can call the sort method with array
Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 09:31 PM

View PostLocke, on 3 Mar, 2009 - 08:09 PM, said:

Here's the catch, n8wxs...you can't extend String. It's a final class.

So that it is! I looked right at the doc page and didn't see that. Sigh. :)
Was This Post Helpful? 1
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 09:34 PM

View Postn8wxs, on 3 Mar, 2009 - 08:31 PM, said:

So that it is! I looked right at the doc page and didn't see that. Sigh. :)

Don't worry... at your age and with your eyes that might be considered normal :D
Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: using a compareTo to get the longest string

Posted 03 March 2009 - 09:36 PM

View Postpbl, on 3 Mar, 2009 - 10:34 PM, said:

Don't worry... at your age and with your eyes that might be considered normal :D


Ah...good times here in the Java forum. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1