0

Ive been having some trouble with this assignment, its supposed to be kind of like a supermarket thing. I am new in java, the only import we are allowed to have if the scanner one.

The first time we record a customer and their items it works fine, it let me choose as many items as i want and of already existing in the array and everything runs smoothly - it goes on to ask if there´s any coupons and then it "prints" the receipt - the problem is the second customer - it all runs smoothly until this code (the one i´ll show below) it only lets me choose one product and the quantity and then it kind of moves to the next thing (ask for coupons and so on) and the third time it only let me choose the product - so i believe the problem is with the int.nextLine or something - i have looked at other questions and TRIED addind a nextLine or just next but it keeps happening so it might be the do while... i am not sure, i am new in java and in coding really - i will appreciate any kind of suggestion or input on it really.

do{
            
            System.out.println("WHICH ITEM WOULD YOU LIKE TO BUY");
            int CodeItem=input.nextInt();
            //the array has max 20 items
            if(CodeItem<=20 && CodeItem>=1){
        if(item[CodeItem-1]!=("null") && price[CodeItem-1]!=0.0) 
        {
        System.out.println("How many of:"+item[CodeItem-1]);
                 int CANT=input.nextInt();
                  
                 LOCALamount[CodeItem-1]+=CANT;
             GLOBALamount[CodeItem-1]+=CANT;
                 
            //the boolean ending is declared as true at the  start of the code             
        
        }
        else
        {
                System.out.println("****not valid****");
               ending=false;
              
            }}else
        {
                System.out.println("****not valid****");
              ending=false;
              
            }
        
        }while(ending);
cstjmn
  • 11
  • 3
  • 1
    Your code doesn't include `nextLine()`, and yet your text says "I think that might be the problem". Not much we can do, other than take a wild stab in the dark and link you to answers that explain the nature of next vs. nextLine. – rzwitserloot Feb 01 '23 at 05:55
  • This part - `if(item[CodeItem-1]!=("null")`. That doesn't look right to me. If you're trying to compare to the value `null`, then don't use the `" " ` marks. If you're trying to compare to the String `"null"`, then use `.equals` instead of `==` or `!=`. – Dawood ibn Kareem Feb 01 '23 at 06:20
  • When you write _the boolean ending is declared as true at the start of the code_: could it be that it is set to `false` within this loop and for the second customer not set to `true` again before this loop starts? – Thomas Kläger Feb 01 '23 at 07:03

1 Answers1

0

The issue is likely with the use of input.nextInt() and input.nextLine() in the same loop. The input.nextInt() method reads the next integer but does not consume the newline character that follows. This means the first call to input.nextLine() will immediately return an empty line, causing the loop to terminate.

One possible solution is to use input.nextLine() after the call to input.nextInt() and then parse it to an integer using the Integer.parseInt() method. This will ensure that the newline character is consumed, allowing the loop to continue as expected.

Here's an updated version of the code:

do {
    System.out.println("WHICH ITEM WOULD YOU LIKE TO BUY");
    int codeItem = Integer.parseInt(input.nextLine());
    if (codeItem <= 20 && codeItem >= 1) {
        if (item[codeItem - 1] != ("null") && price[codeItem - 1] != 0.0) {
            System.out.println("How many of:" + item[codeItem - 1]);
            int cant = Integer.parseInt(input.nextLine());
            LOCALamount[codeItem - 1] += cant;
            GLOBALamount[codeItem - 1] += cant;
        } else {
            System.out.println("****not valid****");
            ending = false;
        }
    } else {
        System.out.println("****not valid****");
        ending = false;
    }
} while (ending);

dev guy
  • 75
  • 7
  • This answer looks like it was generated by an AI (like ChatGPT), not by an actual human being. You should be aware that [posting AI-generated output is officially **BANNED** on Stack Overflow](https://meta.stackoverflow.com/q/421831). If this answer was indeed generated by an AI, then I strongly suggest you delete it before you get yourself into even bigger trouble: **WE TAKE PLAGIARISM SERIOUSLY HERE.** Please read: [Why posting GPT and ChatGPT generated answers is not currently allowed](https://stackoverflow.com/help/gpt-policy). – tchrist Jul 18 '23 at 13:14