Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Program Compiles, but has errors

cjjm0202cjjm0202 Posts: 1Member
I wrote this code for class, and it has errors when ran. What kind of problem is it, 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.println("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);

}
}




Comments

  • silveredge52silveredge52 Posts: 198Member
    Hey,
    The error says,
    [code]
    [color=Red]Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '.'
    at java.util.Formatter.checkText(Formatter.java:2503)
    at java.util.Formatter.parse(Formatter.java:2467)
    at java.util.Formatter.format(Formatter.java:2414)
    at java.io.PrintStream.format(PrintStream.java:920)
    at java.io.PrintStream.printf(PrintStream.java:821)
    at Lab3.main(Lab3.java:34)[/color][/code]
    The message says there is a format "conversion" error. The first
    statement that mentions your program, Lab3.main(Lab3.java:34), tells which line the problem occurred on. So the problem statement is line 34,
    [code]System.out.printf("We have %d %ss at %.2 each. How many do you want to buy?%n", quantity1, name1, price1);[/code]
    The problem is that %.2 is missing the type of field to format - which should be a floating-point number - ie. price1
    The correction is:
    [code]System.out.printf("We have %d %ss at %.2f each. How many do you want to buy?%n", quantity1, name1, price1);[/code]
    Here is a reference to [link=http://www.devdaily.com/blog/post/software-dev/cheat-sheet-reference-printf-format-specifier-syntax]printf format reference page (cheat sheet)[/link]. I found it using google.

    regards, se52
Sign In or Register to comment.