Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Jtable - reading from file and splitting data

$
0
0
I know how to read text from a file line by line. But I would like to get data from a file to the Jtable using a better method such as a for loop or a while loop. Basically, the jtable has 4 columns. Name, Hotel, Cost, Country. I want the jtable to read every four lines into each row.

filename.txt
----------
Ramal
Hotel1
100
UK
Bren
Hotel2
200
Spain
Dave
Hotel3
300
France


For example, first row should look like this:

Ramal Hotel1 100 UK



This is what i have done so far.




button3.addActionListener(new ActionListener(){
public  void actionPerformed(ActionEvent e){

		try{
		
		FileInputStream fstream = new FileInputStream("filename.txt");
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));	
        
        pane.setVisible(true);
        
        String line1 = br.readLine();
		String line2 = br.readLine();
		String line3 = br.readLine();
		String line4 = br.readLine();
		String line5 = br.readLine();
		String line6 = br.readLine();
		String line7 = br.readLine();
		String line8 = br.readLine();
		
			
		String a = (line1.toString());
		String b = (line2.toString());
		String c = (line3.toString());
		String d = (line4.toString()); 
		combobox1.getSelectedItem();
		String e = (line5.toString());
		String f = (line6.toString());
		String g = (line7.toString());
		String h = (line8.toString()); 
		combobox1.getSelectedItem();

      	        String[] data1 = {(a), (B)/>/>/>/>/>/>, (c), (d)};
      	        String[] data2 = {(e), (f), (g), (h)};

      	
      	        model.addRow(data1);
      	        model.addRow(data2);
      	
		in.close();
		
		}catch (Exception e){
			
		System.err.println("Error: " + e.getMessage());
		
		}

   	}
       });



Viewing all articles
Browse latest Browse all 51036

Trending Articles