Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Working with Do while loops
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Sevenheartz




PostPosted: Thu Feb 05, 2009 9:41 pm   Post subject: Working with Do while loops

Alright so... I haven't done ANY Java for an entire year so I've basically forgotten everything. This is an Intro Assignment that was given to us to just recap things... but I have a slight issue that I'm not sure how to fix at all. The situation is simple enough: ask questions and have the user enter data... However, when I use a do while loop, it seems to skip the first part. Let me get the code first:

Java:
import java.util.Scanner;

/** The "IntroAssignment" class.
 * Enters the name, price, and quantity on hand for one or more products, as well
 * as to output the product with the highest total value and the overall total value.
 */


public class IntroAssignment {

        /**
         * @param args
         */

        public static void main(String[] args) {
                // Defining variables
                Scanner keyboard = new Scanner (System.in);
                String ans = " ";
                Double total = 0.00;
                Double highestValue = 0.00;
                String highestProduct = "";
               
                // Title
                System.out.printf("%65s%n%n", "Inventory Management Program");
               
                // Main Program, loops if the user wishes to enter another product
                do
                {
                        System.out.print ("Please enter the name of the next product: ");
                        String product = keyboard.nextLine();
                        System.out.printf("Please enter the unit price for %s: $", product);
                        Double unitPrice = keyboard.nextDouble();
                        System.out.printf("How many %s do you have on hand?: ", product);
                        int numOfProduct = keyboard.nextInt ();
                        Double totalCost = unitPrice*numOfProduct;
                        System.out.printf("You have %d %s @ $%.2f for a total value of $%.2f%n", numOfProduct, product, unitPrice, totalCost);
               
                        // Calculates the total value thus far and checks if this product has
                        // the highest value
                        total += totalCost;
                        if (totalCost > highestValue)
                        {
                                highestValue = totalCost;
                                highestProduct = product;
                        }
               
                        // Asks if the user wants to repeat, and loops if the answer is not y/n.
                        do
                                {
                                System.out.print("Do you have anymore products? (y/n): ");           
                                ans = keyboard.next ().toUpperCase();
                                if (ans.charAt(0) == 'Y')
                                        System.out.println();
                                }
                        while (ans.charAt(0) !='Y' && ans.charAt(0) != 'N');
                }
                while (ans.charAt(0) == 'Y'); // repeats program if the user answers 'y'
       
                // End of the program
                System.out.println ();
                System.out.printf ("The total value of all inventory is: $%.2f%n", total);
                System.out.printf ("The item with the highest total value is $%.2f for %s.%n", highestValue, highestProduct);
                System.out.println("Thank you for using the Inventory Management Program");
        }
}


I've found that the problem is with
code:
String product = keyboard.nextLine();
. When the user enters Y in order to loop the program, it simply skips this piece of code and leaves the product name as just " ", and starts with askin how much is the unit price. I thought of using keyboard.next();, but not only would that not read everything if there was a space involved (i.e. "Large Items"), but it'd give me an error if I tried anything with a space. Sorry if my explanation isn't too clear. To better explain this, here's the output I get:

code:
Please enter the name of the next product: Item
Please enter the unit price for Item: $4
How many Item do you have on hand?: 4
You have 4 Item @ $4.00 for a total value of $16.00
Do you have anymore products? (y/n): y

Please enter the name of the next product: Please enter the unit price for : $


If anyone could help me out, it would be great. Thanks.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Fri Feb 06, 2009 8:52 am   Post subject: RE:Working with Do while loops

The problem here is that you're mixing calls to next() and nextLine() incorrectly. What happens is that when you type in the "Y" for "I have more items", it's only getting the next() token, which is "Y" without a newline character. When you call nextLine() to get the product name again, it sees that there's still some information available in the Scanner and returns the "remainder of the line", as per its specification...though in this case that means just the newline character.

You should change the next() call to be a nextLine(), but you will also need to add a call to nextLine() immediately after the nextInt() call, as you need that to suck up the newlines remaining in the Scanner after entering the integer.
Sevenheartz




PostPosted: Fri Feb 06, 2009 2:08 pm   Post subject: RE:Working with Do while loops

Got it working now. Thanks for the help!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: