Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: If input value is wrong, throw error and reenter

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If input value is wrong, throw error and reenter

    Hi, I am very new to java programming. I would like to let user enter the number greater than 0, and if i entered number is 0 then would inform user with message and let enter that number again. There are many inputs in program that have same conditions.
    I would think of that code, but it says dublicate variables or something. Anyway i hope you get the idea....
     System.out.print( "Type first number: " );
    int a= scanner.nextInt();
    if (a<=0){
    System.out.println("Must be more above 0");
    System.out.print( "Type first number: " );
    int a=scanner.nextInt();
    }
    System.out.print( "Type second number: " );
    int b=scanner.nextInt();
    if (b<=0){
    System.out.println("Must be more above 0");
    System.out.print( "Type first number: " );
    int a=scanner.nextInt();
    }
    and so on....


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: If input value is wrong, throw error and reenter

    Can you show your entire code in the code tags, as there could be other issues?

    As for the current problem, you only need to initialize variables once. That is, you only need to write int a = scanner.nextInt(); once. If you want to use the variable a again, just write a = scanner.nextInt(); I imagine you have the same problem for int b. The formatting when it prints to the console may be a bit awkward since you have 2 System.out.print(...) lines back to back. Either use println or \n at the end of the line.

    It's not an error but try to avoid use the object name scanner because it is very similar to the method Scanner. You can still use it, just be careful on your capitalization.

    Also, it would be better to write this in a loop (for, while or do-while) so you only have to write it out once and it becomes easier to follow. Here are some links to help you out with using them:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If input value is wrong, throw error and reenter

    Error, and cant run code: Duplicate local variable a. Appears when i ask to enter again value if previous one was wrong and store it into same variable.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If input value is wrong, throw error and reenter

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: If input value is wrong, throw error and reenter

    Quote Originally Posted by sabbath View Post
    Error, and cant run code: Duplicate local variable a. Appears when i ask to enter again value if previous one was wrong and store it into same variable.
    I don't know what the problem is without seeing the code but if it's similar to your first post, then make sure you only declare each variable once.

Similar Threads

  1. [SOLVED] how could I throw an exception in this case
    By mia_tech in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 2nd, 2012, 09:14 PM
  2. user input going to wrong ArrayList
    By havinFun in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2012, 02:58 PM
  3. Applet throw ClassNotFound exception when one of the file is recompiled
    By philwei in forum What's Wrong With My Code?
    Replies: 21
    Last Post: July 12th, 2011, 01:21 PM
  4. Calculator Throw Error
    By bengregg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 5th, 2011, 09:25 PM
  5. [SOLVED] Need help with input/output error
    By stefan2892 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2011, 10:44 AM