4 Replies - 1280 Views - Last Post: 08 December 2012 - 10:55 PM Rate Topic: -----

#1 Walker1101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-December 12

HELP, opening a file then read and display the data!

Posted 06 December 2012 - 11:36 PM

I am stuck on my code to open the string file and read the data that is stored in the file. What am I missing for my code. Please help me!



import java.text.NumberFormat;     //class for numeric formatting
import java.util.Locale;	       //class for country-specific information
import javax.swing.JOptionPane;	  
import javax.swing.JTextArea;
import java.util.Scanner;
import java.io.*;

public class Interest 
{
    public static void main(String[] args) throws IOException
    	{
    	double amount,		//amount of deposit at end of each year 
    		   principal,	//initial amount before interest
    		   rate;		//rate of interest
  	    String filename;			//File name
  	    String filename2;			//Open file name
  	    String input;
  	    
        //Create a Scanner object for keyboard input.
		Scanner keyboard = new Scanner(System.in);
        
        //create NumberFormat for currency in US dollar format
    	NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( Locale.US );
    	
    	//create JTextArea to display output
    	JTextArea outputTextArea = new JTextArea();
    	
    	input = JOptionPane.showInputDialog("Please enter Principal: ");
    	principal = Double.parseDouble(input);
    	
    	input = JOptionPane.showInputDialog("Please enter Interest Rate (Format: 0.00) ");
    	rate = Double.parseDouble(input);
    	
    	//set first line of to display output
    	outputTextArea.setText("Year\tAmount on deposit\n");
    			
		//Get the filename.
		filename = JOptionPane.showInputDialog("Please enter a file name: ");
		
		//Open the file.
		PrintWriter outputFile = new PrintWriter(filename);
    	
    	//calculate amount on deposit for each of ten years
    	for (int year = 1; year <= 10; year++)
    	{
    		amount = principal * Math.pow(1.0 + rate, year);
    		
    		// append one line of text to outputTextArea
    		outputTextArea.append( year + "\t" +
    			moneyFormat.format(amount) + "\n");
    	
    			outputFile.println(year + "\t" + amount + "\n");
    	
    	}// end for
    	   	
    	//Open the filename.
		filename2 = JOptionPane.showInputDialog("Please enter your file name to open the file: ");
		
		File file = new File(filename2);
		Scanner inputFile = new Scanner(file);
		
		while (inputFile.hasNext())
		{			
			    inputTextArea.append( year + "\t" +
    			moneyFormat.format(amount) + "\n");

			inputFile.println(year + "\t" + amount + "\n");
		}
    	
    	System.exit(0);  //terminate the application
    	
    }//end of main
    
    
}//end of Interest.java


Is This A Good Question/Topic? 0
  • +

Replies To: HELP, opening a file then read and display the data!

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: HELP, opening a file then read and display the data!

Posted 07 December 2012 - 02:32 AM

This while loop (starting line 62) never reads data from the file:
while (inputFile.hasNext())
{			
    inputTextArea.append( year + "\t" +
        moneyFormat.format(amount) + "\n");

    inputFile.println(year + "\t" + amount + "\n");
}

Try collecting a line of data from the file with a statement like:

String lineOfData = inputFile.next(); // or nextLine()

and then use the lineOfData as needed.
Was This Post Helpful? 1
  • +
  • -

#3 Walker1101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-December 12

Re: HELP, opening a file then read and display the data!

Posted 07 December 2012 - 09:40 PM

So a loop is not needed?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: HELP, opening a file then read and display the data!

Posted 08 December 2012 - 12:32 AM

Yes, a while loop that contains useful statements is needed. Try adding a line like the one I suggested to the existing while loop, doing something with the line read, of course.
Was This Post Helpful? 0
  • +
  • -

#5 Walker1101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-December 12

Re: HELP, opening a file then read and display the data!

Posted 08 December 2012 - 10:55 PM

Ok I understand. Will I also need to replace the variables inside the loop?

		while (inputFile.hasNext())
		{			
		  String lineOfData = inputFile.next();	
                  inputFile.println(lineOfData + "\t" + lineOfData + "\n");
		}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1