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..

Recommended Answers

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).

Jump to Post

Exactly. your else is placed after that }. There's no such thing as a for-else loop.

Jump to Post

All 8 Replies

Count your curly brackets. You must've closed one too early.

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

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).

            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

put a curly bracket before the break keyword.

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.

the one curly brace is used in the for loop..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.