Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to put things from a textfield into an integer array in java
    text
    copied!<p>I have a program I am trying to put into a GUI and I have no Idea how to get something that a user puts into the textfield into an array Here is my code so far: </p> <pre><code>import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Arrays; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class GUIMain extends JFrame{ @SuppressWarnings("unused") private static JLabel label3; @SuppressWarnings("unused") private static JLabel label2; @SuppressWarnings("unused") private static JLabel label4; @SuppressWarnings("unused") private static JLabel label; private static JTextField text; private static final long serialVersionUID = 1L; private static int [] array={98,99,100}; @SuppressWarnings("static-access") public GUIMain(){ super("Math Converter"); //this.array = array; Arrays.sort(array); //System.out.println("The median is : "+median(array));//prints Median //System.out.println("The mode is : "+mode(array));//prints Mode //System.out.println("The mean is : "+mean(array));//prints the mean //pack(); setSize(600,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setResizable(true); setVisible(true); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(10,10,10,10));// JTextField text = new JTextField(10); JLabel label = new JLabel(toStringMedian(array)); JLabel label2 = new JLabel(toStringMode(array)); JLabel label3 = new JLabel(toStringMean(array)); JLabel label4 = new JLabel("How many numbers do you wan't in your array?"); Container content = getContentPane(); FlowLayout layout = new FlowLayout(); layout.setAlignment(FlowLayout.CENTER); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); add(panel1); content.setLayout(layout); content.add(panel); content.add(panel1); content.add(panel2); panel.setLayout(new GridLayout(10,2,10,10));//10,2,10,10 panel1.setLayout(new GridLayout(1,2,1,10));//1,2,1,10 panel2.setLayout(new GridLayout(1,4,10,10));//1,4,10,10 panel.add(label4); panel.add(text); panel.add(label); panel.add(label2); panel.add(label3); text.addActionListener(new Action()); JMenuBar menu = new JMenuBar(); setJMenuBar(menu); JMenu file = new JMenu("File"); menu.add(file); JMenu Help = new JMenu("Help"); menu.add(Help); JMenuItem About = new JMenuItem("About"); Help.add(About); JMenuItem Exit = new JMenuItem("Exit"); file.add(Exit); Exit.addActionListener(new Exit()); this.label = label; this.label2 = label2; this.label3 = label3; this.label4 = label4; this.text = text; } public static void main(String[] args) throws Exception { new GUIMain(); } public static int mode(int[] a) { int previous = a[0];//sets previous to array at 0 int popular = a[0];//Initializes popular int count = 1;//initializes count int maxCount = 1;//initializes max count for (int i = 1; i &lt; a.length; i++) {//goes through the array if (a[i] == previous)//tests to see if array at i is equal to previous count++;//adds one every time this is true else {//if not then it will go through the next statement if (count &gt; maxCount) {//tests to see if count is greater than max count popular = a[i-1];//sets popular to a at i-1 maxCount = count;//sets max count to count } previous = a[i];//sets previous to a at i count = 1;//sets count to one when finished then it goes through the loop again } } return count &gt; maxCount ? a[a.length-1] : popular;//returns only if count is greater than max count the integer popular } public static int median(int[] a){ int median= 0 ;//declares median if(a.length%2==0)//tests to see if the length of the array is even median = (a[(int)a.length/2] + (int)a[a.length/2+1])/2;//if it is even then it takes the averages of the two numbers in the middle else{ median = a[a.length/2];//finds the number in the middle of the array } return median;//returns the value of median } public static int mean(int[]a){ int mean = 0; for(int i = 0; i&lt;a.length;i++){ mean =(int)(a[i]+a [i]-1)/a.length;//finds the average of the elements of the array } return mean;//returns the value of mean } public static String toStringMedian(int[]a){ return"This is the Median : "+median(a); } public static String toStringMean(int[]a){ return"This is the Mean : "+mean(a); } public static String toStringMode(int[]a){ return"This is the Mode : "+median(a); } static class Exit implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } } static class Action implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { int x = 0; int y = 0; int z = 0; if(e.getSource()==text){ } } } } </code></pre> <p>I have tried to change it before with something like this int Input = Integer.parseInt(text.getText()); but that takes too long is there another way?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload