Results 1 to 4 of 4
  1. #1
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default String array from file to ComboBox

    Hi.
    I have a file: myFile.txt
    In that file is in every line: name + '\t' + numberOfYears.

    I want to get String array of names and put that names to be ComboBox items.

    I'm thinking like that:
    1. make class ReadFromFile
    2. in that class write a function getNames which opens a file, read line by line
    and get from each line name and put it in string array. This function return that string array.
    3. make class MyPanel extends JPanel
    4. in MyPanel create object ReadFromFile and call function getNames.
    5. use return value from this function to populate items in ComboBox.

    please tell me your thinking, or share some ideas with me.
    Please give me advice, or tell me better way of thinking and planing, or tell me is something wrong with my planning.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,345
    Blog Entries
    7
    Rep Power
    20

    Default

    Your thinking is fine, go ahead.

    kind regards,

    Jos

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    With String[] it goes with null exception, so I change it to ArrayList.

    I've written first part of code in a small program:

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    import java.util.ArrayList;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class StudNames {
    	public static void main(String[] args) {
    		ReadFromFile namesOfStudents = new ReadFromFile();
    		ArrayList<String> students = namesOfStudents.getNames("students.txt");
    		System.out.println(students.toString());
    	}
    
    }
    
    class ReadFromFile {
    	int indexOfNames = 0;
    	ArrayList<String> names = new ArrayList<String>();
    	
    	public ArrayList<String> getNames(String filename) {
    		BufferedReader reader = null;
    		try {
    			int positionOfTab = 0;
    			String lineOfString;
    			reader = new BufferedReader(new FileReader(filename));
    			while((lineOfString = reader.readLine()) != null) {
    				positionOfTab = lineOfString.indexOf('\t');
    				
    				if(positionOfTab > 0)
    					names.add(lineOfString.substring(0, positionOfTab));
    				else
    					names.add("");
    			}
    		} catch(FileNotFoundException e) {
    			e.printStackTrace();
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    		
    		return names;
    	}
    }
    when I write the whole program will post it here.

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    program that reads data from the file "students.txt" (format of students.txt is:
    studentName + '\t' + age).
    Then it takes all the studentNames and places as ComboBox item.

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    import java.util.ArrayList;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.swing.JFrame;
    import javax.swing.JComboBox;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class StudNames {
    	public static void main(String[] args) {
    		ReadFromFile namesOfStudents = new ReadFromFile();
    		
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				StudFrame frame = new StudFrame("Students names");
    				frame.showGUI();
    			}
    		});
    	}
    
    }
    
    class StudFrame extends JFrame {
    	PanelWithComboBox panel = new PanelWithComboBox();
    	
    	public StudFrame(String name) {
    		setTitle(name);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300,300);
    		add(panel);
    	}
    	
    	public void showGUI() {
    		setVisible(true);
    	}
    }
    
    class PanelWithComboBox extends JPanel {
    	JComboBox comboBox;
    	
    	ReadFromFile rff = new ReadFromFile();
    	
    	
    	public PanelWithComboBox() {
    		ArrayList<String> listOfNames = rff.getNames("students.txt");
    		comboBox = new JComboBox(listOfNames.toArray());
    		add(comboBox);
    	}
    }
    
    class ReadFromFile {
    	int indexOfNames = 0;
    	ArrayList<String> names = new ArrayList<String>();
    	
    	public ArrayList<String> getNames(String filename) {
    		BufferedReader reader = null;
    		try {
    			int positionOfTab = 0;
    			String lineOfString;
    			reader = new BufferedReader(new FileReader(filename));
    			while((lineOfString = reader.readLine()) != null) {
    				positionOfTab = lineOfString.indexOf('\t');
    				
    				if(positionOfTab > 0)
    					names.add(lineOfString.substring(0, positionOfTab));
    				else
    					names.add("");
    			}
    		} catch(FileNotFoundException e) {
    			e.printStackTrace();
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    		
    		return names;
    	}
    }

Similar Threads

  1. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  2. Replies: 0
    Last Post: 03-22-2010, 06:45 AM
  3. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  4. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •