aspose file tools*
The moose likes Java in General and the fly likes Exception problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception problem. " Watch "Exception problem. " New topic
Author

Exception problem.

Thomas Banks
Greenhorn

Joined: Apr 03, 2012
Posts: 10
hello guys,

I'm trying to throw an exception when the program receives a non integer parameters. ie. a string
I have looked at many examples on the internet and have so far failed to come up with a working solution. My test case continually fails.
I am not overly familiar with the try/catch method but I think that would be the logical way to go about things, any help would be great thanks.



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The general structure looks right. I don't see any code that actually uses the scanner to read the input string and stick the results into the sides array though.

What exact problem are you having? "My unit test fails" doesn't really contain any information.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
You said you had it working in your previous post.
What's changed since then ?
Thomas Banks
Greenhorn

Joined: Apr 03, 2012
Posts: 10
This is my full code. To be honest I'm not sure if the parameters are being read into the sides of the array myself but it seems to be working.
There is Comparison failure coming up and I think the two test are confusing each other. I have included that other test case which I suppose to find no integers.





And a picture paints a thousand words so I have attached one.
Thanks



[Thumbnail for testfilure55.png]

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Thomas Banks wrote:
And a picture paints a thousand words so I have attached one.


Please PostTextNotScreenshots.(⇐click) Speaking only for myself, I make it a point never to even bother looking at screenshots.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Why

instead of


A triangle constructor shouldn't have to worry about parsing a String to get its parameters. If you want to accept a String that you'll turn into 3 ints for your Triangle, do that as a separate step, before you even invoke the constructor. If parsing the String fails, then you don't even bother creating the Triangle.

Also, the loop for parsing that string is unnecessarily complex, regardless of whether it's in the c'tor or not.
Thomas Banks
Greenhorn

Joined: Apr 03, 2012
Posts: 10
The program I am creating is part of a college assignment.
My lecturer wants the numbers inputted as a string separated by a comma for reasons unknown, probably to make it 10 times tougher.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
In which case, I think you need to go back to square one. I suggest the following stages, which you can probably do quite easily by altering your existing work slightly. It is exactly what Jeff V is thinking of.
  • 1: Create a Triangle class with a constructor whose signature is Triangle(int, int, int)
  • 2: Use Exceptions or similar to enforce the invariant for a triangle: it has three sides, length > 0, and a + b > c, and a + c > b, and b + c > a. That is part of the Triangle constructor.
  • 3: Work out how to divide a String like "123, 234, 345" into three. Hint: there are methods in the String class to split it.
  • 4: Work out how to verify that each part of the String is in the correct form for an int.
  • 5: Parse the Strings to ints, probably after stripping whitespace.
  • Note that stages 3 and 4 can be swapped. It is hardly easier to check that a String is in the form "123" than to check it matches "123, 234, 345". You will probably need to know about regular expressions to achieve that.

    There are other ways to do this.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Thomas Banks wrote:The program I am creating is part of a college assignment.
    My lecturer wants the numbers inputted as a string separated by a comma for reasons unknown, probably to make it 10 times tougher.


    Did he say that the constructor has to take a String? Or just that you have to be able to accept input as a String? There's a huge difference.

    If he didn't specifically say the c'tor has to take a String, then use the 3-ints s'tor I suggested. Separate parsing the input from constructing the Triangle. This kind of separation of responsibility is at the very heart of all computer programming.

    Thomas Banks
    Greenhorn

    Joined: Apr 03, 2012
    Posts: 10
    Thank you for them steps.
    My lecturer however, does not want us to edit his code (skeleton code with empty methods that compiles but does nothing) in anyway.
    The assignment is to show our knowledge of the software development process, write test cases first then get the tests to pass by writing our own product code.
    The code I have shown is just one part of the assignment as I have to get the area and do individual tests on the 3 sides. I will post the skeleton code below so you know what I am talking about.

    He did include the string tokenizer class so I assume he wanted us to use it but I was not familiar with it at all, someone recommended I use the scanner class instead.
    Is there fundamental problems with my code? Other than the fact I cant throw an exception if a string is entered because it has taken me quite a bit of time to get this far.

    thanks

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    You have some repetition there. You have setXXX methods and constructors which appear to throw the same Exception.
    There are some dubious features, not least the use of StringTokenizer (←read this link to find out why I said “dubious”). As Jeff V has said, the constructor ought to take three ints, and the String splitting should be done elsewhere. But you have no choice about that.
    I would suggest the setXXX methods can call the validate method, too.
    If you want a String in the format "123, 234, 345", you ought not to use Scanner, either. Use what I suggested earlier.
    Thomas Banks
    Greenhorn

    Joined: Apr 03, 2012
    Posts: 10
    I have amended the code in my constructor to the way you recommended and I my valid and invalid triangle integer tests are passing. I also wrote some code to calculate the area
    of the triangle and it worked but after creating the if statement to throw the exceptions (valid and invalid triangle) it no longer worked. How would I get my area test to pass again?
    Since I enforced the invariant's for a triangle in the constructor what good are my setxxx methods and how should I re-write them?

    Also, the validate method will still take string parameters how can I call that method from my constructor or any other method for that matter and also how would I create a test case if the triangle is now of integer type.
    I have included all my product code and some of my tests. I have made a go at the validate method and tried to use the string split method but i think its not fully correct yet.

    Thanks for your help



    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    No. No. No. You can’t throw an Exception if the triangle is valid and also if it isn’t valid. You are also throwing the same Exception from several different places. You should do all your exception throwing from a validate() method, or similar. Try overloading that method to take (int, int, int) as parameters, as well as (String). The String method can parse the String into three ints and then call the other validate method. You can call validate from a setXXX method too, so you get all yoru exception throwing in one place. You will have to declare that Exception with a throws on those methods, and the constructors.

    And why can’t you have a triangle with sides >500?
    Thomas Banks
    Greenhorn

    Joined: Apr 03, 2012
    Posts: 10
    What I have done is create two Triangle constructors, one which will take string parameters (two many/few parameters) and one that will take integers and calls the validate method(calculate triangle <=0,>500,s1 > s2 +s3 ect).
    I don't know if that's good practice(probably not?) but since the assignment is about writing my test cases first and then writing product code to make them pass I'm not too bothered so long as they pass, and they do. This is also why I throw a valid triangle exception so my test can pass for 3 valid parameters. Basically, my lecturer whats all my test cases to pass!

    The very first problem I had when I started this thread (non-integer exception) is now working 100% with the string split method so thanks for that.

    I still have a problem, in calling the validate method in my integer constructor I seem to upset the sides I have initialized to calculate the area. Is there something I could do to make them work in harmony?
    This is what my constructor looks like and the area and validation tests that get in each others way.



    Oh and I limited it because I didn't want big ridiculous triangles.

    Martin Vajsar
    Sheriff

    Joined: Aug 22, 2010
    Posts: 3611
        
      60

    A small note regarding triangle validation. You use this condition to verify a triangle is valid:

    This assumes a triangle is valid even if a sum of two sides is equal to the third side. In most cases this would not be considered a valid triangle, since it would have zero area, two of its angles would have 0 degrees (and the third one would have 180 degrees) and it would be equal to a segment (ie. a line between two points; not sure about the terminology here). I think you should require the sum of any two sides to be greater than (not equal to) the third side.
    Thomas Banks
    Greenhorn

    Joined: Apr 03, 2012
    Posts: 10
    Well spotted Martin. I have made the changes. Thank you
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Exception problem.