I HAVE WRITTEN THIS BUT ITS NOT COMIPLING ITS GIVING ME ERRORS. CAN SOMEONE PLEA
ID: 3534335 • Letter: I
Question
I HAVE WRITTEN THIS BUT ITS NOT COMIPLING ITS GIVING ME ERRORS. CAN SOMEONE PLEASE FIX IT
import java.io.*;
import java.text.*;
import java.util.*;
//import java.util.StringTokenizer;
public class Assignment
{
public static void main(String [] args) throws IOException
{
NumberFormat nUS = NumberFormat.getCurrencyInstance(Locale.US);
DecimalFormat pf2 = new DecimalFormat("##.0#%");
String line;
String CustomerID, CustomerName;
int dunsels, widgets, lightWeights, gidgets;
float dumprice = 10.25F;
float widprice = 8.75F;
float lightprice = 4.25F;
float gidprice = 5.22F;
float taxrate = 0.0875F;
float shipping = 0.1F;
double subtotal = 0;
int count = 0, numtokens = 0;
StringTokenizer delimit;
boolean morerecords = true;
// Call the Scanner object "input" (can be named anything) - Alternative to BufferedReader
Scanner input = new Scanner(new File("Customerdata.dat"));
input.useDelimiter(System.getProperty("line.separator"));
while(input.hasNextLine())
{
try
{
line = input.nextLine();
delimit = new StringTokenizer(line, ",");
//numtokens = delimit.countTokens();
CustomerID = delimit.nextToken();
CustomerName = delimit.nextToken();
dunsels = delimit.nextToken();
widgets = delimit.nextToken();
lightWeights = delimit.nextToken();
gidgets = delimit.nextToken();
// Convert Strings into numeric values
dumprice = new Float(dunsels).floatValue();
widprice = new Float(widgets).floatValue();
lightprice = new Float(lightWeights).floatValue();
gidprice = new Float(gidgets).floatValue();
System.out.println(" Record #" + (count+=1));
System.out.println("Customer ID: "+ CustomerID);
System.out.println("Customer name: "+ CustomerName);
if(dunsels!=0)
{
System.out.println(dunsels+" Dunsels @ $10.25 $"+ pf2.format(dunsels * dumprice));
}
subtotal= dunsels * dumprice;
if(widgets!=0)
{
System.out.println(widgets+" Widgets @ $8.75 $"+ pf2.format(widgets * widprice));
}
subtotal+= widgets * widprice;
if(lightWeights!=0)
{
System.out.println(lightWeights+" Lightweights @ $4.25 $"+ pf2.format(lightWeights*lightprice));
}
subtotal+= lightWeights * lightprice;
if(gidgets!=0)
{
System.out.println(gidgets+" Gidgets @ $5.22 $"+ pf2.format(gidgets * gidprice));
}
subtotal+= gidgets * gidprice;
System.out.println();
System.out.println("Subtotal $"+ pf2.format(subtotal));
System.out.println();
System.out.println("Sales Tax @ 8.75% $"+ pf2.format(taxrate*subtotal));
System.out.println("Shipping and Handling @ 10% $"+ pf2.format(shipping*subtotal));
System.out.println();
System.out.println("Grandtotal $"+ pf2.format(subtotal+(taxrate*subtotal)+ pf2.format(shipping*subtotal)));
}
catch (NoSuchElementException e)
{ }
}
input.close();
}
}
I
Explanation / Answer
//here is the modified version of your code, i have removed the errors, and the program is giving the correct output just check it.
import java.io.*;
import java.text.*;
import java.util.*;
//import java.util.StringTokenizer;
public class Assignment
{
public static void main(String [] args) throws IOException
{
NumberFormat nUS = NumberFormat.getCurrencyInstance(Locale.US);
DecimalFormat pf2 = new DecimalFormat("#.00");
String line;
String CustomerID, CustomerName;
int dunsels, widgets, lightWeights, gidgets;
float dumprice = 10.25F;
float widprice = 8.75F;
float lightprice = 4.25F;
float gidprice = 5.32F;
float taxrate = 0.0875F;
float shipping = 0.1F;
double subtotal = 0;
int count = 0, numtokens = 0;
StringTokenizer delimit;
boolean morerecords = true;
// Call the Scanner object "input" (can be named anything) - Alternative to BufferedReader
Scanner input = new Scanner(new File("Customerdata.dat"));
input.useDelimiter(System.getProperty("line.separator"));
while(input.hasNextLine())
{
try
{
line = input.nextLine();
delimit = new StringTokenizer(line, ",");
//numtokens = delimit.countTokens();
CustomerID = delimit.nextToken();
CustomerName = delimit.nextToken();
dunsels = Integer.parseInt(delimit.nextToken());
widgets =Integer.parseInt(delimit.nextToken());
lightWeights = Integer.parseInt(delimit.nextToken());
gidgets = Integer.parseInt(delimit.nextToken());
System.out.println(" Record #" + (count+=1));
System.out.println("Customer ID: "+ CustomerID);
System.out.println("Customer name: "+ CustomerName);
if(dunsels!=0)
{
System.out.println(dunsels+" Dunsels @ $10.25 $"+ pf2.format(dunsels * dumprice));
}
subtotal= dunsels * dumprice;
if(widgets!=0)
{
System.out.println(widgets+" Widgets @ $8.75 $"+ pf2.format(widgets * widprice));
}
subtotal+= widgets * widprice;
if(lightWeights!=0)
{
System.out.println(lightWeights+" Lightweights @ $4.25 $"+ pf2.format(lightWeights*lightprice));
}
subtotal+= lightWeights * lightprice;
if(gidgets!=0)
{
System.out.println(gidgets+" Gidgets @ $5.22 $"+ pf2.format(gidgets * gidprice));
}
subtotal+= gidgets * gidprice;
System.out.println();
System.out.println("Subtotal $"+ pf2.format(subtotal));
System.out.println();
System.out.println("Sales Tax @ 8.75% $"+ pf2.format(taxrate*subtotal));
System.out.println("Shipping and Handling @ 10% $"+ pf2.format(shipping*subtotal));
System.out.println();
System.out.println("Grandtotal $"+ pf2.format(subtotal+taxrate*subtotal+shipping*subtotal));
}
catch (NoSuchElementException e)
{ }
}
input.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.