Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

HELP-- else if not compiling like it should

Posted on 2006-11-21
7
Medium Priority
?
213 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
This video teaches viewers about errors in exception handling.
Suggested Courses

618 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