im trying to get the program to send the results gotten from a .txt file to a se
ID: 3531934 • Letter: I
Question
im trying to get the program to send the results gotten from a .txt file to a separate output file. but for some reason when i added the very last "try" statement to my code, the program wouldn't read my sales.txt file anymore. how do i output to a separate .txt file correctly?
import java.io.*;
import java.io.FileNotFoundException;
import java.util.*;
import java.io.PrintWriter;
import java.text.DecimalFormat;
public class Sales4
{
public static void main(String[] args)
{
DecimalFormat df=new DecimalFormat("#,###,###.00");
String name;
double service;
double lodge;
double costs;
double dinner;
double misc;
String date;
Double total;
double a=0;
double b=0;
double c=0;
double d=0;
double f=0;
String filename = "salelist.txt";
String filename2="output.txt";
System.out.println("name"+" "+"service"+" "+"lodge"+" "+"costs"+" "+"dinner"+" "+"misc"+" "+"total"+" "+"date");
try
{
File file = new File(filename); // Creates a variable of file
Scanner in = new Scanner(file); //Creates a variable for the scanner
while (in.hasNextLine())
{
String line = in.nextLine(); //Takes the whole line as input from Scanner to line
try
{
System.out.println();
String[] strArray = line.split(";"); // strings are seperated by ";"
int i=0;
for (String str : strArray) //Loop until strings exists
{
if(i==0)
{
name = str;
i++;
System.out.print(name + " ");
}
else if(i==1)
{
service = Double.parseDouble(str);
a=service;
i++;
System.out.print(df.format(service) + " ");
}
else if(i==2)
{
lodge = Double.parseDouble(str); //converting the string taken into integer and assigning it to lodge
b=lodge;
i++;
System.out.print(df.format(lodge) + " ");
}
else if(i==3)
{
costs = Double.parseDouble(str); //Converting the string taken into integer and assigning it to cost
c=costs;
i++;
System.out.print(df.format(costs) + " ");
}
else if(i==4)
{
dinner = Double.parseDouble(str); //Converting the string taken into integer and assigning it to dinner
d=dinner;
i++;
System.out.print(df.format(dinner) + " ");
}
else if(i==5)
{
misc = Double.parseDouble(str); //Converting the string taken into integer and assigning it to misc
f=misc;
i++;
System.out.print(df.format(misc) + " ");
}
else if(i==6)
{
total=a+b+c+d+f;
i++;
System.out.print(df.format(total)+" ");
}
else if(i==7)
{
date = str; // assigning str get from file it to date
i++;
System.out.print(date + " ");
}
}
}
catch(InputMismatchException e) //Handling Input Mismatch Exception
{
System.out.println("Input Mismatch");
}
catch (NumberFormatException e) //Handling this exception mean any value not in the required format
{
System.out.println("Number format Exception ");
}
}
}
catch(FileNotFoundException e) //If file is not found
{
System.out.println("File not found");
}
try {
PrintWriter outputStream=new PrintWriter("sales.txt");
outputStream.println("new file");
outputStream.close();
}catch(FileNotFoundException e)
{System.out.println("file not found");
}
}
}
Explanation / Answer
You can usejava.io.BufferedReaderto read a file line by line and copy the contents to the seperate.txt file
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.