Results 1 to 6 of 6
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default What went wrong. Methods problem.

    <SOLVED> I dont know how to do to make it solved at the link.

    Evening all.

    Had it working, when I had 1 Class 1 Constructor and 1 Method. In the Method i had the code to read both Columns and Data. And ofcourse i got the brigth ide to split Columns in one method and Data in one. And now my table dont read the files. Worth mention is that i moved Vector<String> to be declared in my class so it could be reached by both method. Posting all code and hope someone can find the. As you migth see i tryed to return value in Column method. but didnt get it to work.

    Java Code:
    package table;
    import java.awt.BorderLayout;
    import java.io.*;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.table.DefaultTableCellRenderer;
    
    public class oceanTable extends DefaultTableCellRenderer {
    int a;
    String[] splitColumn= null;
    String[] splitData= null;
    int wordCountColumn = 0, numberCountColumn =0;
    int wordCountData = 0,  numberCountData = 0;;
    int totalColumn, totalData;
    private Vector<String> row = new Vector<String>();
    private Vector<Vector> rowData = new Vector<Vector>();
    private Vector<String> columnNames = new Vector<String>();
    
    public oceanTable() {
    		showGui();
                    readColumn();
                    readData(splitData);
    }
    
      void readData(String[] a) {
            
    		// START LOOP DATA TABLE	
                    String  strData;
    	 try {
    		StreamTokenizer sTokenizerData = new StreamTokenizer
                                      (new FileReader("/home/magnus/workspace/ocean/src/table/data.dat"));
    		while (sTokenizerData.nextToken() != StreamTokenizer.TT_EOF) {
                     // Count number of words
    		  if (sTokenizerData.ttype == StreamTokenizer.TT_WORD)
    		  wordCountData++;
    		 // Count number of numbers.
    		  else if (sTokenizerData.ttype == StreamTokenizer.TT_NUMBER)
    		  numberCountData++;
    		    }
    	        //  give "total" the value of both word and numbers.
    		 totalData = wordCountData+numberCountData;
    
    		BufferedReader in = new BufferedReader(new InputStreamReader
                                      (new FileInputStream("/home/magnus/workspace/ocean/src/table/data.dat")));
    		 while ((strData = in.readLine())  != null ) {
    			 // Split the string at every -.
    			splitData = strData.split("-");
    			// Write out data in table.
    		for (int i=0;  i < totalData ; i++) {
                            row.addElement( splitData [i]);
    			}
    		 }
    	      in.close();
    	   } catch (IOException e) {
    		e.printStackTrace();
    		System.out.println("Error:" +e.toString());
               }
    	    rowData.addElement(row);
    	    // END DATA TABLE
            }
    
       public String[]  readColumn() {
    	    // START READ COLUMN
    	    String strColumn;
    	   try {
    		   // Count number of words and numbers in file.
    	         StreamTokenizer sTokenizerColumn = new StreamTokenizer
                      (new FileReader("/home/magnus/workspace/ocean/src/table/columns.dat"));
    		  while (sTokenizerColumn.nextToken() != StreamTokenizer.TT_EOF) {
    		  if (sTokenizerColumn.ttype == StreamTokenizer.TT_WORD)
    		  wordCountColumn++;
    		  else if (sTokenizerColumn.ttype == StreamTokenizer.TT_NUMBER)
                      numberCountColumn++;
    		  totalColumn=wordCountColumn+numberCountColumn;
    		    }
    		 // Read all columns in file.
    		  BufferedReader columnIn = new BufferedReader(new InputStreamReader
                      (new FileInputStream("/home/magnus/workspace/ocean/src/table/columns.dat")));
    		  while ((strColumn = columnIn.readLine())  != null ) {
                      // Split the string at every -.
    	          splitColumn = strColumn.split("-");
    	  	  // Write out columns in table.
    		  for (int i=0;  i < totalColumn ; i++) {
                           columnNames.addElement( splitColumn [i]);
    			}
    		}
    		columnIn.close();
    		}catch (IOException e) {
    		e.printStackTrace();
    		System.out.println("Error:" +e.toString());
    	   }
                return splitColumn;
       }
    
         void showGui(){
    	JFrame frame = new JFrame("Table");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setSize(200,200);
    	frame.setVisible(true);
    	JPanel panel = new JPanel();
    	JTable table = new JTable(rowData, columnNames);
    	JScrollPane scrollPane = new JScrollPane(table);
    	frame.add(panel);
            panel.add(scrollPane, BorderLayout.CENTER);
    	frame.pack();
    	}// END COLUMN.
      
    	public static void main(String[] args) {
    		java.awt.EventQueue.invokeLater(new Runnable(){
    			public void run(){
    				new oceanTable();
    			}
    		}
    );
    	}
    
    }
    And if there is a better why to do this i would be happy to learn it.
    Last edited by ocean; 11-02-2009 at 07:33 PM. Reason: missspelled

  2. #2
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    You actually do not need to return splitColumn in your readColumn method because you are already working with the class level variable. Also, you do not need to pass any parameters to readData (you don't even use the parameter).

    Also, you should close your fileReader that you use with your Tokenizer. This means youll have to create a FileReader variable and then pass that in to the StreamTokenizer so you can properly close the stream before opening the same file again.

  3. #3
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    The way i have it now i need the return. Else netBeans complain.

    Java Code:
     public String[] readData() {
     return splitData;
    }
     public String[]  readColumn() {
     return splitColumn;
    }
    But good point in closing the file. And the parameters I have removed. Still, i cant get it to work. Mabey have to go back to one method.

  4. #4
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Hm can it be this row that is wrong?
    Java Code:
    JTable table = new JTable(rowData, columnData);
    Do i need to add the methods like. (readData().rowData) ? Aa i wish i was not so noobish. =)

  5. #5
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    Ah, I see the problem now. You are calling showGUI() first! It will fill in the table stuff with nothing (because you haven't read in the files yet. Place the showGUI() call after you do readData() in your oceanTable constructor.

  6. #6
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Yes Indeeeeeeaaadddd it was the problem. O my. A very big thank to you.. god bless you.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. What am I doing wrong in this basic problem
    By Blaedel in forum New To Java
    Replies: 10
    Last Post: 10-11-2009, 08:45 PM
  3. Please help, what am I doing wrong?
    By mmac1218 in forum New To Java
    Replies: 2
    Last Post: 09-02-2009, 12:21 AM
  4. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  5. Wats the problem here ? anythig wrong with IDE
    By Shyam Singh in forum New To Java
    Replies: 6
    Last Post: 07-17-2008, 05:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •