Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9103927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:44:39+00:00 2026-06-17T01:44:39+00:00

I have a code that read file and store in arraylist and then convert

  • 0

I have a code that read file and store in arraylist and then convert to array(To use for table model)

My class extends abstracttablemodel correctly.

My All Code is:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

public class ReadFileToList extends AbstractTableModel{
String[] col={"Fname","Lname","Number"};
List<String> data=new ArrayList<String>();
String[][] Arraydata;

public ReadFileToList(){
try{
FileReader fr=new FileReader("D:\\AllUserRecords.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine()) !=null){
    data.add(line);
}
br.close();
Arraydata=(String[][]) data.toArray();
}
catch(IOException ioe){
    ioe.printStackTrace();
}
}

@Override
public String getColumnName(int colu){
return col[colu];
}

public int getRowCount() {
return Arraydata.length;
}

public int getColumnCount() {
return col.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
return Arraydata[rowIndex][columnIndex];
}
}

My main Class is ReadFileToListM:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class ReadFileToListM  {
ReadFileToList rftl=new ReadFileToList();
public ReadFileToListM(){
    JFrame frame=new JFrame();
    JTable table=new JTable(rftl);
    JPanel panel=new JPanel();
    JScrollPane sp=new JScrollPane(table);
    panel.add(sp);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(470,470);
    frame.setVisible(true);
}

public static void main(String[] args){
    new ReadFileToListM();
}
}

but it has Exception!

this is my Exceptions:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.String;
    at javaapplication1.ReadFileToList.<init>(ReadFileToList.java:37)
    at javaapplication1.ReadFileToListM.<init>(ReadFileToListM.java:8)
    at javaapplication1.ReadFileToListM.main(ReadFileToListM.java:22)
Java Result: 1

My txt File:

 FName Lname Number
 second secondsecond 22
 thired thithird 33
 fourth fourfourr 44
 fifth fiffif 55

Please help me, Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:44:40+00:00Added an answer on June 17, 2026 at 1:44 am

    Contructor of the ReadFileToList class is empty, you should change:

    public void readtolist() throws IOException{
    

    to

    public ReadFileToList() throws IOException{
    

    because if dont do that initialization of your model(code below) doesnt execute your method which put data in your collections but only execute contructor from extended class.

    ReadFileToList rftl=new ReadFileToList();
    

    2nd

    You can’t cast List which is one-dimential dynamic sized array to two-dimential array. Other methods from your model looks good.

    Also you should somehow seperate data from the File. At this moment it’s only splitted to to rows.

    It will be really helpful if you show us few lines from the file.

    EDIT: 06-01-2013 After posting file content Change Countructor in model to this one

    public ReadFileToList() throws IOException{
        FileReader fr=new FileReader("D:/AllUserRecords.txt"); //one slash instead of two backslash
        BufferedReader br=new BufferedReader(fr);
        String line = br.readLine();
        while((line=br.readLine()) !=null){
            data.add(line.trim());  //trim() delete spaces before and after line
            System.out.println(line);
        }
        br.close();
        Arraydata = new String[data.size()][];
        for (int i=0;i<data.size();i++){
            Arraydata[i]=data.get(i).split(" "); //split text to array by space
        }
    
    }
    

    main should look like this

    public class ReadFileToListM  {
    
    public ReadFileToListM() throws IOException{
        JFrame frame=new JFrame();
        ReadFileToList rftl=new ReadFileToList();
        JTable table=new JTable(rftl);
        JPanel panel=new JPanel();
        JScrollPane sp=new JScrollPane(table);
        panel.add(sp);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(470,470);
        frame.setVisible(true);
    }
    
    public static void main(String[] args) throws IOException{
        new ReadFileToListM();
    }
    }
    

    Result:
    Result

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read that Windows wp8 allows native code. So how then have ports such
I previously had code that used File.ReadAllLines() to read a text file and store
I have made a code that read data from flash Nand (without filesystem). fd
I have code that looks like this: template<class T> class list { public: class
hi I have to write a windows api code that encrypts a file by
I have two arrays the store the values of two lines that are read
I have the following block of code that i am using to read a
I have a program that load data from a file using std::ifstream and store
I have the following code and tries to store an array in a .data
I'm trying to create a simple class to read a csv file and store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.