Results 1 to 5 of 5
  1. #1
    guinea222 is offline Member
    Join Date
    Sep 2014
    Posts
    10
    Rep Power
    0

    Default Java sentinel while loop trouble

    I am writing a program where I need to input the following: customer Id, their income, federal withholding,state withholding and deductions. However, when I run my program it asks for my customerID and that is it... I am not sure what is incorrect here. This is just one section of my entire program.

    Java Code:
    int customerID = 0;
         // Get first Customer ID
        { while(customerID != -1)
        {
          System.out.print("Enter Customer ID: ");
          customerID = input.nextInt();
          // Get income and withholding information
          
          System.out.print("Enter Income: ");
               income = input.nextDouble();
                  
          System.out.print("Enter Federal Taxes Withheld: ");
          federalwh = input.nextDouble();
          
          System.out.print("Enter State Taxes Withheld: ");
          statewh = input.nextDouble();
          
          System.out.print("Enter Deductions: ");
          deduction = input.nextDouble();
        }
    
          // Get next Customer ID
         System.out.println("Enter Customer ID: ");
          customerID = input.nextInt();
    Last edited by guinea222; 09-25-2014 at 09:08 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Java sentinel while loop trouble

    You haven't shown enough of your program. Why are you using a loop if you are not saving the values? What is the
    initial value of customerID?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    guinea222 is offline Member
    Join Date
    Sep 2014
    Posts
    10
    Rep Power
    0

    Default Re: Java sentinel while loop trouble

    I did not want any copying my code for their hw, since I have been working so long on this, which is why I did not post it all. I have my loop running now, but at the end when I output all my information its putting the -1 from the loop into my Customer ID spot, when it should be the customer ID I am entering.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Java sentinel while loop trouble

    Do you still prompt twice (inside and outside of the loop) for the
    Customer ID? You need to show your latest code. I doubt that anyone will copy it. Besides,
    even if they do, then they won't learn anything and that will be apparent when they
    take the tests.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    dcarter's Avatar
    dcarter is offline Student
    Join Date
    Sep 2014
    Location
    NC
    Posts
    14
    Rep Power
    0

    Default Re: Java sentinel while loop trouble

    I am a little confused. So, a -1 input for customer ID should prompt the loop to end but the loop won't check the value of customerID against the boolean expression until the end of each iteration. Won't your loop then still ask for input for each of the other variables before checking whether to continue or end, therefore prompting the user to enter information for a customer ID of -1? And why do you ask for input of another customer ID after the loop ends?

Similar Threads

  1. Sentinel Loop
    By pricklypete in forum New To Java
    Replies: 3
    Last Post: 11-10-2012, 05:57 AM
  2. Sentinel value in a while loop
    By Mike Miller in forum New To Java
    Replies: 9
    Last Post: 09-30-2011, 03:04 AM
  3. Counting with a sentinel loop
    By Teclis in forum New To Java
    Replies: 0
    Last Post: 03-22-2011, 08:38 PM
  4. Trouble with for loop
    By subiedude101 in forum New To Java
    Replies: 4
    Last Post: 02-13-2011, 10:27 PM
  5. trouble with loop
    By aamster in forum New To Java
    Replies: 10
    Last Post: 10-16-2009, 10:18 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
  •