Solved

HELP-- else if not compiling like it should

Posted on 2006-11-21
7
195 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
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now