Results 1 to 5 of 5
  1. #1
    evdjoint is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Smile 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.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default 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) {
    ...
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  4. #4
    evdjoint is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help with assignment.

    Quote Originally Posted by Fubarable View Post
    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.
    Thanks I made it work ! :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Need help for Assignment!!!
    By HolyPK in forum New To Java
    Replies: 6
    Last Post: 11-23-2011, 06:37 PM
  2. Help with assignment
    By mehnihma in forum New To Java
    Replies: 1
    Last Post: 09-18-2011, 03:06 AM
  3. Need Help With Assignment
    By zeo in forum New To Java
    Replies: 10
    Last Post: 02-25-2011, 12:09 AM
  4. Assignment
    By shinkymar in forum New To Java
    Replies: 2
    Last Post: 03-06-2010, 01:17 PM
  5. need help! assignment due by tmr!
    By keycoffee in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 05:45 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
  •