Results 1 to 5 of 5
  1. #1
    harvest117 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Help with fault tolerance for simple program?

    So I recently started learning Java, outdated textbook in hand, and decided to take my experience slowly. Since I have plenty of time to learn I thought I would occasionally create my own applications; not unique ones, but apps that tested my knowledge of the syntax hopefully allowing me to learn more.

    Here I have coded a simple input/output type application. You type in some information (Name, Birthday) in some input dialog boxes and it repeats it back to you in a message dialog box. I know that it's very rudimentary: looking at it might hurt you because of its inelegance.

    Java Code:
    package identification;
    
    import javax.swing.JOptionPane;
    
    public class Identification
    {
        public static void main (String[] args)
        {
            String name;
    
            String birthDayNumber,
                   birthMonthNumber,
                   birthYearNumber;
    
            String output;
    
            int birthDay;
            int birthMonth;
            int birthYear;
            int age;
            int ageMinusOne;
    
            name = JOptionPane.showInputDialog ( "Enter name",
                    "Type your whole name here" );
    
            output = "";
    
            birthDayNumber = JOptionPane.showInputDialog ( "Enter day of birth (1 - 31)" );
            birthMonthNumber = JOptionPane.showInputDialog ( "Enter birth month (1 - 12)" );
            birthYearNumber = JOptionPane.showInputDialog ( "Enter birth year" );
    
            birthDay = Integer.parseInt ( birthDayNumber );
            birthMonth = Integer.parseInt ( birthMonthNumber );
            birthYear = Integer.parseInt ( birthYearNumber );
    
            age = 2011 - birthYear;
            ageMinusOne = 2010 - birthYear;
    
            if ( birthMonth == 1 );
                output = "Your name is " + name + ".\nYou were born on January "
                        + birthDay + ", " + birthYear + ".\n" + "You are about " 
                        + ageMinusOne + " to " + age + " years old.";
            if ( birthMonth == 2 );
                output = "Your name is " + name + ".\nYou were born on February "
                        + birthDay + ", " + birthYear + ".\n" + "You are about "
                        + ageMinusOne + " to " + age + " years old.";
            if ( birthMonth == 3 );
                output = "Your name is " + name + ".\nYou were born on March "
                        + birthDay + ", " + birthYear + ".\n" + "You are about "
                        + ageMinusOne + " to " + age + " years old.";

    The 'if' statements continue until the number twelve to cover all the months. I admit I only know how to deal with Strings and integers so this is how I worked around converting integer values (number of the month) into a String.

    It continues until:

    Java Code:
            if ( birthMonth == 12 );
                output = "Your name is " + name + ".\nYou were born on December "
                        + birthDay + ", " + birthYear + ".\n" + "You are about "
                        + ageMinusOne + " to " + age + " years old."; 
           
            JOptionPane.showMessageDialog ( null, output, "Identification Results",
                    JOptionPane.PLAIN_MESSAGE);
    
            System.exit ( 0 );

    The textbook I'm using doesn't actually include the System.exit class-method thingy in the example but it was said to be important for reasons unknown to me.

    As for the actual problem. I had my dad look at it and after running the program he entered info into the input dialogs: joseph, sixteen, ten, nineteen sixty, etc. Why? He said "I'm breaking your program". That was fine; I already knew I had to set parameters for wrong inputs and such but my question is how?

    My idea was to use an if statement for birthDay/Month/Year. I'll explain it in English.

    if birthDay/Month/Year does not equal an integer, then:

    1. Call back the same dialog until the a proper input is entered (how do I 'call' or 'go' to certain functions?)

    or

    2. Display an error message (e.g. Please enter an integer value) and then execute option 1. (how do I call up a certain message dialog?)

    If someone could help me with this I can also use it in an earlier Addition program (which was an actual project in the textbook) so if inputs for firstNumber and secondNumber are not integers I can set the value to zero.

    Sorry if I rambled a bit, I tend to write how I would talk. Any help would be greatly appreciated.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    if ( birthMonth == 1 );
    Look very closely at all your if statements. Perhaps review by looking at examples in the book.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    And to avoid the problem Junky is hinting at, always use brackets for control statements (if, for, while etc).

    ETA: OK, it doesn't avoid it, but it does help you to see what's going on, especially if you stick the open bracket on the same line as the control statement.

  4. #4
    harvest117 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    I took a look at the original program (it was used to compare 2 integers with equality symbols) and realized I had twelve too many semi-colons. Thank you for pointing it out!

    The app works perfectly as it is right now with the extra terminators which seems strange. How would you specify a non-integer in an if statement? Would it be

    if ( birthMonth != 'numbers 1 to 12' )
    JOptionPane.showMessageDialog ( null, "Please enter a valid integer value", "Error", JOptionPane.ERROR_MESSAGE )
    'call back previous input dialog';

    What I don't know the syntax for is highlighted in single apostrophes (' '): specifying every invalid input, and what how to call back the input dialog where the invalid input was entered.

    EDIT: I've decided just to keep on reading and come back to the problem when I get to it. Thank you for your help though.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I understand you solved it, however; I will explain what your were looking for. You can do if it's not in the range 1-12 with < and > signs.

    How would you determine if a number is less than 12 or greater than 1? Now inverse the statement and it till return true any time the input is not 1 - 12.

    Another tip, check out switch statements as opposed to lots of if/else statements when it comes to numbers

    Java Code:
    switch(input){
      case 1: //do something for 1
      case 2 : //do something for 2
      case 3: // do something for 3
      ...
      case 12: //do something for 12
      default:
    }
    One thing to be aware with in switch statements is when it finds a case that matches the input it will continue through all others, so you will need a break after each instruction case
    Java Code:
    case 1: //do something
      break;
    case 2: //do something 
      break;
    To get around people trying to break your code with a word instead of a number(january, nineteen) You can create 2 methods

    Java Code:
    public int findMonth(String month)
    public int findDate(String day)
    and this code can recognize some string and return the correct number.
    Java Code:
    findMonth("January") == 1
    findDate("twenty two") == 22

Similar Threads

  1. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 12:44 AM
  2. Simple program help
    By jtyler in forum New To Java
    Replies: 3
    Last Post: 09-20-2010, 07:43 AM
  3. Replies: 5
    Last Post: 10-28-2009, 01:59 PM
  4. segmentation fault error
    By Mrs. Deswal in forum Networking
    Replies: 0
    Last Post: 08-27-2009, 07:44 AM
  5. 'Incompatible operand' fault
    By fatnic388 in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 07:05 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
  •