Results 1 to 7 of 7
  1. #1
    killmenow is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Writing classes in Eclipse

    Hey guys, new to Java and Eclipse.
    Was wondering if anyone could explain to me why my code isnt working?
    I'm trying to write a class and i'm trying to throw an IllegalArgumentException but i keep getting this syntax error

    so far i have

    public class RangeCatenate {

    private String lower;
    private String upper;

    public RangeCatenate() {
    }

    public RangeCatenate (String lower, String upper)
    throws IllegalArgumentException {
    if (lower > upper)
    throw new IllegalArgumentException("lower is greater than upper"); //i get a syntax error thrown here that says > is undefined for the argument type.
    }


    what i'm trying to do is when you make a new object with the lower and upper value, it returns the range between those two values(with strings) and throw an illegalargumentexception
    any help would be great!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Writing classes in Eclipse

    i get a syntax error thrown here that says > is undefined for the argument type
    Your code says "lower>upper" and the compiler is referring to lower and upper as the arguments of the operator >. The type of these arguments is String because you declared them that way, and the compiler is pointing out that the > operator is not defined for strings. Strings aren't greater or lesser than one another in any straightforward sense the way numbers are.

  3. #3
    killmenow is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Writing classes in Eclipse

    Quote Originally Posted by pbrockway2 View Post
    Your code says "lower>upper" and the compiler is referring to lower and upper as the arguments of the operator >. The type of these arguments is String because you declared them that way, and the compiler is pointing out that the > operator is not defined for strings. Strings aren't greater or lesser than one another in any straightforward sense the way numbers are.
    so how would i write it to do what i want it to do? i completely understand that strings aren't greater or lesser than the way numbers would be, but i thought i remember that when strings are read(i know i'm probably saying it wrong but bear with me), its by like dictionary order. say if i want my lower to be cat and my upper to be mouse, then i would want to throw an exception when lower is bigger than my upper by dictonary order like mouse is lower and cat is upper

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Writing classes in Eclipse

    The String class provides a method which performs a dictionary comparison: compareTo().

    As you can see from its documentation it gives a ponderous definition of dictionary ordering and returns an integer value. If you say something like "foo.compareTo(bar)" the result will be a positive integer if the foo string comes after bar in the dictionary and a negative integer if it comes before. This returned integer can be compared with zero using >.

    Java Code:
    public class CatAndMouse {
        public static void main(String[] args) {
            String foo = "cat";
            String bar = "mouse";
            
            if(foo.compareTo(bar) > 0) {
                System.out.println("cat is after (bigger than) mouse!");
            } else {
                System.out.println("cat is before mouse");
            }
            
            foo = "tiger";
            if(foo.compareTo(bar) > 0) {
                System.out.println("now foo is bigger than bar!");
            } else {
                System.out.println("foo is still less than bar");
            }
        }
    }
    Try replacing "lower>upper" with an expression involving lower.compareTo(upper).

  5. #5
    killmenow is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Writing classes in Eclipse

    Quote Originally Posted by pbrockway2 View Post
    The String class provides a method which performs a dictionary comparison: compareTo().

    As you can see from its documentation it gives a ponderous definition of dictionary ordering and returns an integer value. If you say something like "foo.compareTo(bar)" the result will be a positive integer if the foo string comes after bar in the dictionary and a negative integer if it comes before. This returned integer can be compared with zero using >.

    Java Code:
    public class CatAndMouse {
        public static void main(String[] args) {
            String foo = "cat";
            String bar = "mouse";
            
            if(foo.compareTo(bar) > 0) {
                System.out.println("cat is after (bigger than) mouse!");
            } else {
                System.out.println("cat is before mouse");
            }
            
            foo = "tiger";
            if(foo.compareTo(bar) > 0) {
                System.out.println("now foo is bigger than bar!");
            } else {
                System.out.println("foo is still less than bar");
            }
        }
    }
    Try replacing "lower>upper" with an expression involving lower.compareTo(upper).

    ohhh i completely forgot that i had to compare it to 0. i was writing compareTo but it kept giving me a syntax error so i thought it was wrong. that was really helpful!
    i had one more question though, if i were trying to catenate all the words between range "cat" and "mouse", would i use the same compareTo in my "getter" method? say if i was trying to write


    public String getCatenation() {

    }


    should i use more compareTo methods in-between the block? i looked at some examples, and i noticed that majority of getter methods just return something like:


    public int getRollCount() {
    return rollCount;
    }

    am i suppose to catenate it somewhere else within the class and declare it to some variable then return it as the same variable in my getCatenation?
    thanks so much, this is really helpful!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Writing classes in Eclipse

    should i use more compareTo methods in-between the block? ... am i suppose to catenate it somewhere else within the class and declare it to some variable then return it as the same variable
    That's up to you. Doing the concatenation at the time the range is set (and assigning it to a variable etc) is sometimes called "eager" while waiting for someone to call getConcatenation() is called "lazy". A hybrid is common: have a String variable but leave it as null. When getConcatenation() is called check this variable and if it is non null return it, otherwise do the concatenation assign the result to the variable and then return it. That way you have the result as a variable in case getConcatenation is called multiple times, but you don't have to do the work at all if the method is never called.

    However you slice it you will go through the words of interest, check them (twice) with compareTo() and on that basis concatenate them (or not). If you haven't seen it before, check out the StringBuilder class which is designed for multiple concatenations.

    At the risk of confusing things... If you keep the words of interest (the words you will be concatenating) in a sorted collection (like a SortedSet) you may be able to reduce the number of compareTo() calls you have to make. (Keeping them sorted is another eager strategy vs lazily using compareTo() at the time you concatenate them.)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Writing classes in Eclipse

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Writing Two Classes help Please!!!!
    By stuckonjava in forum New To Java
    Replies: 36
    Last Post: 01-11-2012, 04:36 AM
  2. difficulty understanding writing classes
    By elecleoalune in forum New To Java
    Replies: 11
    Last Post: 04-18-2011, 03:06 PM
  3. Writing classes???
    By Bgreen7887 in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 08:06 AM
  4. Writing importable classes
    By Katsu in forum New To Java
    Replies: 6
    Last Post: 03-08-2009, 09:10 PM
  5. Writing classes in graphics
    By CyberFrog in forum Java Applets
    Replies: 2
    Last Post: 04-05-2008, 05:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •