how to put a static method into Object[ ] [ ] array?
publicstatic Object[] readTextFile()
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)
{
List list =new ArrayList();
String[] out = text.split(" ");// Separated by "whitespace"
for(int i = 0; i < out.length; i++)
{
int a = Integer.parseInt(out[i]);
Integer b =new Integer(a);
list.add(b);
Object[] output = list.toArray();
}
}
}
catch ( IOException exc )
{
exc.printStackTrace();
}
returnnew String[0];
}
i have create a static method which is use to read .txt file....now i wish i can put this method into object[ ][ ]
...how do i do it?
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));
Your method to read the file has significant errors that I have tried to fix.
public static Object[][] readTextFile()
throws FileNotFoundException, IOException
{
BufferedReader in = new BufferedReader(
new FileReader("C:/Documents and Settings/seng/Desktop/testfile/assignment_matrix.txt"));
List list = new ArrayList();
String text;
while((text = in.readLine()) != null)
{
Object[] out = text.split(" "); // Separated by "whitespace"
for (int index = 0; index< out.length; index++)
out[index] = Integer.parseInt((String)out[index]);
list.add(out);
}
Object[][] output = (Object[][])list.toArray(new Object[0][0]);
return output;
}
Please study my revised version and compare it with yours. It should be obvious then how to use it to create your table.
[nobr]soli about repeating question....
Object[] out = text.split(" "); // Separated by "whitespace"
for (int index = 0; index< out.length; index++)
out[index] = Integer.parseInt((String)out[index]);
list.add(out);
thank you for your coding, now i have learn somethgs from this coding...but i still have somethgs which is not understand...
i have read some resources from website, mostly explain Integer.parseInt()
is use to converting the value into integer(binary). But now from the code
Object[] out = text.split(" "); // Separated by "whitespace"
for (int index = 0; index< out.length; index++)
out[index] = Integer.parseInt((String)out[index]);
list.add(out);
we are converting a "String out[index] " value into " Object[] out ".is this can working?
PS: sorry every1, in my hand i have only
Stringsn1;
Stringsn2;
int n1,n2;
sn1 = request.getParameter("n1");
sn2 = request.getParameter("n2");
n1 = Integer.parseInt(sn1);
n2 = Integer.parseInt(sn2);
out.println("The numbers were " + sn1 + " and " + sn2);
out.println("<br>");
out.println("The sum is " + (n1+n2));
this example..that why myself not familiar with this Integer.parseInt ()..hope every1 can share with your knowladge...[/nobr]