Solved

HELP-- else if not compiling like it should

Posted on 2006-11-21
7
198 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 123
JavaFX TableView not displaying correctly 3 62
hashmap order 17 37
jsp error 6 27
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

820 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