View Single Post
  #3 (permalink)  
Old 03-24-2008, 05:28 PM
Javashak Javashak is offline
Member
 
Join Date: Mar 2008
Posts: 3
Javashak is on a distinguished road
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;


public class FileIO extends JFrame implements ActionListener{

private int lineCount;
private String[] A;
private String[] nodeLine = new String[2];
private int[] L;
private int[] R;

private JTextArea display=new JTextArea();
private JButton read=new JButton("Read From File");

private JTextField nameField=new JTextField(20);
private JLabel prompt=new JLabel("Filename:",JLabel.RIGHT);
private JPanel commands=new JPanel();

public FileIO()
{super("Read data from file");
read.addActionListener(this);

commands.setLayout(new GridLayout(2,2,1,1));
commands.add(prompt);
commands.add(read);
commands.add(nameField);

//for graphing

display.setLineWrap(true);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add("North",commands);
this.getContentPane().add(new JScrollPane(display));
this.getContentPane().add("Center",display);
}
public void readTextFile(JTextArea display,String fileName) {

try {BufferedReader coStream=new BufferedReader(new FileReader(fileName));

String line=coStream.readLine();
int i = 1;
lineCount = 1;
while(line!=null) {
line=coStream.readLine();
lineCount = lineCount + 1;
i = i + 1;
}
coStream.close();
}

catch(FileNotFoundException e)
{display.setText("IO ERROR:File NOT Found:" + fileName + "\n");

e.printStackTrace();
}
catch(IOException e)
{
display.setText("IO ERROR:" +e.getMessage()+"\n");
e.printStackTrace();
}

A = new String[lineCount];
L = new int[lineCount];
R = new int[lineCount];

try {BufferedReader inStream=new BufferedReader(new FileReader(fileName));

String line=inStream.readLine();
A[0] = line;
int i = 1;
while(line!=null) {
display.append(line+"\n");
line=inStream.readLine();
A[i] = line;
i = i + 1;
}
inStream.close();
}

catch(FileNotFoundException e)
{display.setText("IO ERROR:File NOT Found:" + fileName + "\n");

e.printStackTrace();
}
catch(IOException e)
{
display.setText("IO ERROR:" +e.getMessage()+"\n");
e.printStackTrace();
}

for (int i = 0; i < (lineCount-1); i++) {
nodeLine = A[i].split(" ");
if (nodeLine[0].equals("-")) {
nodeLine[0] = "0";
}
if (nodeLine[1].equals("-")) {
nodeLine[1] = "0";
}
L[i] = Integer.parseInt(nodeLine[0]);
R[i] = Integer.parseInt(nodeLine[1]);
}

printStuff();
calculate();

}
public void actionPerformed(ActionEvent evt)
{
String fileName=nameField.getText();
if(evt.getSource()==read)
{
display.setText("");

readTextFile(display, fileName);

}
else System.out.println("Hello World");
}

public void printStuff() {
for (int i = 0; i < (lineCount-1); i++) {
System.out.println(L[i]);
}
System.out.println("---");
for (int i = 0; i < (lineCount-1); i++) {
System.out.println(R[i]);
}
}

public void calculate() {

}


}
Reply With Quote