Results 1 to 8 of 8
Thread: Program doesn't continue...
- 10-04-2008, 05:25 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 29
- Rep Power
- 0
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....
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..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); } }
thanks
ReiynLast edited by Reiyn; 10-04-2008 at 05:28 PM.
- 10-04-2008, 06:03 PM #2
Member
- Join Date
- Sep 2008
- Posts
- 29
- Rep Power
- 0
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)
- 10-04-2008, 06:34 PM #3
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.
- 10-04-2008, 06:51 PM #4
Member
- Join Date
- Sep 2008
- Posts
- 29
- Rep Power
- 0
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 :
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...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); }
- 10-04-2008, 08:22 PM #5
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?
- 10-05-2008, 05:31 PM #6
Member
- Join Date
- Oct 2008
- Posts
- 10
- Rep Power
- 0
why don't you use switch?
- 10-05-2008, 07:00 PM #7
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
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.
- 10-07-2008, 10:28 PM #8
Member
- Join Date
- Sep 2008
- Posts
- 29
- Rep Power
- 0
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
-
press any key to continue
By dotnet007 in forum New To JavaReplies: 3Last Post: 05-11-2008, 05:19 AM -
Demonstration of break and continue keywords
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:47 PM -
How to use Continue with a label
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:46 PM -
How to use Continue
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks