Page 3 of 3 FirstFirst 123
Results 41 to 48 of 48
  1. #41
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    That's closer to what I was going to suggest some time back - before you switched to the no argument version of computeTotalValue().

    What happens if you remove the duplicated getXXX() calls (and make sure the getXXX() calls all occur within the if block where myResultSet is in a valid state):

    Java Code:
    if (myResultSet.next () )
    {
        stockNameJTextField.setText(
                myResultSet.getString( "stockName" ) );
        stockSymbolJTextField.setText(
                myResultSet.getString( "stockSymbol" ) );
        String shares = myResultSet.getString( "shares" );
        sharesJTextField.setText(shares);
    					
        DecimalFormat priceFmt = new DecimalFormat( "$0.00" );
        double price = myResultSet.getDouble ( "price" );
        priceJTextField.setText( priceFmt.format (price) );	
        totalJTextField.setText ( 
                computeTotalValue(Integer.parseInt(shares), price));
    }
    myResultSet.close(); // close myResultSet
    Last edited by pbrockway2; 12-12-2010 at 09:40 PM.

  2. #42
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    I GOT IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

    with this:

    Java Code:
    private void displayStockData ( String stockName )
    	{
    		//display stock name, stock symbol, shares and price
    		try
    		{
    			//get stock name, stock symbol and shares
    			myResultSet = myStatement.executeQuery( "SELECT stockName, stockSymbol, "
    				+ " shares, price FROM stockInformation WHERE " +
    				"stockName = '" + ( stockName ) + "'" );
    			
    			// non empty result
    			if (myResultSet.next () )
    			{
    				stockNameJTextField.setText(
    					myResultSet.getString( "stockName" ) );
    				stockSymbolJTextField.setText(
    					myResultSet.getString( "stockSymbol" ) );
    					
    				DecimalFormat sharesFmt = new DecimalFormat ( "0" );	
    				int shares = myResultSet.getInt ( "shares" );
    				sharesJTextField.setText ( sharesFmt.format (shares) );
    					
    					
    				DecimalFormat priceFmt = new DecimalFormat( "$0.00" );
    				double price = myResultSet.getDouble ( "price" );
    				priceJTextField.setText( priceFmt.format (price) );	
    				
    				
    		
    			totalJTextField.setText ( computeTotalValue(shares,price) );
    			}
    			
    			
    			
    			myResultSet.close(); // close myResultSet
    			
    			
    		}
    		catch ( SQLException exception )
          	{
             	exception.printStackTrace();
          	}
          
    	}// end method display stock data
    	
    	// calculate total value
    	private String computeTotalValue(int shares, double price)throws SQLException
    	{
    		
    		// define display format
    		DecimalFormat dollars = new DecimalFormat( "$0.00" );
    
    		return dollars.format( shares * price );
    		
    	} // end method computeTotalValue

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

    Default

    I GOT IT!

    Well done!

    And, yes, "int shares" is better than "String shares".

  4. #44
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    i cant articulate my answer the way i want, but let me try to explain my thought process.

    the more i tried different things i was thinking that the shares was still sort of a "string" since i was getting the string from the DB....so in order to be multiplied by the Price, which was a double, it needed to be converted to an int.....so i set it up so it pulled information similar to the way "price was already formatted to do so. i know there must be an easier way, but hey, it works.....right?

  5. #45
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Thank you very much for your help the past few days.

    I'm still humbled by the knowledge in this forum, and the willingness people have to help. thank you very much!

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

    Default

    so i set it up so it pulled information similar to the way "price was already formatted to do so. i know there must be an easier way, but hey, it works.....right?

    In fact you did it exactly the right way. The database can supply data as String, int, double whatever. But, in fact, "shares" really is an int value (the database has been set up that way). So it makes perfect sense to access the "shares" value as an int - the same as accessing "price" as a double.

    The formatting is just something that is required by the text fields. (Because they work with String). So you add the formatting as an extra step to keep the text fields happy (and to make the data look nice), which is exactly what you did.

    ----------------------------

    One small point...

    You can now remove "throws SQLException" from computeTotalValue() since it no longer does any database access and will not throw an exception.

    Sort of ironic really when you consider how this thread started.

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

    Default

    thank you very much!

    You're most welcome.

  8. #48
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post

    One small point...

    You can now remove "throws SQLException" from computeTotalValue() since it no longer does any database access and will not throw an exception.

    Sort of ironic really when you consider how this thread started.
    Ahhhh! your right!

    Again, thank you. next week, the study is on Polymorphism, so im sure ill be back...haha.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Have I done this exercise right?
    By ccie007 in forum New To Java
    Replies: 7
    Last Post: 09-28-2010, 05:54 PM
  2. Exercise for java 3d
    By armiri in forum Java 2D
    Replies: 2
    Last Post: 05-13-2010, 11:14 PM
  3. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-13-2010, 11:13 PM
  4. Help needed with Java exercise - Including arrays - Reward
    By TheDarkReverend in forum New To Java
    Replies: 7
    Last Post: 10-23-2008, 02:52 AM
  5. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 04:48 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
  •