1,105,391 Community Members

Java program compile, but errors occur when ran.

Member Avatar
cjjm0202
Newbie Poster
2 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I wrote this code for class. It compiles fine, but I get errors when it is ran. What could be the problem, and what is the solution?

import java.util.*;

public class Lab3
{
    public static void main(String[] args)
    {
// variables
String name1= "Phones";
String name2 = "Laptops";
String name3 = "TVs";

int stockQty1 = 20;
int stockQty2 = 10;
int stockQty3 = 5;

double price1 = 29.99;
double price2 = 399.99;
double price3 = 999.99;
int quantity1 = 0;
int quantity2 = 0;
int quantity3 = 0;
double subTotal = 0;
double tax = 0;
double total = 0;
double taxRate = 0.06;
int stateCode = 0;
Scanner console = new Scanner (System.in);

System.out.printf("Welcome to The Shop%n");
// Item #1

System.out.printf("We have %d %ss at %.2 each. How many do you want to buy? %n", quantity1, name1, price1);
if (console.hasNextInt() ) {
quantity2 = console.nextInt();
if (quantity1 < 0) {
    quantity1 = Math.abs(quantity1);
}
  if (quantity1 > stockQty1)
      quantity1 = stockQty1;
System.out.printf("We only have %d items in stock. You just bought them all! %n");


} else {
    System.out.printf("Illegal input. Nothing sold.%n");
    quantity1 = 0;
    console.next();
        }
//Item #2
System.out.printf("We have %d %ss at %.2 each. How many do you want to buy? %n", quantity2, name2, price2);
if (console.hasNextInt() ) {
quantity2 = console.nextInt();
if (quantity1 < 0) {
    quantity1 = Math.abs(quantity2);
}
  if (quantity2 > stockQty2)
      quantity2 = stockQty2;
System.out.printf("We only have %d items in stock. You just bought them all! %n");

} else {
    System.out.printf("Illegal input. Nothing sold.%n");
    quantity2 = 0;
    console.next();
}
//Item #3

System.out.printf("We have %d %ss at %.2 each. How many do you want to buy? %n", quantity3, name3, price3);
if (console.hasNextInt() ) {
quantity2 = console.nextInt();
if (quantity3 < 0) {
    quantity3 = Math.abs(quantity3);
}
  if (quantity3 > stockQty3)
      quantity1 = stockQty3;
System.out.printf("We only have %d items in stock. You just bought them all! %n");

} else {
    System.out.printf("Illegal input. Nothing sold.%n");
    quantity1 = 0;
    console.next();
}

System.out.printf("Where do you live? Enter a number (0-3)%n");
System.out.printf("0) Maryland%n");
System.out.printf("1) Delaware%n");
System.out.printf("2) Pennsylvania%n");
System.out.printf("2) Virginia%n");
if (console.hasNextInt()) {
    stateCode = console.nextInt();
} else {
    stateCode = 0;
    console.next();
}

switch(stateCode) {
    case 0:
        taxRate = 0.06;
        System.out.printf("You are in Maryland. Your sales tax rate is 6%%n");
        break;
    case 1:
        taxRate = 0.00;
        System.out.printf("You are in Delaware. Your sales tax rate is 0%%n");
        break;
    case 2:
        taxRate = 0.06;
        System.out.printf("You are in Pennsylvania. Your sales tax rate is 6%%n");
        break;
    case 3:
        taxRate = 0.05;
        System.out.printf("You are in Virginia. Your sales tax rate is 5%%n");
        break;
    default:
        taxRate = 0.06;
        System.out.printf("You are in Maryland. Your sales tax rate is 6%%n");

}


System.out.printf ("%-10s%8s%12s%10s%n", "Name", "Price", "Quantity", "Total");
System.out.printf ("%-10s%8.2f%10d%8s%-12.2f%n", name1, price1, quantity1, "$", price1*quantity1);
System.out.printf ("%-10s%8.2f%10d%8s%-12.2f%n", name2, price2, quantity2, "$", price2*quantity2);
System.out.printf ("%-10s%8.2f%10d%8s%-12.2f%n", name3, price3, quantity3, "$", price3*quantity3);
System.out.printf ("----------------------------------------");

subTotal = quantity1*price1 + quantity2*price2 + quantity3*price3;
tax = subTotal * taxRate;
total = subTotal + tax;


System.out.printf("subTotal: %28.2f%n", subTotal);
System.out.printf("Tax: %28.2f%n", tax);
System.out.printf("total: %28.2f%n", total);

        }
    }
Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

move chars from lines 5. and 6. behing int stateCode = 0;

Member Avatar
cjjm0202
Newbie Poster
2 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Moving lines 5 and 6 doesn't seem to fix the program, just add to the errors.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: