Hello all
I require a bit of help with my program I am attempting to build. The final purpose of it is to allow a user to, through a keypad on a GUI enter a string of 5 numbers (a zip code), which will be compared to a set of data and output relevant information that pertains to that area. Right now, I think I am having trouble getting the data into the arrays themselves I have set up. I am pretty sure the file is being read in because when I do a System.out.println() inside the while loop ( the loop I have set up to load data from the text files into the arrays) using inputFile.nextLine, or Array[any number] I get a value. But when I do System.out.println(Array[any number]) outside of the while loop, or use i outside or inside the loop I get nothing but null values. To anyone who can help, thankyou.
Here is the gui
import java.awt.* ; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class MichiganZip extends JFrame { //Variables JTextField zipField; JButton zero, one, two, three, four, five, six, seven, eight, nine, submit, clear; JPanel keyPad, visual; String zip = ""; public MichiganZip() { ZipManager zipmanager = new ZipManager(); keyPad = new JPanel(); keyPad.setLayout(new GridLayout(4, 3)); visual = new JPanel(); visual.setLayout(new FlowLayout()); zipField = new JTextField(6); zipField.setEditable(false); visual.add(zipField); zero = new JButton("0"); one = new JButton("1"); two = new JButton("2"); three = new JButton("3"); four = new JButton("4"); five = new JButton("5"); six = new JButton("6"); seven = new JButton("7"); eight = new JButton("8"); nine = new JButton("9"); submit = new JButton("Submit"); clear = new JButton("clear"); zero.addActionListener(new ButtonListener()); one.addActionListener(new ButtonListener()); two.addActionListener(new ButtonListener()); three.addActionListener(new ButtonListener()); four.addActionListener(new ButtonListener()); five.addActionListener(new ButtonListener()); six.addActionListener(new ButtonListener()); seven.addActionListener(new ButtonListener()); eight.addActionListener(new ButtonListener()); nine.addActionListener(new ButtonListener()); submit.addActionListener(new ButtonListener()); clear.addActionListener(new ButtonListener()); keyPad.add(one); keyPad.add(two); keyPad.add(three); keyPad.add(four); keyPad.add(five); keyPad.add(six); keyPad.add(seven); keyPad.add(eight); keyPad.add(nine); keyPad.add(submit); keyPad.add(zero); keyPad.add(clear); Container Gui = getContentPane(); Gui.setLayout(new GridLayout(1,1)); Gui.add(keyPad); Gui.add(visual); setTitle("Michigan Zips") ; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; setSize(500, 300) ; setVisible(true) ; } private class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ String actionCommand = e.getActionCommand(); if(actionCommand.equals("1")) { zip += "1"; zipField.setText(zip); } if(actionCommand.equals("2")) { zip += "2"; zipField.setText(zip); } if(actionCommand.equals("3")) { zip += "3"; zipField.setText(zip); } if(actionCommand.equals("4")) { zip += "4"; zipField.setText(zip); } if(actionCommand.equals("5")) { zip += "5"; zipField.setText(zip); } if(actionCommand.equals("6")) { zip += "6"; zipField.setText(zip); } if(actionCommand.equals("7")) { zip += "7"; zipField.setText(zip); } if(actionCommand.equals("8")) { zip += "8"; zipField.setText(zip); } if(actionCommand.equals("9")) { zip += "9"; zipField.setText(zip); } if(actionCommand.equals("0")) { zip += "0"; zipField.setText(zip); } if(actionCommand.equals("clear")) { zip = " "; zipField.setText(zip); } } } public static void main(String[] args){ MichiganZip search = new MichiganZip(); } }
Here is the Zip code manager
import java.io.*; import java.util.*; import javax.swing.JOptionPane; public class ZipManager { //Variables final static int NUM_ELM = 1300; static String Zip[] = new String[NUM_ELM]; static double cordOne[] = new double[NUM_ELM]; static double cordTwo[] = new double[NUM_ELM]; static String State[] = new String[NUM_ELM]; static String City[] = new String[NUM_ELM]; int i = 0; File file; Scanner inputFile; //attempt to open the file { try { file = new File("zipMI.txt"); inputFile = new Scanner(file); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "File not found" + e); } } { while (inputFile.hasNext()) { Zip[i] = inputFile.next(); cordOne[i] = inputFile.nextDouble(); cordTwo[i] = inputFile.nextDouble(); State[i] = inputFile.next(); City[i] = inputFile.next(); i++; } } public static void main(String[] args){ for(int i = 0; i < NUM_ELM; i++ ){ System.out.println(Zip[i]);} } }
And the text file with the zip codes is attached as a text file.