Results 1 to 3 of 3

Thread: Arraylist

  1. #1
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default Arraylist

    Hello,
    I have an arraylist and i'm trying to put the contents in a Jtable under column names First Name, Last Name, School ID, Gender.

    The code below extracts the values of First Name, Last Name, School ID, and Number from a file. I am trying to put these info in a Jtable but i don't know if and how to use the default table model or abstract table model.
    Please Help.

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    
    
    public class ReadFile {
    	private static String output = "";
    	//static List list = new ArrayList(); 
    	 static ArrayList<String> list = new ArrayList<String>();
    	
        public static void main(String[] args) {
      
            //
            // Create an instance of File for data.txt file.
            //
            File file = new File("data.txt");
            
             
          
            try {
                
                Scanner scanner = new Scanner(file);
               
                //output = "";
                while (scanner.hasNextLine()) {
                    String str = scanner.nextLine();
                    //System.out.println(line);
                    if (parseString(str) != null) {
                    // output += parseString(str);
                     list.add(parseString(str));
                           
                    } 
                   
                }
                
                System.out.println(list);
                System.out.println();
                
                for(int i=0;i<list.size();i++)
                {
                System.out.println(list.get(i));
                }
    
               
    
                //System.out.println(list);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            
        }
        
        private static String parseString(String line) {
        
            if (line.contains("First Name") || line.contains("Last Name") || line.contains("School ID")|| line.contains("Number")) {
                        line = line.substring(line.indexOf("=") + 1, line.length());
                //return line + "\n";
                return line;
               
            	
            } else {
                return null;
            }
        } 
        
     }
    I have a txt file with the following:

    ID = 0
    First Name=Joe
    Last Name=Doe
    Age=21
    Gender=Male
    School = middle school
    StudentID = 1234
    Address = some address.

    Thank You in advance for your help
    Last edited by BeeGee; 09-15-2010 at 02:28 PM. Reason: Additional info

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Arraylist

    JTable table = new JTable(data, col);
    String data[][] = {{"Mike","15"}}
    String col[] = {"Name","Age"}

    DefaultTableModel model = new DefaultTableModel(data, col);
    JFrame frame = new JFrame("PROGRAM FRAME");
    JPanel panel = new JPanel();

    heres all of the basics, be sure to look up a full functioning example online

    Basicly what you need to do is create a frame/panel then add a jtable which adds a model that adds the strings(data)

Similar Threads

  1. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 04:34 PM
  2. Help with arraylist
    By alexisasoxfan in forum New To Java
    Replies: 12
    Last Post: 05-07-2010, 07:07 AM
  3. Arraylist
    By MaN-GreaSe in forum New To Java
    Replies: 2
    Last Post: 04-24-2010, 02:30 AM
  4. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  5. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 08:30 PM

Posting Permissions

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