I am working on a homework assignment where I have to read some annual income numbers from a text file, then determine how much tax is owed assuming the tax bracket is 30%. I am having a couple issues with my code. First: when the message dialog trys to print the array that stores the owed tax it displays "[D@a77106". Second: I have noticed that the code is not reading the first line in the file. I found this out from changing the meassage dialog statement to print only one element of the array i.e. owedTax[1]. Any help would be much appreciated.

Here is the code:

package incomeTax;
import java.io.*;
import javax.swing.JOptionPane; // needed for dialog interaction


public class IncomeTax {
	public static void main (String[] args) throws IOException
	{		
		String fileName;
		fileName = JOptionPane.showInputDialog("Please enter the file name: ");
		ReadIncomeFile(fileName);
	}
	public static void ReadIncomeFile(String fileName) throws IOException
	{
		//String userIncome;
		try
		{
			FileReader freader = new FileReader(fileName);
			BufferedReader inputFile = new BufferedReader(freader);
			
			String record = null; 
			double userIncome = 0;
			double owedTax[] = new double[fileName.length()];;

			   for (int index = 0; (record = inputFile.readLine()) != null; index++ ) { 
				   
				   userIncome = Double.parseDouble(record);
				   owedTax[index] = userIncome * .30;
				   
			   } 
			   JOptionPane.showMessageDialog(null, owedTax);	     
	        
		}		
		
		catch (FileNotFoundException e)
		{
			JOptionPane.showMessageDialog(null, "file not found");
		}
	}
}

I have also attached the text file that I have been using to test the code.

Attachments
25000.0
35000.0
45000.0

It actually works, but the way you write out your array is wrong. You cannot print the array object out right if it is not char[]. You need to iterate through your array before you print out.

// change your line 31 to
String taxes = ""
for (int i=0; i<owedTax.length; i++) {
  taxes += owedTax[i]+"\n";
}
JOptionPane.showMessageDialog(null, taxes);

Though, your code may not work properly due to the way you create your owedTax array size. You used the file name length which may or may not be correct (size may be too small to hold all incoming data). However, I don't know what your instructor tells you or instruct you. Is it in the requirement? Or you can just create whatever size you want but make sure that it can hold the whole data?

You could do 2 things - either use a dynamic array (ArrayList) or read through the whole file once to count how many data in the file before creating your array.

Edited 5 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.