0

i get this 'else' without 'if' error on this case:

case 2:
                for(int i=0;i<currentRegisterdItems;i++){                            
                System.out.println("#"+i+" - "+itemArray[i] + "\t\t\t" + priceArray[i]);
                System.out.println("Select products and add to cart - Simple Shop Software");
                System.out.println("Enter the product id: ");
                int searchIdProduct =  Integer.parseInt(a.readLine());
                    if(itemArray[searchIdProduct] != null){
                        System.out.println("Add "+itemArray[searchIdProduct]+" to your cart?");
                        System.out.println("Confirm: Y    Cancel: N");
                        char confirmFlag = a.readLine().charAt(0);
                            if(confirmFlag == 'y'){
                                System.out.println("How many "+itemArray[searchIdProduct]+" do you want?");                         
                                int amountProduct =  Integer.parseInt(a.readLine());
                                System.out.println("Confirm: Y    Cancel: N");
                                char confirmFlag1 = a.readLine().charAt(0);
                                    if(confirmFlag1 == 'y')
                                    {
                                        if(amountProduct >= 1)
                                        {                                       
                                            double subTotalCost = priceArray[searchIdProduct] * amountProduct;                          
                                            quantityCartArray[opCounter] = amountProduct;
                                            cartArray[opCounter] = itemArray[searchIdProduct];
                                            subtotalCartArray[opCounter] = subTotalCost;

                                    System.out.println("Subtotal: "+df.format(subTotalCost));
                                    totalCost += subTotalCost;
                                    opCounter++;
                                    }
                                }
                            }
                        }
                    }else{
                        System.out.println("ERROR! - Product ID doesnt exist... to main menu");
                    }
            break;

can you tell me what is wrong that causes 'else' without 'if' error to show..

4
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

i already checked the curly braces and it's exact.. i can't figure what is wrong in it..

1

It looks like it's the foreach close bracket...

Keep the positioning of your curly brackets the same, make its more readable and consistant (you have some on new lines and some at the end of lines).

Edited by ChrisHunter

0
            case 2:
                for(int i=0;i<currentRegisterdItems;i++)
                {                           
                System.out.println("#"+i+" - "+itemArray[i] + "\t\t\t" + priceArray[i]);
                System.out.println("Select products and add to cart - Simple Shop Software");
                System.out.println("Enter the product id: ");
                int searchIdProduct =  Integer.parseInt(a.readLine());
                    if(itemArray[searchIdProduct] != null)
                    {
                        System.out.println("Add "+itemArray[searchIdProduct]+" to your cart?");
                        System.out.println("Confirm: Y    Cancel: N");
                        char confirmFlag = a.readLine().charAt(0);
                            if(confirmFlag == 'y')
                            {
                                System.out.println("How many "+itemArray[searchIdProduct]+" do you want?");                         
                                int amountProduct =  Integer.parseInt(a.readLine());
                                System.out.println("Confirm: Y    Cancel: N");
                                char confirmFlag1 = a.readLine().charAt(0);
                                    if(confirmFlag1 == 'y')
                                    {
                                        if(amountProduct >= 1)
                                        {                                       
                                            double subTotalCost = priceArray[searchIdProduct] * amountProduct;                          
                                            quantityCartArray[opCounter] = amountProduct;
                                            cartArray[opCounter] = itemArray[searchIdProduct];
                                            subtotalCartArray[opCounter] = subTotalCost;

                                    System.out.println("Subtotal: "+df.format(subTotalCost));
                                    totalCost += subTotalCost;
                                    opCounter++;
                                        }
                                    }
                            }
                    }
                }else{
                    System.out.println("ERROR! - Product ID doesnt exist... to main menu");
                }
            break;

this is the code that curly braces are positioned the same

0

You really should get a programmer's editor and clean that code up - proper consistent indentation would make this kind of problem obvious.
Anyway, I count four "if ... {"s, but there are five "}" before the else, so that's the error.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.