Passing values through to text fields

Hi there,

I am little rusty with java and I am trying to program a filechooser application that:

1)Opens and reads in a text file of integers

2)Stores these integers in an array

2)Performs various operations on the array (average, highest number etc)

3)Displays the results in text boxes in the gui.

I haven't done much java in a year or so. Could someone give me some pointers.

Here is the code I have so far. It can display the number of integers in a file but I can't pass the avg value to the text boxes.

Thanks.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.util.*;

import java.lang.Integer;

publicclass GroupProjectextends JFrame{

JFileChooser _fileChooser =new JFileChooser();

JTextField_fileNameTF =new JTextField(10);

JTextField_hiNum =new JTextField(3);

JTextField_freq1 =new JTextField(2);

JTextField_secondhiNum =new JTextField(3);

JTextField_freq2 =new JTextField(2);

JTextField_loNum =new JTextField(3);

JTextField_freq3 =new JTextField(2);

JTextField_secondloNum =new JTextField(3);

JTextField_freq4 =new JTextField(2);

JTextField_avgNum =new JTextField(3);

JTextField_numCount =new JTextField(2);

JTextField_sortDisp =new JTextField(2);

GroupProject(){

_fileNameTF.setEditable(false);

_numCount.setEditable(false);

JPanel content =new JPanel();

content.setLayout(new GridLayout(6,2));

content.add(new JLabel("File:"));

content.add(_fileNameTF);

content.add(new JLabel("Number of Integers"));

content.add(_numCount);

content.add(new JLabel("Highest Value Integer"));

content.add(_hiNum);

content.add(new JLabel("Frequency"));

content.add(_freq1);

content.add(new JLabel("Second Highest Value Integer"));

content.add(_secondhiNum);

content.add(new JLabel("Frequency"));

content.add(_freq2);

content.add(new JLabel("Lowest Value Integer"));

content.add(_loNum);

content.add(new JLabel("Frequency"));

content.add(_freq3);

content.add(new JLabel("Second Lowest Value Integer"));

content.add(_secondloNum);

content.add(new JLabel("Frequency"));

content.add(_freq4);

content.add(new JLabel("Average of Integers"));

content.add(_avgNum);

content.add(new JLabel("Display Sorted Integers"));

content.add(_sortDisp);

JMenuBar menubar =new JMenuBar();

JMenufileMenu =new JMenu("File");

JMenuItem openItem =new JMenuItem("Open");

openItem.addActionListener(new OpenAction());

menubar.add(fileMenu);

fileMenu.add(openItem);

this.setJMenuBar(menubar);

this.setContentPane(content);

this.setTitle("Practice Group Project");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.pack();

this.setLocationRelativeTo(null);

}

publicvoid fillOutForm(ArrayList ints){

Collections.sort(ints);

// int[] a = (int[])ints.toArray(typeof(int));

//ArrayList = Integer.parseInt(ints.get(0).toString());

//int [] a = (int[])ints.toArray(typeof(int));

Integer a[] = (Integer[]) ints.toArray(new Integer[0]);

_numCount.setText("" + ints.size());

_avgNum.setText("" +"not working");

//int max = Integer.parseInt(ints.get(ints.size()-1));

//_hiNum.setText("" + max);

}

// getFrequency takes in an array and a number and searches the array for that number.

// Each time this happens increment a variable then return the variable.

publicvoid getFrequency(){

}

// sortArray sorts the array, reads the last number (highest), and calls getFrequency with this number. Fills out text boxes

// then read the array from the end, inwards, until it gets a different number. calls getFreq on this number, and

// fills out the boxes then reads the first number (lowest) in the array, and calls getFreq on this number. fill out the boxes.

// then read the array one cell at a time, going forwards until it gets a different number. call getFreq on this number and

// then fill out text boxes.

publicvoid sortArray(){

}

// avgArray adds up all the numbers in the array and divides by array size

publicstaticint avgArray(int [] ints)

{

int avg = 0;

for (int i = 0; i < ints.length; ++i)

{

avg += ints[i];

}

return avg;

}

class OpenActionimplements ActionListener{

publicvoid actionPerformed(ActionEvent ae){

int retval = _fileChooser.showOpenDialog(GroupProject.this);

if (retval == JFileChooser.APPROVE_OPTION){

File file = _fileChooser.getSelectedFile();

ArrayList ints = getIntArray(file.getAbsolutePath());

_fileNameTF.setText(file.getName());

fillOutForm(ints);

}

}

//Reads through a given file and fills out an ArrayList with the integers found in that file/

public ArrayList getIntArray(String filename){

ArrayList ints =new ArrayList();

try{

BufferedReader reader =new BufferedReader(new FileReader(filename));

String line = reader.readLine();

while(line !=null){

ints.add(Integer.parseInt(line));

line = reader.readLine();

}

}

catch (Exception e){ System.out.println("Error");}

{

return ints;

}

}

}

publicstaticvoid main(String[] args){

JFrame window =new GroupProject();

window.setVisible(true);

}

}

Message was edited by:

Duckula85

Message was edited by:

Duckula85

[10220 byte] By [Duckula85a] at [2007-10-3 8:29:25]
# 1
Anyone?
Duckula85a at 2007-7-15 3:36:17 > top of Java-index,Java Essentials,Java Programming...
# 2

Duckula85-

First, you need to create a new Integer object for each 'int' parsed from the input file in your method 'getIntArray(..)'.

For your method 'avgArray(..)', I would change the argument from an 'int[]' back to an ArrayList of the Integers. Iterate through the ArrayList to get the sum. Don't forget to divide by 'ints.size()'.

Hope this helps.

mikeconn4a at 2007-7-15 3:36:17 > top of Java-index,Java Essentials,Java Programming...