Solved

HELP-- else if not compiling like it should

Posted on 2006-11-21
7
197 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 45
session migration servlets 2 39
hibernate example for saving data 19 43
eclipse buid path vs tomcat lib path 10 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

831 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