Results 1 to 7 of 7
Thread: Input help
- 09-24-2009, 04:09 AM #1
Input help
Hey guys, I'm coding a homework assignment
I'm trying to make the session lok like this:
A session might look like this:
Year >>2100 ***
Value must be between 2001 and 2025
Year >>2010
This is what I have so far for the main class
Hehe, it doesn't work though.Java Code:import java.util.Scanner; public class Packages { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Year>> "); int year = keyboard.nextInt(); } private static int getYear(Scanner keyboard, int year) { int newYear; newYear = year; if (newYear < 2001 && newYear > 2005) System.out.println("Year should be between 2001 and 2005"); return year; }
I've tried to create a private class inside that method which was suggested by my teacher.
I tried to use that instead ofJava Code:private static int GetInt(input, "Year", 2001, 2005)
I would so greatly appreciate any help from you guys.Java Code:private static int getYear(Scanner keyboard, int year)
Thanks you!
- 09-24-2009, 09:56 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
What you've posted doesn't compile (missing bracket), but at least you used code tags!
You have my eyes undying gratitude for that...:)
Anyway, with what you've posted, you never actually call getYear().
- 09-24-2009, 10:39 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First compile your code and see there are any errors in your application. As Tolls says, there are few. And then read the error messages carefully, you can have an idea what's wrong in your code.
Good luck!
- 09-24-2009, 11:26 AM #4
Member
- Join Date
- Sep 2009
- Location
- Italy, Turin
- Posts
- 39
- Rep Power
- 0
Exactly ..and you didn't close the bracket of the method getYear! i suggest you to use an IDE like NetBeans or Eclipse to simply fix this little mistakes..
anyway.. in your main() ,perhaps you meant to do something like:
So your getYear prototype should be:Java Code:int year = Packages.getYear(keyboard.nextInt());
because i don't see any reasons to pass keyboard as a parameter in a method where you don't use it.. do you agree?Java Code:private static int getYear(int year)
- 09-25-2009, 06:20 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 09-25-2009, 03:38 PM #6
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
I agreed with others your posted code is not syntactically correct, and you did not call getYear method either to validate the year input.
Your conditional expression to check the keyed-in year has error. You may want to use || instead of &&.
Something like this:
Java Code:private static int getYear(int year) { int newYear; newYear = year; if (newYear < 2001 [COLOR="Red"]|| [/COLOR]newYear > 2005) { System.out.println("Year should be between 2001 and 2005"); } return year; }
- 09-26-2009, 07:07 PM #7
Similar Threads
-
Input technique for unknown lines of input
By ducreative in forum New To JavaReplies: 16Last Post: 09-23-2009, 09:26 AM -
help with input reader
By masterhoulahan in forum New To JavaReplies: 2Last Post: 03-25-2009, 05:12 AM -
skipping input
By windie_86 in forum New To JavaReplies: 1Last Post: 02-12-2008, 07:09 PM -
Using StreamTokenizer to take input
By Java Tip in forum Java TipReplies: 0Last Post: 02-04-2008, 09:38 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks