Reading from a file using an array and textarea
Hi,
I am writing a program that will read a file from in the classes folder. The program will then sort the information. As I have it now, I am able to load the file into the textarea. The only problem with that is when I tried to append text to add "enters" at the appropriate places, it only shows up as "/n" and not a new line. That is problem num 1.
Problem number two is that the program will not draw the array. I want to make an array because that is what I will be using to sort the information. I will then put the new information into the textarea. I need to draw the string however to make sure the array exist, and to test that it sorts properly. Here is my code.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
public class Assignement3 extends Applet {
Button load, save, sort;
TextField filename;
TextArea filecontent;
String filewords[] = new String[20];
private boolean isSort;
private boolean isStart = true;
private boolean isb;
int s=0;
int b=0;
int l=0;
public void init() {
load= new Button("Load");
save= new Button("Save");
sort= new Button("Sort");
add(sort);
add(save);
add(load);
load.addActionListener (new TempListener());
save.addActionListener (new TempListener());
sort.addActionListener (new TempListener());
filename = new TextField("test.txt",40);
add(filename);
filecontent = new TextArea(10,40);
add(filecontent);
}
public void paint (Graphics g){
if (isStart)
{
g.drawString("Please enter the name of the file to be loaded then sorted.",200,200);
}
if (isb)
{
g.drawString("Thank you!",200,200);
}
if (isSort)
{
g.drawString(filewords[0],500,500);
}
}
public class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == load)
{
isb=true;
isStart=false;
isSort=false;
repaint();
try{
for(s=0;s>0;s++);
{
BufferedReader filetext = new BufferedReader (new FileReader(filename.getText()));
String line;
//loop until we have no more lines
while ((line = filetext.readLine()) != null) {
filecontent.setText(filecontent.getText() + line);
//adds a line in the text area after every line in the file.
//mimics the files shape.
filecontent.append(" /n");
//Problem #1
}
filetext.close();
}
}
catch(Exception e) {}
}
else if (event.getSource() == save)
{
try{
FileWriter a = new FileWriter(filename.getText());
a.write(filename.getText());
a.close();
}
catch(Exception e){
e.printStackTrace();
}
}
else if (event.getSource() == sort)
{
try{
BufferedReader filestring = new BufferedReader (new FileReader(filename.getText()));
for(b=0;b<20;b++);
{
filewords[b] = filestring.readLine();
}
filestring.close();
}
catch(Exception e){}
isSort=true;
isb=false;
isStart=false;
repaint();
}
}
}
}
Any help would be appreciated.