loading numbers from a file and calculating sd, mean, max
I need help to load numbers from a text file from path to callculate the mean and standard deviation, max, min and median and then save the data to another file.
I need help to load numbers from a text file from path to callculate the mean and standard deviation, max, min and median and then save the data to another file.
> I need help to load numbers from a text file from
Sun's IO tutorial:
http://java.sun.com/docs/books/tutorial/essential/io/
> path to callculate the mean and standard deviation,
> max, min and median and then save the data to another
> file.
Commons Math is a library of lightweight, self-contained mathematics and statistics components addressing the most common problems not available in the Java programming language...
http://jakarta.apache.org/commons/math/
I have this code but it only loads the data to a text area. How do I manipulate this into an arraylist suppose i dont know the file length. If I load any file and calculate it will return those values
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
/**
*
* @author zyu06wvu
*/
public class Statistica extends JFrame
implements ActionListener {
// initialising components
private BufferedReader inFile;
private Button calculate;
private JTextArea area;
private JTextField mean, median, sd, max, min,
fileName1;
private String fileName;
private JLabel mean1, median1, sd1, max1, min1,
fileLabel1;
public static void main (String[] args)
{
Statistica frame = new Statistica();
frame.setSize(600, 500);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setVisible(true);
window.setLayout(new FlowLayout());
fileLabel1 = new JLabel("File Name");
window.add(fileLabel1);
fileName1 = new JTextField(40);
fileName1.setText("");
window.add(fileName1);
area = new JTextArea("", 20, 45);
JScrollPane scrollPane = new JScrollPane(area);
window.add(scrollPane);
calculate = new Button("Calculate");
window.add(calculate);
calculate.addActionListener(this);
mean1 = new JLabel("means : ");
window.add(mean1);
median1 = new JLabel("Median is :");
window.add(median1);
sd1 = new JLabel("Standard Deviation is : ");
window.add(sd1);
max1 = new JLabel("Max is : ");
window.add(max1);
min1 = new JLabel("Min is : ");
window.add(min1);
}
public void actionPerformed(ActionEvent e) {
String StandardDeviation = null;
if(e.getSource() == calculate){
try {
inFile = new BufferedReader(
new FileReader(fileName1.getText()));
area.setText( "");
String line;
ArrayList file = new ArrayList();
while( (line = inFile.readLine()) != null){
area.append(line+"\n");
mean1.setText("mean " + StandardDeviation);
}
inFile.close();
}
catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Cannot locate file" +
ex.toString());
}
}
}
}
Step number 1) Take all that swing code and put it to the side. You want to get this baby up and running with non-swing console programming before attaching it to Swing.
2) What type of numbers do you have? Are they all ints? Read up on the Scanner class and how you can use it to extract numbers.
3) Read up on ArrayLists and Collections in the tutorials. ArrayLists are wonderful for handling numbers groups when you don't know the final size of the group.