im trying to add the sums of a .txt file into a variabe named total which i decl
ID: 3531920 • Letter: I
Question
im trying to add the sums of a .txt file into a variabe named total which i declared a double. for some reason its giving me an error saying that a,b and c might not have been initialized, but i did. any help is appreciated
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
public class Sales3
{
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;
double b;
double c;
String filename = "sales.txt";
System.out.println("name"+" "+"service"+" "+"lodge"+" "+"costs"+" "+"dinner"+" "+"misc"+" "+"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
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
//misc=;
i++;
System.out.print(df.format(misc) + " ");
}
else if(i==6)
{
total=a+b+c;
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");
}
}
}
Explanation / Answer
initialize a,b,c to 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.