Solved

HELP-- else if not compiling like it should

Posted on 2006-11-21
7
200 Views
Last Modified: 2012-05-05
I wrote the following program,but the elseif statement that decides how much of a discount to give keeps giving me an illegal start of type error....I cant figure out whats up. or what an illegal start is.


/************************************************************************
File: LogicErrors.java
 
This program reads in the items in a customer order for a company.  The item
number is read, the quantity, and the price per item.  A reduction of 10% is given
on items if 100 or more are ordered. A 20% reduction for 1000 or more items.  
In addition, another 20% reduction is given if the total bill exceeds $100,000.

For each item, the total cost and the amount saved is printed.

At the end the total order cost is printed along with the total amount of savings due to
item discounts and total bill discounts.

*************************************************************************/

import javax.swing.JOptionPane;
import java.text.NumberFormat;

public class LogicErrors{

      public static void main ( String args[] ){
      
            final double DISCOUNT1000 = 0.20;
            final double DISCOUNT100  = 0.10;
            final double GRAND_DISCOUNT = 0.20;

            String temp;
            String outputString = "";
         int   itemNo,
                        quantity;                       // quantity ordered

         double itemPrice,                         // Price of an item
                        totalItemPrice,            // total price of the item including discount
                        totalBill,
                        itemDiscount,                   // quantity discount on the item
                        totalItemDiscounts,
                        grossDiscount;
   
            NumberFormat money = NumberFormat.getCurrencyInstance();
                               
         
         totalBill = 0;
            totalItemDiscounts = 0;
            
            temp =  JOptionPane.showInputDialog("Enter the item number (enter 0 to stop): ");      
            itemNo = Integer.parseInt(temp);
                                     
          while (itemNo != 0)                    
         {                                      
                  temp = JOptionPane.showInputDialog("Quantity ordered: ");        
                  quantity = Integer.parseInt(temp);
                                   
                  temp = JOptionPane.showInputDialog("Price per item: $");    
            itemPrice = Float.parseFloat(temp);
               
                  // calculate the total cost of the items purchased including any discount
                  itemDiscount = 0;
                  totalItemPrice = itemPrice * quantity;

            if (quantity > 1000)
                  itemDiscount = DISCOUNT1000 * itemPrice;
                        else if (quantity >= 100 && <1000)
                  itemDiscount = DISCOUNT100 * itemPrice;
            else
            
            
            
                  totalItemPrice -= itemDiscount;
     
             totalBill += totalItemPrice;    
                  totalItemDiscounts = itemDiscount;
       
                  outputString += "Item number:" + itemNo + "   Quantity: "  + quantity + "  Cost per item: " + money.format(itemPrice);
                  outputString += "\nTotal for this item: " + money.format(totalItemPrice) + "   Savings on the item: " + money.format(itemDiscount) + "\n\n";
       
                    temp =  JOptionPane.showInputDialog("Enter the item number (enter 0 to stop): ");      
                  itemNo = Integer.parseInt(temp);
            }
            
            grossDiscount = 0;

            if ( totalBill >= 100000 )
            {
                  grossDiscount = GRAND_DISCOUNT * totalBill;
                  totalBill -= grossDiscount;
            }
            
          outputString += "\n\nTotal order cost is: " + money.format(totalBill) + "   Total savings: " + money.format(totalItemDiscounts + grossDiscount);                  
   
             JOptionPane.showMessageDialog ( null, outputString, "Invoice", JOptionPane.INFORMATION_MESSAGE );
            System.exit(0);                          
      
      } // end main

} // end class LogicErrors                                  
0
Comment
Question by:lusius188
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17989257
> else if (quantity >= 100 && <1000)
should be

else if(quantity >= 100 and quantity <1000)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17989258
>>else if (quantity >= 100 && <1000)

should be

else if (quantity >= 100 && quantity <1000)
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17989266
sorry about typo, it should be

else if(quantity >= 100 && quantity <1000)
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:lusius188
ID: 17989411
It actually compiles now which is awesome....but it completely screws up when you have anything about 1000 for quantity. I get values like 0 or .80 if im lucky....grrrr.....
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17989493
>>>>

                  else if (quantity >= 100 && <1000)
               itemDiscount = DISCOUNT100 * itemPrice;
          else
         


>>>>>>

I believe you should remove the else statement
0
 
LVL 9

Expert Comment

by:owenli27
ID: 17990345

- change if (quantity > 1000)  to if (quantity >= 1000)

- I believe   totalItemDiscounts = itemDiscount;  should be  totalItemDiscounts = totalItemDiscounts + (itemDiscount * quantity);

- I think that  outputString += "\n\nTotal order cost is: " + money.format(totalBill) + ...
  should be    outputString += "\n\nTotal order cost is: " + money.format(totalBill - totalItemDiscounts - grossDiscount) + ...
0
 
LVL 6

Accepted Solution

by:
SamsonChung earned 500 total points
ID: 17990833
Java refresh.

I know its annoying. but {} are very useful

if (quantity > 1000)
               itemDiscount = DISCOUNT1000 * itemPrice;
                    else if (quantity >= 100 && <1000)
               itemDiscount = DISCOUNT100 * itemPrice;
          else

rewrite to

if (quantity > 1000){
  itemDiscount = DISCOUNT1000 * itemPrice;
} else if (quantity >= 100 && quantity<1000){
  itemDiscount = DISCOUNT100 * itemPrice;
} else {
  //else block
}

This will save your bacon every time.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question