代码工作,但它不断给我的错误:InputMismatchException时和

问题描述 投票:-1回答:3

我试图在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

java
3个回答
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();

它应该工作。


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
投票

忘了注释掉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();
© www.soinside.com 2019 - 2024. All rights reserved.