Results 1 to 7 of 7
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question [SOLVED] (Double.toString(age)) & if ((age.isEmpty())) {

    Can someone just take a look at this code and tell me what is wrong

    Java Code:
    txtAge.setText(Double.toString(age));
    
                if  ((age.isEmpty())) {
    
                    // if no entry is made then throw error
    
                        throw new IllegalArgumentException("You did not enter any age");
                    }
                        catch (IllegalArgumentException e)   {   // catch the error
    
                        optPane1.showMessageDialog(this,e.getMessage(),"please enter your age",optPane1.ERROR_MESSAGE);
                    }
    I dont know whether its just the opening and closing braces or I have written it wrong.

    Thanks in advance

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, why don't you show us the compiler message? That will usually tell you exactly what's wrong.

    Edit: Although what I see, right off the bat is you're using catch without try. And, I have no idea why you're throwing an exception in the first place. Why not just pop up the dialog directly?

  3. #3
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    I will tell you what I am aiming for first.

    I have the following that works

    Java Code:
    try {
                        age = Double.parseDouble(txtAge.getText());
                        //read from screen as integer
                   if (age > 70) throw new IllegalArgumentException("Maximum Age is 70 Years Old");// set a trap to catch Age range 17 > 70
                     else if (age < 17)  throw new IllegalArgumentException("Minimum Age is 17 Years Old");
                     // else if (age = 0) throw new IllegalArgumentException("You must enter an age");
    				 }
                      catch (NumberFormatException e)
                      // will catch the error if the input is not a number
                     {
                      optPane1.showMessageDialog(this,"Age Must Be Between 17 + 70","ERROR",optPane1.ERROR_MESSAGE);
                      ok=false;
                     }
                      catch (IllegalArgumentException e)
                      //will catch the errors outside of the parameters set
                     {
                      optPane1.showMessageDialog(this,e.getMessage(),"Error",optPane1.ERROR_MESSAGE);
                      ok=false;
                      // e.getMessage will get the message for e as set in the trap
                     }
                    // Age try catch      END
    As I say this works, I am trying to make another try catch that will open if user doesnt put anything in the field but will not do the calculation at the end if there is an empty field prior, so I thought y not use

    Java Code:
    if  ((age.isEmpty())) {
    I assume that this would be good practice but if there is a more economical way of doing this I am open to suggestions as age is the first of four fields that I want working in the same way.

    As for the error(s)

    i get Double cannot be dereferenced from the following code

    Java Code:
    txtAge.setText(Double.toString(age));
    
                if  ((age.isEmpty())) {
                     try {
                    // if no entry is made then throw error
    
                        throw new IllegalArgumentException("You did not enter any age");
                    }
                        catch (IllegalArgumentException e)   {   // catch the error
    
                        optPane1.showMessageDialog(this,e.getMessage(),"please enter your age",optPane1.ERROR_MESSAGE);
                    }


    And yes I can hear you all laugh but if I comment the errors below out there are no errors and the program runs fine

    I put the above code back and I get 52 errors.

    As for the suggestion made abt
    Why not just pop up the dialog directly?
    would this work in the way I am wanting?


    :confused:


    Java Code:
    init:
    deps-jar:
    Compiling 9 source files to C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\build\classes
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1047: illegal start of expression
        private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdExitActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1047: illegal start of expression
        private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdExitActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1047: ';' expected
        private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdExitActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1047: ';' expected
        private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdExitActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1052: illegal start of expression
        private void cmdFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdFirstActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1052: illegal start of expression
        private void cmdFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdFirstActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1052: ';' expected
        private void cmdFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdFirstActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1052: ';' expected
        private void cmdFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdFirstActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1085: illegal start of expression
        private void cmdSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSearchActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1085: illegal start of expression
        private void cmdSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSearchActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1085: ';' expected
        private void cmdSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSearchActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1085: ';' expected
        private void cmdSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSearchActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1136: illegal start of expression
        private void cmdConvertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdConvertActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1136: illegal start of expression
        private void cmdConvertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdConvertActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1136: ';' expected
        private void cmdConvertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdConvertActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1136: ';' expected
        private void cmdConvertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdConvertActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1230: illegal start of expression
        private void cmdResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdResetActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1230: illegal start of expression
        private void cmdResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdResetActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1230: ';' expected
        private void cmdResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdResetActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1230: ';' expected
        private void cmdResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdResetActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1245: illegal start of expression
        private void jRmetricActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRmetricActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1245: illegal start of expression
        private void jRmetricActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRmetricActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1245: ';' expected
        private void jRmetricActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRmetricActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1245: ';' expected
        private void jRmetricActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRmetricActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1249: illegal start of expression
        private void txtConvHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtConvHeightActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1249: illegal start of expression
        private void txtConvHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtConvHeightActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1249: ';' expected
        private void txtConvHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtConvHeightActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1249: ';' expected
        private void txtConvHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtConvHeightActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1253: illegal start of expression
        private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdUpdateActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1253: illegal start of expression
        private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdUpdateActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1253: ';' expected
        private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdUpdateActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1253: ';' expected
        private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdUpdateActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1340: illegal start of expression
        private void jRimpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRimpActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1340: illegal start of expression
        private void jRimpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRimpActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1340: ';' expected
        private void jRimpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRimpActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1340: ';' expected
        private void jRimpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRimpActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1344: illegal start of expression
        private void txtResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtResultsActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1344: illegal start of expression
        private void txtResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtResultsActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1344: ';' expected
        private void txtResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtResultsActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1344: ';' expected
        private void txtResultsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtResultsActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1349: illegal start of expression
        private void txtGenderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGenderActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1349: illegal start of expression
        private void txtGenderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGenderActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1349: ';' expected
        private void txtGenderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGenderActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1349: ';' expected
        private void txtGenderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGenderActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1353: illegal start of expression
        private void txtHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHeightActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1353: illegal start of expression
        private void txtHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHeightActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1353: ';' expected
        private void txtHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHeightActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1353: ';' expected
        private void txtHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHeightActionPerformed
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1360: illegal start of expression
        public static void main(String args[]) {
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1360: illegal start of expression
        public static void main(String args[]) {
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1360: ';' expected
        public static void main(String args[]) {
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1360: ')' expected
        public static void main(String args[]) {
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1360: illegal start of expression
        public static void main(String args[]) {
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1360: ';' expected
        public static void main(String args[]) {
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1360: illegal start of expression
        public static void main(String args[]) {
    C:\Users\Admin\Desktop\javaprojects\collegeSamples\homework\Assessment3Bmi\src\assessment3bmi\frmBmi.java:1360: ';' expected
        public static void main(String args[]) {
    56 errors
    BUILD FAILED (total time: 0 seconds)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One problem I see is that age is a primitive variable, a double, and you can't call any methods on a primitive as if it were an object. In other words, this:
    Java Code:
    age.isEmpty()
    is not Kosher. You call the isEmpty method on a String only, not on a double variable.

    Perhaps instead you wish to get the text from the JTextField and place it in a String variable, then call isEmpty() on this variable. If not empty then try to parse it to a double.

  5. #5
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    well i have it working, maybe an experienced programmer can say whether this is classed as good programming or just a lazy way of doing it?

    As I say it works alongside everything else in the program.

    Java Code:
     String Ages;
            Double.toString(age);
            try {
            Ages = txtAge.getText();
                if  ((Ages.isEmpty())) 
            // if no entry is made then throw error
                        throw new IllegalArgumentException("You did not enter your age");
                    }
                        catch (IllegalArgumentException e)   {   // catch the error
                        optPane1.showMessageDialog(this,e.getMessage(),"please enter your age",optPane1.ERROR_MESSAGE);
                    }

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Once again, though, there is no reason to use a try catch block there. Simply
    Java Code:
    String Ages = txtAge.getText();
    if  ((Ages.isEmpty()))
        optPane1.showMessageDialog(this,"You did not enter your age","please enter your age",optPane1.ERROR_MESSAGE);
    will accomplish the exact same thing without the try/catch overhead.
    Last edited by masijade; 04-09-2009 at 08:04 AM.

  7. #7
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

Similar Threads

  1. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  2. toString question
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 07:41 PM
  3. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  4. Arrays.toString
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:35 PM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:32 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
  •