Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: File Input

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default File Input

    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.
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: File Input

    Nothing prints out when I execute the code???

    I get a keypad in a GUI.

    Where do you see the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] File input help
    By Freezer Boy in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 6th, 2012, 05:39 PM
  2. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  3. Input file Not Opening =/
    By dannybrgc in forum What's Wrong With My Code?
    Replies: 10
    Last Post: April 11th, 2012, 05:21 PM
  4. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 12:05 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM