Results 1 to 8 of 8
  1. #1
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default Program doesn't continue...

    I am using NetBeans 6.1, and our book is Introduction to Java Programming by Y. Daniel Liang.

    This is an agechecking program.
    Here is my code, it appears error free, but when run it doesn't print to the console - seemingly ignoring getInfo in main....
    Java Code:
    package agecheck;
    
    /**
     *
     * @author 06111246
     */
    import javax.swing.*;
    import java.util.*;
    public class AgeCheck {
    private
            String Name;
            String Age;
            int iAge;
            
            Scanner scanner = new Scanner(System.in);
            boolean continueInput = true;
    void setInfo()
    {
        Name = JOptionPane.showInputDialog(" Please enter your name:");
        Age = JOptionPane.showInputDialog("Enter your age: ");
        iAge = Integer.parseInt(Age);
        //int number = iAge;
    do
    {
        try{
            
            int number = scanner.nextInt();
            JOptionPane.showMessageDialog(null, "You entered " + iAge);
            continueInput = false;
        }
        catch (InputMismatchException number)
        {
            JOptionPane.showMessageDialog(null, "Enter a number for your age");
            scanner.nextLine();
        }
    }while (continueInput);
    
        
    }      
            
        
    int getInfo(int Check)
    {
        System.out.println 
             (Name + ", you are " + iAge + " years old.");
        if (iAge > 16)
          System.out.println("You are old enough to drive");     
        else
            System.out.println("You are not old enough to drive");
        if (iAge > 18)
            System.out.println("You are old enough to vote");
        else
            System.out.println("You are not old enough to vote");
        if (iAge >21)
            System.out.println("You are old enough to drink");
        else
            System.out.println("You are not old enough to drink");
        if (iAge > 72)
            System.out.println("You are old enough to be put out to pasture");
        else
            System.out.println("You are not old enough to retire");
               
             
             return 0;
    }
    
    }
    
    HERE IS MY MAIN:
    package agecheck;
    
    /**
     *
     * @author 06111246
     */
    public class Main 
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            int Check = 0;
            AgeCheck oAgeCheck;
            oAgeCheck = new AgeCheck();
            oAgeCheck.setInfo();
            oAgeCheck.getInfo(Check);  
        }
    }
    Again, i get no errors, I am wondering why it won't complete the program to tell if the age entered allows driving, voting, etc..
    thanks
    Reiyn
    Last edited by Reiyn; 10-04-2008 at 05:28 PM.

  2. #2
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    I do get an error when run:

    init:
    deps-jar:
    Compiling 1 source file to C:\Documents and Settings\06111246\Desktop\Unit03\AgeCheck\build\cl asses
    compile:
    run:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "k"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Integer.parseInt(Integer.java:497)
    at agecheck.AgeCheck.setInfo(AgeCheck.java:26)
    at agecheck.Main.main(Main.java:23)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 5 seconds)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    At line 26 in your code there is a call to parseInt. The value being passed to that method is not numeric.
    You either need to test for valid input values or to catch the exception and react to it.

    Add more println()s to your code to display the value of variables and to show execution flow.

    oAgeCheck.getInfo(Check);
    The above method returns a value. What are you doing with that value? Why does it return a value?
    Last edited by Norm; 10-04-2008 at 06:37 PM.

  4. #4
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    I've changed the do-try code. I'm thinking now it will accept the user age as a string, parse it, and then check that it is an integer :

    Java Code:
    do
    {
        try
        {
            Age = JOptionPane.showInputDialog("Enter your age ");
            iAge = Integer.parseInt(Age);
            scanner.nextInt(iAge);
            continueInput = false;
        }
        catch (InputMismatchException ex)
        {
            JOptionPane.showMessageDialog(null, "Enter a number for your age");
            scanner.nextLine();
        }
    }while (continueInput);
        
    }
    So far as the int Check in the getInfo, I need to pass something to call the function yes? Maybe I'm not understanding so well as I thought so far...

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    I don't understand what your question is.
    Do you get errors?
    Does it do what you want it to do?
    If not, please explain.

    scanner.nextInt(iAge);
    What is this line of code supposed to do? What does the API doc say?
    Does the nextInt method return a value?

  6. #6
    xdope is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default

    why don't you use switch?

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Where would you use a switch?
    Here? Nah, I don't think so.
    Java Code:
    if (iAge > 16)
          System.out.println("You are old enough to drive");     
        else
            System.out.println("You are not old enough to drive");
        if (iAge > 18)
            System.out.println("You are old enough to vote");
        else
            System.out.println("You are not old enough to vote");
        if (iAge >21)
            System.out.println("You are old enough to drink");
        else
            System.out.println("You are not old enough to drink");
        if (iAge > 72)
            System.out.println("You are old enough to be put out to pasture");
        else
            System.out.println("You are not old enough to retire");
    I die a little on the inside...
    Every time I get shot.

  8. #8
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    Our app is supposed to answer whether a user is old enough to drive, vote, drink or retire. iAge is the parsed integer for age.
    I posted the runtime error I get...
    I did change the Check int that I was passing to call the getInfo function to a string OutPut, and thus the if/then area has changed also...I'll be able to check my latest revisions tomorrow, thanks for helping so far.
    Norm I hope I clarified your questions some.

Similar Threads

  1. press any key to continue
    By dotnet007 in forum New To Java
    Replies: 3
    Last Post: 05-11-2008, 05:19 AM
  2. Demonstration of break and continue keywords
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:47 PM
  3. How to use Continue with a label
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:46 PM
  4. How to use Continue
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:46 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
  •