-1

I'm trying to work on a java program that takes prices from a text file, allows people to enter in the quantity of what they want, and print it out into another text file.

sample code

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;

}

}

However every time I start it, this error message pops up:

    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)

I'm new to coding with java and have been screwing with line 46 for hour now trying to figure it out.

what am I doing wrong?

Thanks, HG

3 Answers3

0

Take input food_one to price three like this:

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();

It should work.

Mukit09
  • 1,890
  • 3
  • 15
  • 33
0

I changed the format in which you are storing the food and price (in prices.txt) to the following (with food and price separated by a delimiter, in this case a colon)

Pizz:20.10
Burger:10.30
Coffee:5.99

I changed the code to the following and now it is working. Hope this helps

    // 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]);
Hari Prasad
  • 184
  • 6
-2

Forgot to comment out fileIn.nextLine(); around like 46
your trying to put the name of a food into a 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();

Should be:

 //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();
Dylanrr
  • 31
  • 1
  • 6