数组—在java中将文件内容读入jtable

axkjgtzd  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(267)

这个程序是用来读取一个文件,将其存储在一个二维数组中,并将其打印到一个表上。但是,我真的不知道为什么这不是打印出我的表中的二维数组“courses”的内容,但是,它只打印一行和一些列。在这些列中,它打印出以下内容: java.lang.String;@5edc2a37 求你了,我需要有人帮我找出问题所在。

import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.util.*;
    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.util.Scanner;
    import java.util.Calendar;
    import java.io.*;

    public class tablePanel extends JPanel {

        private static final long serialVersionUID = 1L;
        private JTable table;
        CourseCatalog viewCourses;
        String[][] courses;
        int count;

        public tablePanel() 
    {
      count = 5;
      try
      {
         ViewCourses();
         //count = viewCourses.countLines();
      }
      catch(IOException fe){}

      //viewCourses = new courseCatalog();

      courses = new String[count][6];
      /*try
      {
         //courses = viewCourses.ViewCourses();
      }
      catch(IOException fe){}*/

      setLayout(new BorderLayout());

        Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
        Object[][] data = {courses};

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) 
        {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) 
            {
                switch (column) 
                {
                    case 0:
                        return String.class;
                    case 1:
                        return String.class;
                    case 2:
                        return String.class;
                    case 3:
                        return String.class;
                    case 4:
                        return String.class;
                    default:
                        return String.class;
                }
            };
        };

         table.getModel().addTableModelListener(new TableModelListener() 
         {
              @Override
              public void tableChanged(TableModelEvent e) 
              {

                  int roW = e.getFirstRow();
                  int coLL = e.getColumn();

                  table.getModel().getValueAt(roW , coLL);

                  String VAlue = (String)table.getModel().getValueAt(roW , coLL);
                  System.out.println(roW+" "+coLL+": Value: "+VAlue);
              }
      });

        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane tableContainer = new JScrollPane(table);
        add(tableContainer, BorderLayout.CENTER);
        //getContentPane().add(scrollPane);
    }

    int countLines()throws IOException//counts the number of lines in the file
    {
        InputStream is = new BufferedInputStream(new FileInputStream("courseCatalog.txt"));
      byte[] c = new byte[1024];
      int count = 0;
      int readChars = 0;
      boolean empty = true;
      while ((readChars = is.read(c)) != -1)
      {
          empty = false;
          for (int i = 0; i < readChars; ++i)
          {
              if (c[i] == '\n')
              {
                  ++count;
              }
          }
      }

      is.close();
        return count;

    }

    String[][] ViewCourses()throws IOException//Returns 2D array with all the courses
    {
        int count = countLines(), counter = 0;
        courses = new String[count][6];
        Scanner read = new Scanner(new File("courseCatalog.txt"));
        read.useDelimiter("\\*");
        BufferedReader file = new BufferedReader(new FileReader("courseCatalog.txt"));
      System.out.println("I was called: Table");
        for(int i = 0; i < count; i++)
        {
            for(int j = 0; j < 6; j++)
            {
                courses[i][j] = read.next();
            System.out.println(courses[i][j]);
            }
            counter++;
            if(counter == count)
                return courses;
        }

        return courses;
    }

数组的内容如下:

courses[0] = { "aim", "go", "hear", "h","kkk", "hn"}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题