Results 1 to 5 of 5
  1. #1
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default having trouble with loops

    Hey all, I'm new to this forum and programming and I hit a little wall when trying to finish my project for school.

    I have to meet certain requirements for this one program, and one of them is as follow:

    The program will require the user to enter the needed values one after another. If
    any of the entered values is invalid, then the program will detect that and go into a
    loop requiring the user to enter a valid value. The program must keep track of the
    total number of invalid values that the user has entered.
    I know how to set a restriction and create a loop to keep asking the same question until it is satisfactory ( in this case I used a while loop) but I am at loss on how I should keep track of the total number of invalid values. I know I can use an increment but how do I set a variable for when the program meets an wrong input from the user ( Don't know if the question makes sense :/)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: having trouble with loops

    Declare the loop counter before (and hence outside of) the loop itself. This is not the most common use of a for loop, but it is quite respectable.


    In your case a while loop seems reasonable. So, declare a counter, initialize it to zero (or one, whatever is appropriate), then I side the loop both get and check the input, and increment the counter. After the loop is done you will have "good" input and a counter which says how many times you went around the loop.

  3. #3
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: having trouble with loops

    Hey, instead of doing another thread, I would like to ask another question if you don't mind...

    I have to create a second loop where I ask user to input the cost for every item. I can't seem to be able to make the loop go in the order of asking for item 1, item 2, item 3, etc... ( min of 1 item and max of 10). I did this:


    while ( items < 1 || items > 10)

    {
    System.out.print("Sorry the input was not correct, please try again\n");
    items = x.nextInt();
    i++;
    }

    for (item=1; item=items; item++)
    {
    System.out.println("Please input the price of"+item)
    }
    However eclipse says that for item=items , it cannot convert from int to boolean. But both item and items are declared as int variables. So i'm kinda stuck with this loop too.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    27

    Default Re: having trouble with loops

    Use code tags, not quote tags, for code: BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    27

    Default Re: having trouble with loops

    A single = is an assignment, not a comparison, operator.
    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. New to Java please help!!! arrays loops while loops
    By Silver22 in forum New To Java
    Replies: 3
    Last Post: 05-29-2014, 02:11 AM
  2. Trouble with loops/classes/something
    By SykoPyro in forum New To Java
    Replies: 3
    Last Post: 09-05-2012, 01:34 PM
  3. trouble with showing the average with functions and loops
    By anonymous445 in forum New To Java
    Replies: 12
    Last Post: 01-29-2011, 11:30 PM
  4. trouble with loops
    By relics in forum New To Java
    Replies: 1
    Last Post: 09-23-2010, 12:01 AM
  5. Replies: 2
    Last Post: 02-23-2009, 03:18 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
  •