can not read .txt file raw data into java table...

package readtext;

import java.io.*;// For input & output classes

import java.util.*;// For the Date class

import javax.swing.*;

import java.awt.*;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

publicclass inputextends JPanel{

privateboolean DEBUG =false;

public input(){

super(new GridLayout(1,0));

String[] columnNames ={"First Name",

"Last Name",

"Sport",

"# of Years",

"Vegetarian"};

Object[][] data ={

{"Mary","Campione",

"Snowboarding",new Integer(5),new Boolean(false)},

{"Alison","Huml",

"Rowing",new Integer(3),new Boolean(true)},

{"Kathy","Walrath",

"Knitting",new Integer(2),new Boolean(false)},

{"Sharon","Zakhour",

"Speed reading",new Integer(20),new Boolean(true)},

{"Philip","Milne",

"Pool",new Integer(10),new Boolean(false)}

};

final JTable table =new JTable(data, columnNames);

table.setPreferredScrollableViewportSize(new Dimension(500, 70));

if (DEBUG){

table.addMouseListener(new MouseAdapter(){

publicvoid mouseClicked(MouseEvent e){

printDebugData(table);

}

});

}

//Create the scroll pane and add the table to it.

JScrollPane scrollPane =new JScrollPane(table);

//Add the scroll pane to this panel.

add(scrollPane);

}

privatevoid printDebugData(JTable table){

int numRows = table.getRowCount();

int numCols = table.getColumnCount();

javax.swing.table.TableModel model = table.getModel();

System.out.println("Value of data: ");

for (int i=0; i < numRows; i++){

System.out.print("row " + i +":");

for (int j=0; j < numCols; j++){

System.out.print(" " + model.getValueAt(i, j));

}

System.out.println();

}

System.out.println("--");

}

/**

* Create the GUI and show it. For thread safety,

* this method should be invoked from the

* event-dispatching thread.

*/

privatestaticvoid createAndShowGUI(){

//Create and set up the window.

JFrame frame =new JFrame("SimpleTableDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

input newContentPane =new input();

newContentPane.setOpaque(true);//content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.pack();

frame.setVisible(true);

}

publicstaticvoid main(String[] args)

{

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

createAndShowGUI();

}

});

}

}

may i know that how can i read a set of raw data from .txt file into table object[ ] [ ] ?

Object[][] data ={

{"Mary","Campione",

"Snowboarding",new Integer(5),new Boolean(false)},

{"Alison","Huml",

"Rowing",new Integer(3),new Boolean(true)},

{"Kathy","Walrath",

"Knitting",new Integer(2),new Boolean(false)},

{"Sharon","Zakhour",

"Speed reading",new Integer(20),new Boolean(true)},

{"Philip","Milne",

"Pool",new Integer(10),new Boolean(false)}

};

i have try to use

BufferedReader in =new BufferedReader(

new FileReader("C:/Documents and Settings/seng/Desktop/testfile/VehicleNode.txt"));

String text;

while((text = in.readLine()) !=null)

but it's didn't work at all....

[9030 byte] By [wilfrid100a] at [2007-10-3 2:59:30]
# 1
What does "doesn't work" mean?
CeciNEstPasUnProgrammeura at 2007-7-14 20:49:06 > top of Java-index,Java Essentials,New To Java...
# 2

And IMO, Object[][] is at least 90% mis-designed, since it's usually something like DB columns that represent an entity. If you have an entity, you might as well have a class.

Feed the line to the class's c'tor, break it up, parse it, set the instance variables. Then add the new instance to a list. Next line...

CeciNEstPasUnProgrammeura at 2007-7-14 20:49:06 > top of Java-index,Java Essentials,New To Java...
# 3

package readtext;

import java.io.*;// For input & output classes

import java.util.*;// For the Date class

import javax.swing.*;

import java.awt.*;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

