Results 1 to 5 of 5
Thread: Help with assignment.
- 11-28-2012, 01:02 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Help with assignment.
Heya guys I'm new around here. I would like for lil help with my assignment .
What I want to to is when user inputs the int for price ( tehPrice ) I want it to make sure it is int , and also make a
loop which requires the input to be int
and if it isn't then it should go back and asks for input again. I tried some things with do and while ( setting boolean called correct) but then i had some
error saying not initialized .
Cheers.
Java Code:public void newArtItem() throws InputMismatchException { String tehName; int tehPrice; String tehFilename; scannie = new Scanner(System.in); System.out.print("Enter the name of the Art Item : "); tehName=scannie.next(); System.out.print("What file does it corresponds to ? : "); tehFilename=scannie.next(); System.out.println("Enter it's price : "); try { tehPrice=scannie.nextInt(); } catch(InputMismatchException zed) { System.out.println("Not valid number"); } artItem = new ArtItem(tehName,tehFilename,tehPrice); artItems.add(artItem); }Last edited by evdjoint; 11-28-2012 at 01:15 AM.
- 11-28-2012, 01:17 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: Help with assignment.
For a local variable you must give a initialization value before you can use it. For example for your loop condition check you can give it an initial value like:
Java Code:boolean correct = false; while (!correct) { ... }Website: Learn Java by Examples
-
Re: Help with assignment.
Hello and welcome to java-forums.org!
I think that you may be over-complicating your solution a little since Scanner can check what type of input is available with its hasNextXXX() methods. So exceptions may not be necessary to solve this. One possible solution in "pseudo-code" is:
Java Code:Ask for int input loop: while scan's next input is NOT an int use the Scanner to get the next line and do nothing with it. Tell that they again need to enter an int end of loop get int from Scanner and use it.
- 11-28-2012, 04:47 PM #4
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
-
Re: Help with assignment.
Wonderful and congrats!
Similar Threads
-
Need help for Assignment!!!
By HolyPK in forum New To JavaReplies: 6Last Post: 11-23-2011, 06:37 PM -
Help with assignment
By mehnihma in forum New To JavaReplies: 1Last Post: 09-18-2011, 03:06 AM -
Need Help With Assignment
By zeo in forum New To JavaReplies: 10Last Post: 02-25-2011, 12:09 AM -
Assignment
By shinkymar in forum New To JavaReplies: 2Last Post: 03-06-2010, 01:17 PM -
need help! assignment due by tmr!
By keycoffee in forum New To JavaReplies: 3Last Post: 02-10-2010, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks