if statement and void

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 11746 Views - Last Post: 17 January 2010 - 06:19 PM Rate Topic: -----

#16 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: if statement and void

Posted 17 January 2010 - 05:58 PM

View PostQball522, on 17 Jan, 2010 - 08:50 PM, said:

I would assume that he is in class and they are just trying to demonstrate the use of classes. I am in school for programming now and they have us write classes for some rather simple things that could be easier to do without them. Just to get used to witing and working with classes.


I would make the same assumption. However, unlike many of the Intro teachers, DIC promotes good programming practices. Unless he tells us that he absolutely has to use another class, we'd like to teach him the right thing the first time. :)

Also, I've included a link to the Comparable interface if you want to read more on it. If you're going to go to the trouble of writing a compareTo()-like method, it is probably better to use the Comparable interface as it will make your life easier when working with many API tools like Arrays.sort(), Collections.sort(), etc that look for objects whose classes implement Comparable. Hope it helps some.

Link: http://java.sun.com/...Comparable.html

Edited b/c the OP replied as I was replying.

@OP: The method I provided should be fairly easy to incorporate into your other class then. Go ahead and try incorporating it.

This post has been edited by macosxnerd101: 17 January 2010 - 06:00 PM

Was This Post Helpful? 1
  • +
  • -

#17 thecode222   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 11-November 09

Re: if statement and void

Posted 17 January 2010 - 06:02 PM

My teacher gave me these useful methods.

.compareTo()
This method will tell you if one String is smaller, equal to, or larger than another String.

String x = "abc";
String y = "cba";

int c = x.compareTo(y);
out.println( c ); // prints an int < 0

int c = y.compareTo(x);
out.println ( c ); // prints an int > 0

This post has been edited by thecode222: 17 January 2010 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: if statement and void

Posted 17 January 2010 - 06:19 PM

The compareTo() method is defined in the String class. Your instructor was just showing you how it worked. Basically, it returns -1, 0 or 1 based on whether the invoking object is less than, equal to or greater than the parameter. If you look at the printOrder() method I wrote, it illustrates this concept.

So if you want more information on the compareTo() method, check out the link to the Comparable interface I provided in my last post. You should probably be able to give it another try now. Good luck! :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2