public class input extends JPanel {

private boolean DEBUG = false;

public input() {

super(new GridLayout(1,0));

String[] columnNames = {"First Name",

"Last Name",

"Sport",

"# of Years",

"Vegetarian"};

Object[][] data = {

{"Mary", "Campione",

"Snowboarding", new Integer(5), new Boolean(false)},

{"Alison", "Huml",

"Rowing", new Integer(3), new Boolean(true)},

{"Kathy", "Walrath",

"Knitting", new Integer(2), new Boolean(false)},

{"Sharon", "Zakhour",

"Speed reading", new Integer(20), new Boolean(true)},

{"Philip", "Milne",

"Pool", new Integer(10), new Boolean(false)}

};

final JTable table = new JTable(data, columnNames);

table.setPreferredScrollableViewportSize(new Dimension(500, 70));

if (DEBUG) {

table.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

printDebugData(table);

}

});

}

//Create the scroll pane and add the table to it.

JScrollPane scrollPane = new JScrollPane(table);

//Add the scroll pane to this panel.

add(scrollPane);

}

private void printDebugData(JTable table) {

int numRows = table.getRowCount();

int numCols = table.getColumnCount();

javax.swing.table.TableModel model = table.getModel();

System.out.println("Value of data: ");

for (int i=0; i < numRows; i++) {

System.out.print("row " + i + ":");

for (int j=0; j < numCols; j++) {

System.out.print(" " + model.getValueAt(i, j));

}

System.out.println();

}

System.out.println("--");

}

String filePath = "C:/Documents and Settings/seng/Desktop/testfile/VehicleNode.txt";

public static String[] readTextFile( String filePath )

throws FileNotFoundException

{

if (filePath != null)

{

try

{

BufferedReader reader = new BufferedReader(

new FileReader( filePath ));

Vector lines = new Vector();

String aLine = reader.readLine();

while ( aLine != null )

{

lines.addElement( aLine );

aLine = reader.readLine();

}

String[] lineArray = new String[lines.size()];

lines.copyInto( lineArray );

return lineArray;

}

catch ( IOException exc )

{

exc.printStackTrace();

}

}

return new String[0];

}

/**

* Create the GUI and show it. For thread safety,

* this method should be invoked from the

* event-dispatching thread.

*/

private static void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("SimpleTableDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

input newContentPane = new input();

newContentPane.setOpaque(true); //content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args)

{

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

now i have try to make a a class for read text

String filePath = "C:/Documents and Settings/seng/Desktop/testfile/VehicleNode.txt";

public static String[] readTextFile( String filePath )

throws FileNotFoundException

{

if (filePath != null)

{

try

{

BufferedReader reader = new BufferedReader(

new FileReader( filePath ));

Vector lines = new Vector();

String aLine = reader.readLine();

while ( aLine != null )

{

lines.addElement( aLine );

aLine = reader.readLine();

}

String[] lineArray = new String[lines.size()];

lines.copyInto( lineArray );

return lineArray;

}

catch ( IOException exc )

{

exc.printStackTrace();

}

}

return new String[0];

}

but how can i bring the lineArray

into object[ ] [ ] data

Message was edited by:

wilfrid100

wilfrid100a at 2007-7-14 20:49:06 > top of Java-index,Java Essentials,New To Java...
# 4
String.split()Integer.parseInt()Boolean.getBoolean()Put it all into an Object[]Put the array into the listCall list.toArray() later.
CeciNEstPasUnProgrammeura at 2007-7-14 20:49:06 > top of Java-index,Java Essentials,New To Java...
# 5

public static String[] readTextFile( String filePath )

throws FileNotFoundException

{

try

{

BufferedReader in = new BufferedReader(

new FileReader("C:/Documents and Settings/seng/Desktop/testfile/assignment_matrix.txt"));

String text;

while((text = in.readLine()) != null)

{

String[] out = text.split(" "); // Separated by "whitespace"

for(int i = 0; i < out.length; i++)

{

int a = Integer.parseInt(out[i]);

}

}

}

catch ( IOException exc )

{

exc.printStackTrace();

}

return new String[0];

}

i have try to modify the previous coding, but now i'm not very understand about Boolean stepBoolean.getBoolean

why i have to use the Boolean in here?

is it i use Boolean to detecting " null " string?

Message was edited by:

wilfrid100

wilfrid100a at 2007-7-14 20:49:06 > top of Java-index,Java Essentials,New To Java...