Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Working with Methods

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Working with Methods

    This is my code:

    import javax.swing.JOptionPane ;
    import java.util.Date ;
     
     
    public class blahblah  {
      // *************************************************************************main
      public static void main(String[] args) {
     
    	String province ;
     
    	province = getGoodProvinceCode() ;
     
    	}// close main
     
     
     
          // *********************************************************************getPST
      public static String getPST(String province) {
     
    	final double GST = 0.05 ;
    	final double AB_PST = 0.0  ; // AB provincial sales tax
    	final double BC_PST = 0.07 ; // BC provincial sales tax
    	final double MB_PST = 0.07 ; // MB provincial sales tax
    	final double NB_PST = 0.08 ; // NB provincial sales tax
    	final double NL_PST = 0.08 ; // NL provincial sales tax
    	final double NS_PST = 0.08 ; // NS provincial sales tax
    	final double ON_PST = 0.08 ; // ON provincial sales tax
    	final double PE_PST = 0.10 ; // PE provincial sales tax
    	final double QC_PST = 0.075 ; // QC provincial sales tax
        final double SK_PST = 0.05 ; // SK provincial sales tax
    	double pst ;
    	double gst ;
    	double hst ;
     
    	String prov ;
     
    	province = getGoodProvinceCode() ;
     
    	// find tax rates
        gst = GST ;
        hst = pst = 0. ;
        if ( province.equals("AB") )
          pst = 0. ;
        else if ( province.equals("BC") )
          pst = BC_PST;
        else if ( province.equals("MB") )
          pst = MB_PST;
        else if ( province.equals("NB") ) {
          pst = gst = 0;
          hst = GST + NB_PST ;
        }
        else if ( province.equals("NL") ) {
          pst = gst = 0;
          hst = GST + NL_PST ;
        }
        else if ( province.equals("NS") ) {
          pst = gst = 0;
          hst = GST + NS_PST ;
        }
        else if ( province.equals("ON") )
          pst = ON_PST;
        else if ( province.equals("PE") )
          pst = PE_PST;
        else if ( province.equals("QC") )
          pst = QC_PST;
        else if ( province.equals("SK") )
          pst = SK_PST;
        else
          pst = -1 ;
     
        if ( hst > 0 ) {
    		prov = ("H" + hst) ;
    	}
    	else if ( hst == 0 ) {
    		prov = ("P" + pst) ;
    	}
    	else {
    		prov = ("") ;
    	}
     
    	return prov ;
     
      }// close getPST
     
          // *********************************************************************getGoodProvinceCode
      public static String getGoodProvinceCode() {
     
    	  String prompt ;
    	  String errorMsg ;
    	  String provinceC = "" ;
    	  String prov ;
    	  String province = "" ;
     
    	  boolean inputOk ;
     
        // input province
        prompt = "Enter two-letter province abbreviation (e.g., MB)" ;
        inputOk = false ;
        errorMsg = "" ;
        prov = getPST(province) ;
        while ( ! inputOk ) {
          provinceC = JOptionPane.showInputDialog(null, errorMsg + prompt) ;
          provinceC = provinceC.toUpperCase().trim() ;
          if ( prov.equals("") ) {
            errorMsg = "Input " + provinceC
              + " is not a valid province abbreviation\n" ;
            System.out.println( errorMsg ) ;
    	  }
          else {
            inputOk = true ;
          }
        }
     
        return provinceC ;
     
      }// close getGoodProvinceCode
     
    }// close class

    I get this error:

    Exception in thread "main" java.lang.StackOverflowError
    	at ChurchAustinA3Q1.getGoodProvinceCode(ChurchAustinA3Q1.java:101)
    	at ChurchAustinA3Q1.getPST(ChurchAustinA3Q1.java:48)
    	at ChurchAustinA3Q1.getGoodProvinceCode(ChurchAustinA3Q1.java:111)
    	at ChurchAustinA3Q1.getPST(ChurchAustinA3Q1.java:48)
    	at ChurchAustinA3Q1.getGoodProvinceCode(ChurchAustinA3Q1.java:111)
    	...etc...

    I think I am getting an error because getGoodProvinceCode() calls getPST(String province), and then getPST(String province) calls getGoodProvinceCode(). I just don't know how to fix this loop.

    EDIT/ Is it possible to return a variable in the middle of a method without ending the method at that point?
    Last edited by duckman; November 8th, 2009 at 05:55 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Working with Methods

    What you essentially have is a malformed recursive function.

    To fix:

    Have your main method call getProvince() to get a province, then call getPST() to get the sales tax rate for that province. Remove all calls from getProvince() to getPST() and vise-versa. You'll also need to adjust your methods to not call each other.

    No, a method may not return a value and continue running. That would violate the method contract.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    duckman (November 9th, 2009)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Working with Methods

    in the while loop of getGoodProvinceCode() u didnt tell the while loop how to break if 'prov= "" '. Hence it continues running that loop which resulted into the stackoverflow error.
    The solution is to place just a 'break statement' after printing 'errorMsg' and your problem is solved.

  5. The Following User Says Thank You to oyekunmi For This Useful Post:

    duckman (November 9th, 2009)

  6. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Working with Methods

    Thank you helloworld922 and oyekunmi, I figured it out now.

Similar Threads

  1. Substring program, not working
    By Newoor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2009, 12:46 PM
  2. Novice with java methods, please help!
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2009, 04:23 AM
  3. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 04:29 AM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM
  5. FileNotFound error while working with XML files in windows
    By ochieng in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 14th, 2008, 07:56 AM