Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: try/catch exception for textfield?

  1. #1
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default try/catch exception for textfield?

    Where would I put a try/catch exception in the following program to stop it from force closing if a user doesnt enter a value in the textfield?

    What would a try/catch exception handler that popped up a message if user input was blank or equal to 0 look like for this program:

    Java Code:
    import java.text.DecimalFormat;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.*;
    import android.view.View.OnClickListener;
    import android.widget.*;
    
    public class SplitBill extends Activity {
    	
    	final double TIP_PERCENTAGE = .18D;
    	double tip;
    	double amountOfBill;
    	int numOfPeople;
    	double totalBill;
    	String experienceChoice;
    
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		
    
    		super.onCreate(savedInstanceState);				//Places SplitBill activity on top of the activity stack
    		setContentView(R.layout.split_the_bill);		//displays split_the_bill.xml layout
    		final EditText billAmount = (EditText)findViewById(R.id.txtBillAmt);		//collects bill input from user
    		final EditText peopleNumber = (EditText)findViewById(R.id.txtNumOfPeople);	//collects people input from user
    		final Spinner experience = (Spinner)findViewById(R.id.txtGroup);					//collects input from the users experience selection
    		Button calculate = (Button)findViewById(R.id.btnCalculate);
    		calculate.setOnClickListener(new OnClickListener() {
    		final TextView result = ((TextView)findViewById(R.id.txtResult));
    		
    
    		
    			@Override
    			public void onClick(View v) {											//convert variables into useable data types and assign variable names for use in calculations.
    				
    				amountOfBill = Double.parseDouble(billAmount.getText().toString());
    				numOfPeople  = Integer.parseInt(peopleNumber.getText().toString());
    				tip 	 	 = amountOfBill * TIP_PERCENTAGE;						//calculate tip	
    				totalBill 	 = (amountOfBill + tip)/numOfPeople;					//calculate total bill
    				DecimalFormat currency = new DecimalFormat("$###,###.00");			//format total bill for currency display
    				experienceChoice = experience.getSelectedItem().toString();			// assigns experience to the String variable experienceChoice
    				result.setText("For " + experienceChoice + " service, each person owes " + currency.format(totalBill));
    			
    			}
    		});
    	
    	}
    }




    ************************************************** ************************************






    Java Code:
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.*;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class Main extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);						
            setContentView(R.layout.activity_main);					
            Button start = (Button) findViewById(R.id.btnStart);		//initialize Button control and code Button event handler to launch SplitBill Activity class.
            start.setOnClickListener(new OnClickListener() {			//button listener awaits user interaction with Start button
    			
    			@Override
    			public void onClick(View v) {
    				startActivity(new Intent(Main.this, SplitBill.class));	//launches SplitBill.java class when the start button control is tapped.
    				
    			}
    		});
        }
    }

    The program works flawlessly other than if a user doesnt input a value into the text fields the app crashes. Thanks for any help you can give!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: try/catch exception for textfield?

    The code could validate the contents of the text fields BEFORE blindly using it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: try/catch exception for textfield?

    I am not sure I understand your response. Can you elaborate on that a bit? Thanks

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: try/catch exception for textfield?

    Something like this:
    Get value from text field
    validate value
    if good, use it
    if bad, tell user
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: try/catch exception for textfield?

    How would one do that? I am only a noobie with all of this, do you have an example?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: try/catch exception for textfield?

    Which of the 4 steps are you having problems with?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: try/catch exception for textfield?

    The last 3, in relation to validation / use/prompt

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: try/catch exception for textfield?

    Read the API documentation for, e.g. the Integer.parseInt( ... ) method; it throws a NumberFormatException if the String parameter doesn't represent a number; you can catch that exception an take corrective actions (like tell the user that s/he goofed). You have to wrap that method call in a try { ... } catch (NumberFormatException nfe) { ... ) where the last ellipses are your corrective code.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: try/catch exception for textfield?

    example billAmount,people number etc check for null and "" then only you should use the value.
    Ramya:cool:

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: try/catch exception for textfield?

    Quote Originally Posted by RamyaSivakanth View Post
    example billAmount,people number etc check for null and "" then only you should use the value.
    Cool, if I were a user, I'd type 'foo' and admire the crash ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: try/catch exception for textfield?

    I was wondering whether this is really a use case for JFormattedTextField until I saw this is Android code.

    Moving the thread.

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

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: try/catch exception for textfield?

    You can use a TextWatcher and then either prompt the user with a Toast or use the setError() method on the EditText field to give an error message on the item itself.

    No try/catch involved.
    Norm likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Exception catch
    By jeata in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 09:41 AM
  2. try catch exception
    By soxfan714 in forum New To Java
    Replies: 3
    Last Post: 05-05-2011, 11:11 AM
  3. [SOLVED] Sybtax for Try Catch for empty textfield
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-06-2009, 06:17 PM
  4. Why can't I catch an exception?
    By glhansen in forum New To Java
    Replies: 5
    Last Post: 03-17-2009, 06: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
  •