Local variables


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Local variables

  1. #1
    Join Date
    Jul 2002
    Posts
    2

    Question Local variables

    What is wrong with this code?

    I can do this very same example in VB and it works fine.

    I am new to Java and can't figure out why my String variable doesn't get the right value based on my if statements. If I test this code using 41 as the age it works fine. If I test this code using 71 as the age it works fine. If I test the code using any age for the other two sets of If statements, I get the right Amount but nothing appears for the Message.

    I am developing in Forte for Java 4.0

    Any Ideas??

    Thanks everyone,

    Leo


    Here is the code:

    package MyT1ld;

    // Java extension packages
    import javax.swing.JOptionPane;

    public class MyT1ld {

    // main method begins execution of Java application
    public static void main( String args[] )
    {
    String strLastName; // last name entered by user
    String strFirstName; // first name entered by user
    String strSSNumber; // ss number entered by user
    String strAddress; // address entered by user
    String strCity; // city entered by user
    String strState; // state entered by user
    String strZip; // zipcode entered by user
    String strAge; // number to input the age
    int Age; // your age as an integer
    String strAmount; // amount that insuree will pay
    String strMessage; // the message displayed

    // read last name from user as a String
    strLastName =
    JOptionPane.showInputDialog( "Enter your Last Name:" );

    // read first name from user as a String
    strFirstName =
    JOptionPane.showInputDialog( "Enter your First Name:" );

    // read ss number from user as a String
    strSSNumber =
    JOptionPane.showInputDialog( "Enter your Social Security Number:" );

    // read address from user as a String
    strAddress =
    JOptionPane.showInputDialog( "Enter your Address:" );

    // read city from user as a String
    strCity =
    JOptionPane.showInputDialog( "Enter your City:" );

    // read state from user as a String
    strState =
    JOptionPane.showInputDialog( "Enter your State:" );

    // read zipcode from user as a String
    strZip =
    JOptionPane.showInputDialog( "Enter your Zip Code:" );

    // read age from user as a String
    strAge =
    JOptionPane.showInputDialog( "Enter your Age:" );

    // convert numbers from type String to type int
    Age = Integer.parseInt( strAge );


    // initialize variables to empty String
    strAmount = "";
    strMessage = "";

    if ( Age >= 12 ){
    if ( Age < 25 )
    strAmount = ("$3600");
    strMessage = ("You are in a high accident group");
    }
    else
    strMessage = "";
    if ( Age >= 25 ){
    if ( Age < 40 )
    strAmount = ("$2400");
    strMessage = ("Your insurance rates are lower now");
    }
    else
    strMessage = "";
    if ( Age >= 40 ){
    if ( Age < 70 )
    strAmount = ("$1200");
    strMessage = ("You have the lowest rates available");
    }
    else
    strMessage = "";
    if ( Age > 70 ){
    strAmount = ("$3600");
    strMessage = ("Watch your driving");
    }

    // Display the results
    JOptionPane.showMessageDialog(
    null, "First Name: " + strFirstName + " Last Name: " + strLastName + "\n"
    + "Social Security Number: " + strSSNumber + " Age: " + Age + "\n"
    + "Address: " + strAddress + "\n"
    + "City: " + strCity + " State: " + strState + " ZipCode: " + strZip + "\n"
    + "Insurance Amount: " + strAmount + "\n"
    + "Comments: " + strMessage, "Your Insurance Quote",
    JOptionPane.INFORMATION_MESSAGE );

    System.exit( 0 ); // terminate application

    } // end method main

    } // end class MyT1ld

  2. #2
    Join Date
    Jul 2002
    Posts
    26
    I am having difficulty duplicating any problem here. Could you post the exact cases that don't work?

    -gc

  3. #3
    Join Date
    Jul 2002
    Posts
    2
    I can't get the strMessage to display anything for any age below 40. The right amounts display but no messages display at all. You say it works fine for You?

    Do I really need to be re-initializing the strMessage variable every time after the If statements?

    I've even tried testing for the highest age first, then the next lowest group etc.


  4. #4
    Join Date
    Jul 2002
    Posts
    26
    This might help you out. I'm sorry about before, but I wasn't sure what you were talking about. I have simplified the code a little bit. Tell me if this works for you.

    package MyT1ld;

    import javax.swing.JOptionPane;

    public class MyT1ld
    {
    // main method begins execution of Java application
    public static void main( String args[] )
    {
    String strLastName; // last name entered by user
    String strFirstName; // first name entered by user
    String strSSNumber; // ss number entered by user
    String strAddress; // address entered by user
    String strCity; // city entered by user
    String strState; // state entered by user
    String strZip; // zipcode entered by user
    String strAge; // number to input the age
    int Age; // your age as an integer
    String strAmount; // amount that insuree will pay
    String strMessage; // the message displayed

    // read last name from user as a String
    strLastName = JOptionPane.showInputDialog( "Enter your Last Name:" );

    // read first name from user as a String
    strFirstName = JOptionPane.showInputDialog( "Enter your First Name:" );

    // read ss number from user as a String
    strSSNumber = JOptionPane.showInputDialog( "Enter your Social Security Number:" );

    // read address from user as a String
    strAddress = JOptionPane.showInputDialog( "Enter your Address:" );

    // read city from user as a String
    strCity = JOptionPane.showInputDialog( "Enter your City:" );

    // read state from user as a String
    strState = JOptionPane.showInputDialog( "Enter your State:" );

    // read zipcode from user as a String
    strZip = JOptionPane.showInputDialog( "Enter your Zip Code:" );

    // read age from user as a String
    strAge = JOptionPane.showInputDialog( "Enter your Age:" );

    // convert numbers from type String to type int
    Age = Integer.parseInt( strAge );

    // initialize variables to empty String
    strAmount = "";
    strMessage = "";

    if ( Age >= 12 && Age < 25 )
    {
    strAmount = ("$3600");
    strMessage = ( "You are in a high accident group" );
    }
    else if ( Age >= 25 && Age < 40 )
    {
    strAmount = ("$2400");
    strMessage = ("Your insurance rates are lower now");
    }
    else if ( Age >= 40 && Age < 70 )
    {
    strAmount = ("$1200");
    strMessage = ("You have the lowest rates available");
    }
    else if ( Age > 70 )
    {
    strAmount = ("$3600");
    strMessage = ("Watch your driving");
    }

    // Display the results
    JOptionPane.showMessageDialog(
    null, "First Name: " + strFirstName + " Last Name: " + strLastName + "\n"
    + "Social Security Number: " + strSSNumber + " Age: " + Age + "\n"
    + "Address: " + strAddress + "\n"
    + "City: " + strCity + " State: " + strState + " ZipCode: " + strZip + "\n"
    + "Insurance Amount: " + strAmount + "\n"
    + "Comments: " + strMessage, "Your Insurance Quote",
    JOptionPane.INFORMATION_MESSAGE );

    System.exit( 0 ); // terminate application

    } // end method main
    }

    -gc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center