whats wrong with my program??
I am writing a program that reads data from a file, converts it to integers and than displays 30% of the value...but i cannot get it to work...can someone give me a tip on this please.....
Code :
public class K {
public static void main (String[] args) throws IOException
{
int value;
String fileName;
String income;
double finalList=0;
fileName=JOptionPane.showInputDialog("Enter the file name: ");
try
{
double tax = 0;
FileReader freader=new FileReader(fileName);
BufferedReader inputFile= new BufferedReader(freader);
income= inputFile.readLine();
value=Integer.parseInt(income);
while(income!=null)
{
tax=(value)*.30;
finalList+=tax+'\n';
income=inputFile.readLine();
tax=Double.parseDouble(income);
}
JOptionPane.showMessageDialog(null, tax+'\n');
inputFile.close();
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "file not found");
}
}
}
Re: whats wrong with my program??
Quote:
I am writing a program that reads data from a file, converts it to integers and than displays 30% of the value...but i cannot get it to work...can someone give me a tip on this please.....
What happens when you run it?
Re: whats wrong with my program??
I think the logic in your program is somewhat flaw.
You only read the value from the first line of the file you open and after that you loop through each line in the file reading in each line and storing it in the tax variable which then gets overwritten in the next while cycle by value x 0.30, no matter what the tax you add to the finalList will always be the same. On another note finalList is a primitive double and you try to increase it and add \n to the end?
Should that not just read.
// Json
Re: whats wrong with my program??
Quote:
Originally Posted by
jrodriguo
can someone give me a tip on this please.....
your while loop was wrong when the file contained multiple lines. to read multiple line use my while loop.
Code :
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;
public class TestClass {
public static void main (String[] args) throws IOException
{
int value;
String fileName;
String income;
double finalList=0;
String allIncome = "";
fileName=JOptionPane.showInputDialog("Enter the file name: ");
try
{
double tax = 0;
FileReader freader=new FileReader(fileName);
BufferedReader inputFile= new BufferedReader(freader);
while((income= inputFile.readLine())!=null)
{
value = Integer.parseInt(income);
tax=value / 3.0;
finalList+=tax;
allIncome+=tax + "\n";
}
JOptionPane.showMessageDialog(null, allIncome);
inputFile.close();
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "file not found");
}
}
}
have fun.
Re: whats wrong with my program??
thanks everyone...this worked out for me great...really appreciate everyone's help