2

I have a college assignment where I need to print out items sold by a hardware store, take input from a user, perform some calculations on that input, and then print out an invoice.

I have been able to successfully print out the items sold by the hardware store, but am encountering problems with the while loop that takes the input.

The program asks the user to enter a CODE and then asks for the corresponding QUANTITY. This works fine on the first iteration of the loop, but on the second iteration the user prompts for "CODE:" and "QUANTITY:" appear on the same line, despite my use of println when prompting the user.

I would greatly appreciate a detailed response appropriate for someone new in programming.

Here's the code:

 import java.util.Scanner;

 class HardwareStore {


    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);


        System.out.println("WELCOME TO THE HARDWARE STORE!");
        System.out.println("----------------------------------------------------------------------");


        String sticky = "G22";
        String keyring = "K13";
        String screwy = "S21";
        String padlock = "I30";


        int stickyprice = 10989;
        int keyringprice = 5655;
        int screwyprice = 1099;
        int padlockprice = 4005;


        System.out.println("CODE\t\tDESCRIPTION\t\t\t\t\tPRICE");
        System.out.println("----\t\t-----------\t\t\t\t\t-----");

        System.out.println(sticky + "\t\tSTICKY Construction Glue, Heavy Duty, \n\t\t7oz, 12 Pack \t\t\t\t\t$" + stickyprice);
        System.out.println(keyring + "\t\tCAR-LO Key Ring, Quick Release, \n\t\t1 Pack\t\t\t\t\t\t$ " + keyringprice);
        System.out.println(screwy + "\t\t!GREAT DEAL! SCREW-DUP Screwy Screws, \n\t\tDry Wall Screws, 3 in. Long, 50 Pack\t\t$ " + screwyprice);
        System.out.println(padlock + "\t\tLET-IT-RAIN, Weather Proof Padlock, \n\t\tPortable, One Push Functionality\t\t$ " + padlockprice);
        System.out.println("----------------------------------------------------------------------");


        int i = 10000; 
        String [] usercode = new String[i]; 
        int [] userquantity = new int[i];


        System.out.println("PLEASE ENTER YOUR ORDER:");


        while (true) {
            System.out.println("CODE: (X to terminate)");
            usercode[i] = in.nextLine();

            if (usercode[i].equalsIgnoreCase("x")) {
                break;
            }

            System.out.println("QUANTITY: ");
            userquantity[i] = in.nextInt();
        } 
    }
}
9
  • Also, feel free to edit the rest of the code and give me tips on how to proceed with the program. Or what are some of the ways I can improve. Commented Dec 22, 2011 at 15:18
  • 2
    You should accept an answer on your previous question. Commented Dec 22, 2011 at 15:21
  • How do I do that? I did click YES to "Was this post helpful to you?" to one of the answers. I will do it again if that's what I gotta do. Maybe I didn't do it right the last time. Commented Dec 22, 2011 at 15:25
  • 2
    Thanks a lot. I figured it out and accepted one. Sorry, I am pretty new here. I am sure it can be annoying for you guys to deal with noobs like me but I will try to learn how to better use this website as soon as I can. Commented Dec 22, 2011 at 15:29
  • 2
    @nickecarlo we were all noobs once. don't worry. Hell, I still am. Commented Dec 22, 2011 at 15:29

1 Answer 1

5

when you enter the QUANTITY you're pressing enter. That newline character isn't used by in.nextInt();, it remains in the scanner buffer, until you roll around to in.nextLine() again.

At that point in.nextLine() reads until it finds a newline character, which just happens to be the next one in the buffer. So it skips straight to QUANTITY again.

Sign up to request clarification or add additional context in comments.

6 Comments

if you need more explanation, feel free to comment here nick. I'll update my answer with more info.
Thanks. Would adding an in.nextLine(); fix the problem here? Or am I completely off about this.
@nickecarlo yes, in this instance it would. It would consume the \n for the next loop around. This is not always going to be best practice though. I don't remember if Java allows input separation with a space from the command line, as C++ does. IF that is the case, the in.nextLine() would cause problems if you, for instance, entered "STICKY (SPACE) 3"
Okay, tested that and it seems to have worked. Thanks for the help, its weird that the books don't talk about it. I will post more of this program when I have worked on it more and need help, should I edit this same post, since its related to this program, or make a new one?
@nickecarlo see the edit I made in my last comment. I'd say open another question, unless it is on the same topic (issues with reading from command line)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.