473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Testing a method's return value after a call

10
I have tried to follow this and read around but I have a simple question..

I call method getIntField and return the value or exception, but I want to test that return.. If the value returned was not valid how to I trap and test that?

Method called...

Expand|Select|Wrap|Line Numbers
  1.  
  2. public int getIntField() {
  3.     try     {
  4.         return Integer.parseInt( searchStk.getText());
  5.     } catch (NumberFormatException e) {
  6.         message.setText("" + searchStk.getText() +"INVALID or wrong stock nbr, please re-enter...");
  7.         return 0;
  8.     }
  9. }
  10.  
I want to know what happened before I display the rest of my screen.

Thanks!
Dec 21 '08 #1
8 1795
jkmyoung
2,057 Expert 2GB
Can you put a try/catch around the place where you call getIntField()?
If getIntField ever returns 0 on a normal run, you shouldn't return 0 when you have an error.
Rather you should probably throw a new error after catching it, or rethrow the error. You can create your own Exception class if you want. Add a throws to your method declaration
Expand|Select|Wrap|Line Numbers
  1.  
  2. public int getIntField() throws NumberFormatException {
  3.     try     {
  4.         return Integer.parseInt( searchStk.getText());
  5.     } catch (NumberFormatException e) {
  6.         throw new NumberFormatException("" + searchStk.getText() +"INVALID or wrong stock nbr, please re-enter..."); //declare your new exception message
  7.     }
  8. }
  9.  
Dec 22 '08 #2
Noonga
10
Ah, and then test if NumberFormatException = x
Expand|Select|Wrap|Line Numbers
  1.  
  2. if (NumberFormatException = x) {
  3.      do this
  4. }
  5. else {
  6.      do that
  7. }
  8.  
Something like that?
Dec 22 '08 #3
Noonga
10
Nope, that didn't work. Ok, maybe I missed the exit ramp on this one. Here are the two methods I had so far..

Expand|Select|Wrap|Line Numbers
  1. private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
  2.     function = "search";
  3.     getIntField();
  4.     if (NumberFormatException == "Invalid Number") {
  5.         message.setText("Invalid!!!");
  6.     }
  7.     else {
  8.         recsel = Integer.parseInt(searchStk.getText());
  9.         searchStk.setText("");
  10.         recsel = recsel - 1;
  11.         displayRec( recsel );
  12.     }
  13. }
  14.  
and...

Expand|Select|Wrap|Line Numbers
  1. public int getIntField() throws NumberFormatException {
  2.     try     {
  3.         return Integer.parseInt( searchStk.getText());
  4.     } catch (ArrayIndexOutOfBoundsException e) {
  5.         message.setText("Record not found");
  6.         return 0;
  7.     } catch (NumberFormatException e) {
  8.         throw new NumberFormatException("Invalid Number");
  9.     }
  10. }
  11.  
Am I not getting this at all? heh, I know the light will come on soon.

This is what I get...
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: Invalid Number
Dec 22 '08 #4
Noonga
10
Ok, quick update...

Doing this instead..

Expand|Select|Wrap|Line Numbers
  1.     try     {
  2.         getIntField();
  3.     } catch (ArrayIndexOutOfBoundsException e) {
  4.         message.setText("Record not found");
  5.     } catch (NumberFormatException e) {
  6.         message.setText("Invalid Record, re-enter");
  7.     }
  8. //    recsel = Integer.parseInt(searchStk.getText());
  9. //    searchStk.setText("");
  10. //    recsel = recsel - 1;
  11. //    displayRec( recsel );
  12. }
  13.  
Now to figure out where the code goes when no exception is found and the user has input the proper information...

Getting there!
Dec 22 '08 #5
Noonga
10
ok, got it.. had to dig deeep to find an example of how to add the code if there were no exceptions.....

Thanks!

Expand|Select|Wrap|Line Numbers
  1. private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
  2.     function = "search";
  3.  
  4.     try     {
  5.         Integer.parseInt( searchStk.getText());
  6.         recsel = Integer.parseInt(searchStk.getText());
  7.         searchStk.setText("");
  8.         recsel = recsel - 1;
  9.         displayRec( recsel );
  10.     } catch (ArrayIndexOutOfBoundsException e) {
  11.         message.setText("Record not found");
  12.     } catch (NumberFormatException e) {
  13.         message.setText("Invalid Record, re-enter");
  14.     }
  15. }
  16.  
Dec 22 '08 #6
Noonga
10
ok, that worked, but not like I wanted, so before I post again guess I will try to look further. I did not want the following code to execute unless there were no exceptions..

Expand|Select|Wrap|Line Numbers
  1.         recsel = Integer.parseInt(searchStk.getText()); 
  2.         searchStk.setText(""); 
  3.         recsel = recsel - 1; 
  4.         displayRec( recsel ); 
  5.  
Dec 22 '08 #7
JosAH
11,448 Expert 8TB
Think about what you expect that method to return:

1) an arbitrary integer or
2) a failure status

If you can sacrifice one int value to indicate a failure status you can map all possible return values to the int domain: e.g. x == Integer.MIN_VALUE indicates a failure status otherwise x is the int return value for the method.

If you can't find such a sentinel value think classes; make your method return an instance of this class:

Expand|Select|Wrap|Line Numbers
  1. class ReturnValue {
  2.    private int x;
  3.    private boolean failure;
  4.    //
  5.    ReturnValue() { failure= true; }
  6.    ReturnValue(int x) { this.x= x; }
  7.    //
  8.    boolean isFailure() { return failure; }
  9.    int getX() { return x; }
  10. }
  11.  
Your method will look something like this:

Expand|Select|Wrap|Line Numbers
  1. ReturnValue yourMethod( ... )
  2.    try {
  3.       return new ReturnValue(Integer.parseInt( ... ));
  4.    }
  5.    catch (Exception e) {
  6.       return new ReturnValue();
  7.    }
  8. }
  9.  
... and the callers of this method can check the 'validity' of the returned value and, if no error occurred, get the int value from the ReturnValue object. Classes and objects aren't part of the language for no reason.

kind regards,

Jos
Dec 22 '08 #8
Noonga
10
Oh, thanks Jos!

I did play around with it and ended up returning an int value and then using that value to determine the path to take, similiar to what you mentioned.

I will pluck away at the code next week, but I really do appreciate the input.. your code is much smoother than what I did!
Dec 23 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some...
5
by: Brian | last post by:
Hello all.. Am working on an Air Hockey game... have an table loaded into a picture box. The borders of the table are slightly slanted. Am using hit testing lines with GDI+ to manipulate the...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
15
by: Enrique | last post by:
Question I am posting this question again (3rd time) because some issues with my no spam alias. Here it is the question: I have not been able to run unit tests for a VSTO (2005) project. I...
4
by: a | last post by:
I'm having trouble testing a custom object. I've tried many different approaches. One is shown below. The XML below shows the state of the object and I'm trying to test for that state, ie there...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
0
by: martinmercy2001 | last post by:
Could any body help me with creating a ring buffer class using a string. use memory circular buffer not an IO buffer. just read, write and seek method. Read method should take anumber and return the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.