I've seen a lot of websites that describe how to append text into a textarea
, but is there a way to grab data from a whole .txt file and display it in the textarea?
I've been playing around with various things to put into a line like this:
outputTextArea.append(????);
But no luck yet.
Super new to java and not so good with the terminology but I hope I explained my question well.
EDIT: It won't let me respond to my own question, so I'm just going to put it up here.
I am using JTextArea, but I guess I'm a little overwhelmed. I'm not entirely sure what I'm seeing, but is this what you were talking about?
public FileReader(String fileName);
I've got this so far.
FileWriter fwriter = new FileWriter("BigMoneys.txt", true);
PrintWriter outputFile = new PrintWriter(fwriter);
for (int year = 1; year<= 10; year++)
{
amount = principal * Math.pow(1.0 + rate, year);
outputFile.append( year + "\t" + moneyFormat.format(amount) + "\n");
}
outputFile.close();
JOptionPane.showMessageDialog(null, outputTextArea, "Compound Interest", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
The rest of this was covered in my textbook fairly well, and all makes decent sense, but I just checked and the book has nothing about FileReader.
All I know is I'm supposed to use what's in outputFile and append it to outputTextArea. I'm honestly not trying to get you to do my homework for me, I'm just really really lost.
So if I am supposed to use that line above, could I do this?
FileReader(String fwriter)
EDIT2: This is what I've got so far. Please tell me if I'm on the right track.
import java.util.Scanner;
import java.io.*;
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;
public class Interest3
{
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 input;
String filename;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the filename: ");
filename = keyboard.nextLine();
//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);
outputTextArea.setText("Year\tAmount on deposit\n");
//open new file for writing
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
outputFile.append( year + "\t" + moneyFormat.format(amount) + "\n");
}
outputFile.close();
//open file for reading
File file = new File(filename);
FileReader rd = new FileReader(file);
outputTextArea.append(rd);
//display results
JOptionPane.showMessageDialog(null, outputTextArea, "Compound Interest", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
I've got an error on the line with outputTextArea.append(rd);
that says "append(java.lang.String) in javax.swing.JTextArea cannot be applied to (java.io.FileReader)", so I'm obviously missing something down there.
EDIT3: Aaaand I think I've got it! Thanks for everyone's help. Case closed, goodnight :)