我正在尝试一个Java程序,该程序从文本文件获取价格,允许人们输入所需数量,然后将其打印到另一个文本文件中。
示例代码
import java.io.*;
import java.lang.Math;
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.*;
// work on invoice
public class lab2_notes{
public static void main(String[] args)throws IOException {
Scanner fileIn = null;
try
{
// Attempt to open the file
// file should be in working directory
fileIn = new Scanner(new FileInputStream("prices.txt"));
}
catch (FileNotFoundException e)
{
// If the file could not be found, this code is executed
// and then the program exits
System.out.println("File not found.");
// Shut the entire operation down
System.exit(0);
}
//convert file items to strings and numbers
String food_one;
double price_one;
String food_two;
double price_two;
String food_three;
double price_three;
//give strings and numbers varibles
food_one = fileIn.nextLine();
//fileIn.nextLine();
price_one = fileIn.nextDouble();
fileIn.nextLine();
food_two = fileIn.nextLine();
//fileIn.nextLine();
price_two = fileIn.nextDouble();
//fileIn.nextLine();
food_three = fileIn.nextLine();
//fileIn.nextLine();
price_three = fileIn.nextDouble();
//give input varibles for user to enter in how much food they want
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = keyboard.nextLine( );
System.out.println("Enter your zip code: ");
int zip = keyboard.nextInt( );
System.out.println(food_one + " " + price_one);
int quanity_one = keyboard.nextInt( );
System.out.println(food_two + " " + price_two);
int quanity_two = keyboard.nextInt( );
System.out.println(food_two + " " + price_two);
int quanity_three = keyboard.nextInt( );
//use methods to work the code out
double pr_one = total_for_items(quanity_one, price_one);
double pr_two = total_for_items(quanity_two, price_two);
double pr_three = total_for_items(quanity_three, price_three);
double sub_total = (pr_one + pr_two + pr_three);
double taxation = tax(sub_total);
double final_total = grand_total(sub_total, taxation);
String invoice = Invoice(name, zip);
//convert to deciminal class
DecimalFormat df = new DecimalFormat("$#,###,##.##");
String con_sub = df.format(sub_total);
String con_tax = df.format(taxation);
String con_final = df.format(final_total);
String con_one = df.format(pr_one);
String con_two = df.format(pr_two);
String con_three = df.format(pr_three);
//print out recept on screen
System.out.println("Invoice Number: " + invoice);
System.out.println("Item Quantity Price Total");
System.out.println("======================================");
System.out.printf(food_one + " " + con_one + " " + price_one + " " + pr_one);
System.out.println(food_two + " " + con_two + " " + price_two + " " + pr_two);
System.out.println(food_three + " " + con_three + " " + price_three + " " + pr_three);
System.out.println("======================================");
System.out.println("Subtotal: " + con_sub);
System.out.println("6.25% sales tax: " + con_tax);
System.out.println("Total: " + con_final);
String a = "Invoice Number: " + invoice + "\n";
String b = "Item Quantity Price Total" + "\n";
String c = food_one + " " + con_one + " " + price_one + " " + pr_one + "\n";
String d = food_two + " " + con_two + " " + price_two + " " + pr_two + "\n";
String e = food_three + " " + con_three + " " + price_three + " " + pr_three + "\n";
String f = "======================================";
String g = "Subtotal: " + con_sub + "\n";
String h = "Total: " + con_final + "\n";
//print recept on a self-created text file
PrintWriter recept = new PrintWriter("recept.txt", "UTF-8");
recept.println(a + b + c + d + e + f + g + h);
recept.println();
recept.close();
fileIn.close();
}
public static double total_for_items(double qone, double price){
double total_one = qone * price;
return total_one;
}
public static double tax(double total){
double tax_amt = total * Math.pow(6.25, -2);
return tax_amt;
}
public static double grand_total(double total, double tax){
double g_total = total + tax;
return g_total;
}
public static String Invoice(String name, int zip_code){
String part_one = name;
int part_two = zip_code;
Scanner pileIn = null;
try
{
// Attempt to open the file
// file should be in working directory
pileIn = new Scanner(new FileInputStream(part_one));
}
catch (FileNotFoundException e)
{
// If the file could not be found, this code is executed
// and then the program exits
System.out.println("File not found.");
// Shut the entire operation down
System.exit(0);
}
String Fname;
String Lname;
Fname = pileIn.nextLine();
pileIn.nextLine();
Lname = pileIn.nextLine();
//first part of name
String fnone = Fname.valueOf(Fname.charAt(1));
String fntwo = Fname.valueOf(Fname.charAt(2));
//second part of name
String lnone = Lname.valueOf(Lname.charAt(1));
String lntwo = Lname.valueOf(Lname.charAt(1));
//convert letters to uppercase
String con_fone_let = fnone.toUpperCase();
String con_ftwo_let = fntwo.toUpperCase();
String con_lnone_let = lnone.toUpperCase();
String con_lntwo_let = lntwo.toUpperCase();
String invoice_result = con_fone_let + con_ftwo_let + con_lnone_let + con_lntwo_let + zip_code;
return invoice_result;
}
}
但是,每次我启动它时,都会弹出此错误消息:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at lab2_notes.main(lab2_notes.java:46)
我是Java编程的新手,并且一直在搞弄第46行,现在试图弄清楚。
我在做什么错了?
谢谢,HG
答案 0 :(得分:0)
将输入food_one定价为三个,如下所示:
food_one = fileIn.nextLine();
price_one = fileIn.nextDouble();
fileIn.nextLine();
food_two = fileIn.nextLine();
price_two = fileIn.nextDouble();
fileIn.nextLine();
food_three = fileIn.nextLine();
price_three = fileIn.nextDouble();
应该可以。
答案 1 :(得分:0)
我将存储食物和价格(在prices.txt中)的格式更改为以下格式(食物和价格用定界符分隔,在本例中为冒号)
Pizz:20.10
汉堡:10.30
咖啡:5.99
我将代码更改为以下代码,现在可以正常工作了。希望这会有所帮助
// give strings and numbers varibles
String[] foodPrice = fileIn.nextLine().split(":");
food_one = foodPrice[0];
price_one = Double.parseDouble(foodPrice[1]);
foodPrice = fileIn.nextLine().split(":");
food_two = foodPrice[0];
price_two = Double.parseDouble(foodPrice[1]);
foodPrice = fileIn.nextLine().split(":");
food_three = foodPrice[0];
// fileIn.nextLine();
price_three = Double.parseDouble(foodPrice[1]);
答案 2 :(得分:-2)
忘记评论fileIn.nextLine();
,例如46
您试图将食物名称放入nextDouble()
//give strings and numbers varibles
food_one = fileIn.nextLine();
//fileIn.nextLine();
price_one = fileIn.nextDouble();
fileIn.nextLine();
food_two = fileIn.nextLine();
//fileIn.nextLine();
price_two = fileIn.nextDouble();
//fileIn.nextLine();
food_three = fileIn.nextLine();
//fileIn.nextLine();
price_three = fileIn.nextDouble();
应该是:
//give strings and numbers varibles
food_one = fileIn.nextLine();
//fileIn.nextLine();
price_one = fileIn.nextDouble();
//fileIn.nextLine();
food_two = fileIn.nextLine();
//fileIn.nextLine();
price_two = fileIn.nextDouble();
//fileIn.nextLine();
food_three = fileIn.nextLine();
//fileIn.nextLine();
price_three = fileIn.nextDouble();