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 14 of 14

Thread: loop

  1. #1
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default loop

    Hello,
    For my Java class, I need to prompt the user for sales amounts, and break on a negative number. so far I have this
    		while (true) {
     
    			for (int count = 0; count < salesAmounts.length; count++) {
    				System.out.print("Please enter sales amount (negative to end): ");
    				salesAmounts[count] = askSalesAmount.nextInt();
     
    				if (salesAmounts[count] <= 0)
    					;
    			}
    			for (int showArr: salesAmounts) {	
    				System.out.print(showArr);

    this works, but it always prints a last "Please enter sales amount (negative to end):" statement. I can't figure out how to make it stop when the negative number is input.
    also, I'm trying to show the numbers in the array. But the System.out.print doesn't show anything. Is there a good way to show the array with the numbers grouped together in a range. For example 0 through 10, 11 through 20 - and so on.

  2. #2
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: loop

    Well, if you fill the array up, until count < salesAmount.length, it should then exit the first for loop and move to the second, printing each item in the array. As for exiting the for loop when a number <= 0 is entered, enter a break statement in the if clause?

    I'm not sure what you mean about grouping the numbers together, they're already in an array. If you want 1-10 on one line, and 11-20 on the next, I guess you could make a couple of new arrays (one for numbers 1-10, another for numbers 11-20, etc), and then loop through your original array, comparing each number and adding it to the relavent array. Using System.out.print as you're doing will print them all in a line, with no spaces or line breaks.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: loop

    First, you don't need the first for loop. Just prompt for the input. Do you want to add the negative number to the list? The order of your statements will determine that. Once you determine it's negative, break out of the loop.

    The reason you aren't printing values is because they are inside the while loop and you break out before printing them. You while loop should be devoted to prompting for input. You can print them the way you are doing (but you may want to add a space between them). You can also use the Arrays.toString() method and print that.

    Regards,
    Jim

  4. #4
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop

    Thank you for the quick replies.
    I thought since
    for (int showArr: salesAmounts) {
    System.out.print(showArr);
    was outside of the brackets for the while statement, that it was outside of the loop. Should I put a break statement after the while, so it would look like this
    [code]
    while (true) {

    for (int count = 0; count < salesAmounts.length; count++) {
    System.out.print("Please enter sales amount (negative to end): ");
    salesAmounts[count] = askSalesAmount.nextInt();

    if (salesAmounts[count] <= 0)
    ;
    }
    break;
    for (int showArr: salesAmounts) {
    System.out.print(showArr);
    [\code]

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: loop

    while(true) {
    process input
    }

    print values.

    Regards,
    Jim

  6. #6
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop

    thank you jim829

    dboxall123 to clarify "I'm not sure what you mean about grouping the numbers together, "
    my output is supposed to show
    amounts occurances
    sales amount 100 - 200 4
    sales amount 300 - 400 2

    and so on to show how many time the sales amount for the week was 100 to 200, 300 to 400, and so on.
    So, like you suggested, I would want to create an array for each group of numbers? So array1 would have 100 through 200, array2 300 through 400, and so on?

  7. #7
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: loop

    Ah, I didn't realise that the second for loop was outside of the while loop, I was a bit confused by it.
    my output is supposed to show
    amounts occurances
    sales amount 100 - 200 4
    sales amount 300 - 400 2
    That's even easier, just create some integer variables, all set to zero. Then loop through your array, incrementing each one as required.

  8. #8
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop

    sorry, I'm very new to Java, and I'm not sure I'm explaining myself well. So this is the requirement.
    "Use a single-subscripted array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000 or a total of $650. Write an application (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):
    a) $200-$299
    b) $300-$399
    and so on. So how would I know what to increment by? I won't know what entered into the array.

  9. #9
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: loop

    So you don't have to do any of the math, right? You just have an array of integers? So, for example,

    int twoHundredTo299 = 0;
    int threeHundredTo399 = 0;
     
    for (int wage : wages) {
        if ((wage >= 200) && (wage <= 299)) {
             twoHundredTo299 ++;
        } else if ((wage >= 300) && (wage <= 399)) {
            threeHundredTo399 ++;
        } 
    }


    --- Update ---

    Oh hang on, I've just seen the bit about using an array of counters. So, i guess you still do it like the above example, but instead of incrementing the variables, you do something like this:

    for (int wage : wages) {
        if ((wage >= 200) && (wage <= 299)) {
             salaryRanges[0] ++;
        } else if ((wage >= 300) && (wage <= 399)) {
            salaryRanges[1] ++;
        } 
    }

  10. #10
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop

    dboxall123, I really appreciate your help, but I'm still not getting the output correctly. I'm sure it's my fault because I'm new to Java.
    So in your example, is salaryRanges[0] an array you use just for getting the total of the 200 to 299 range?
    The code I have so far is
    [code]
    import java.util.Arrays;
    import java.util.Scanner;

    public class Test1 {

    public static void main(String[] args) {

    Scanner askSalesAmount = new Scanner(System.in);
    int[] salesAmounts = new int[10];

    while (true) {

    for (int count = 0; count < salesAmounts.length; count++) {
    System.out.print("Please enter sales amount (negative to end): ");
    salesAmounts[count] = askSalesAmount.nextInt();

    if (salesAmounts[count] <= 0)
    break;
    }
    }
    for (int showArr: salesAmounts) {
    System.out.print(showArr);
    }
    }
    }
    [\code]

    I've used salesAmounts for my array to get the input from the user, but from what you're saying, I need to add an array for each range I'm searching? Is that correct?

  11. #11
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: loop

    Right, so I think that the assignment is asking you to create an array to hold the count of each salary range, so you would need to create a second array, let's call it amountsPaid. The size of amountsPaid will be the amount of ranges that there are (200-299, 300-399, 400-499, etc). Then you would loop through your salesAmounts array, checking each item. If the item is between 200-299, increment the first item (amountsPaid[0]) by one, if it's between 300-399, increment the second item by one. I think that's what it means by 'an array of counters'.

  12. #12
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop

    okay, sorry to be a pest, but in your example,

    for (int wage : wages) {
    if ((wage >= 200) && (wage <= 299)) {
    salaryRanges[0] ++;
    } else if ((wage >= 300) && (wage <= 399)) {
    salaryRanges[1] ++;
    }
    }

    where do you get wage and wages? is that the array that the user is inputting the data to? so in my code salesAmount is the array that is getting input from the user. And I guess I'm making an array for the 200 to 299 range. So If I call that twoHundredTo299, then according to your example, would I set it up like this

    for (twoHundredTo299 : twoHundredTo299s ) {
    if ((salesAmounts >=200 && ( salesAmounts<=299)) {
    twoHundredTo299 [0] ++;

    I'm doing something wrong because Eclipse has errors.

  13. #13
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: loop

    You need to show ALL of you code. And please put it between code tags. The easiest way to do that is to go to the advanced editor (lower right button), highlight ALL of your code and click the # symbol.

    Regards,
    Jim

  14. #14
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: loop

    Wage and wages were obviously just examples. And no, that wasn't what I meant. The exercise asks for an 'array of counters'. So, I think you need to set up an array, and each item of the array is set to how many times each possible wage range occurs. Do as Jim said, post your whole code. Also post the entire exercise.

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM