Results 1 to 7 of 7

Thread: Input help

  1. #1
    rice's Avatar
    rice is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default 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


    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;
    }
    Hehe, it doesn't work though.
    I've tried to create a private class inside that method which was suggested by my teacher.
    Java Code:
    private static int GetInt(input, "Year", 2001, 2005)
    I tried to use that instead of
    Java Code:
    private static int getYear(Scanner keyboard, int year)
    I would so greatly appreciate any help from you guys.
    Thanks you!

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

    Default

    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().

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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!

  4. #4
    atom86 is offline Member
    Join Date
    Sep 2009
    Location
    Italy, Turin
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    What you've posted doesn't compile (missing bracket) [...]
    Anyway, with what you've posted, you never actually call getYear().
    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:
    Java Code:
    int year = Packages.getYear(keyboard.nextInt());
    So your getYear prototype should be:
    Java Code:
    private static int getYear(int year)
    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?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by atom86 View Post
    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?
    may be a bad design. ;)

  6. #6
    pkang is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Thumbs up

    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;
    }

  7. #7
    rice's Avatar
    rice is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  2. help with input reader
    By masterhoulahan in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 05:12 AM
  3. skipping input
    By windie_86 in forum New To Java
    Replies: 1
    Last Post: 02-12-2008, 07:09 PM
  4. Using StreamTokenizer to take input
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-04-2008, 09:38 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •