problem i'm having is getSelectedRows() and getSelectedColumns().... they dont return anything when i select the rows.

am i doing it wrong?
i need it so when the button is pressed to output the selected strings[]

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.io.*;
import java.util.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton.*;
import javax.swing.event.ListSelectionEvent;


public class panel extends JFrame
{
    private JTable table;
    private JButton button;
    static ArrayList<String[]> rosterList = new ArrayList<String[]>();
    private JPanel tablepanel;
    private JPanel buttonpanel;
    private JFrame tableframe;
    private JScrollPane scrollPane;
    [B]int [] rowIndex;
    int [] colIndex;[/B]

    public panel()
    {
        setTitle("Roster Sorter");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        textRead();
        jTable();
        jButton();
        add(buttonpanel,BorderLayout.SOUTH);
        add(scrollPane,BorderLayout.NORTH);
        pack();
        setVisible(true);
             
        
        
    }
    
    
     private void jTable()
        {
            
            TableModel myData = new MyTableModel(); 
            table = new JTable(myData);
            scrollPane = new JScrollPane(table);
            scrollPane.setPreferredSize(new Dimension(1400, 700));
            JTableHeader header = table.getTableHeader();
            header.setBackground(Color.yellow);
            table.setAutoCreateRowSorter(true);
            [B]rowIndex = table.getSelectedRows();
            colIndex = table.getSelectedColumns();[/B]
            
            for(int index = 1; index == 26; index++)
        {
        TableColumn col = table.getColumnModel().getColumn(index);
        int width = 150;
        col.setPreferredWidth(width);
        

        }
        int index0 = 0;
        TableColumn col = table.getColumnModel().getColumn(index0);
        int width = 250;
        col.setPreferredWidth(width);
            
        }
    
    
    private class MyTableModel extends AbstractTableModel {
        private String[] columnNames = { "Name" , "Age", "Pos", "Nat", "St", "Tk", "Ps", "Sh", "Ag", "Kab", "Tab", "Pab", "Sab", "Gam", "Sub", "Min", "Mom", "Sav", "Con", "Ktk", "Kps", "Sht", "Gls", "Ass", "DP", "Inj", "Sus"};
        

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

        public int getRowCount() {
            return rosterList.size();
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

      
        public Object getValueAt(int row, int col) 
        {    
             return rosterList.get(row)[col];
                              
        }

        
    }
    
    private static void textRead() 
  {
     BufferedReader br = null;
     
     try
    {
      br = new BufferedReader(new FileReader("rosters.txt"));
      String line = br.readLine();
        
      while (line != null )
      {
        String [] rowfields = line.split("[ ]+");
        rosterList.add(rowfields);
        line = br.readLine();
       }
     
    }
    catch (FileNotFoundException e)
    {
      // can be thrown when creating the FileReader/BufferedReader
      // deal with the exception
      e.printStackTrace();
    }
    catch (IOException e)
    {
      // can be thrown by br.readLine()
      // deal with the exception
      e.printStackTrace();
    }
        
}


private void jButton()
{
    buttonpanel = new JPanel();
    button = new JButton("Save selected to .txt");
    button.setMnemonic(KeyEvent.VK_C);
    buttonpanel.add(button);
    buttonpanel.setSize(50,20);
    button.addActionListener(new ValueButtonListener());
    
}

 private class ValueButtonListener implements ActionListener{
            
             public void actionPerformed (ActionEvent event){
                 
      for(int i = 0; i < rowIndex.length && i < colIndex.length;i++)
      {
        System.out.print(rowIndex[i]);
        System.out.print(colIndex[i]);
    }
    
  
  
}
    
    
    
}
}

Recommended Answers

All 29 Replies

Start with something like:

public void actionPerformed (ActionEvent event){
  int[] selectedRows= table.getSelectedRows()
  for (int i : selectedRows) System.out.println(i);
}

ok now i want to display the actual Strings so i do this:

System.out.println(rosterList.get(i));}

doesnt work... i think i need to do:: butt i dont know how to incorporate that in the same for loop with the rows.

int[] selectedColums= table.getSelectedColumns();

all i can think of is this... but it wont work, im not familiar with the advanced for loop and how to use it properly :(

int[] selectedRows= table.getSelectedRows();  
     int[] selectedCols= table.getSelectedColumns(); 
             for (int i : selectedRows : selectedCols) 
             {
             System.out.println(rosterList.get(i));
            }

I have changed "actionPerformed" Method :

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.io.*;
import java.util.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton.*;
import javax.swing.event.ListSelectionEvent;


public class panel extends JFrame
{
    private JTable table;
    private JButton button;
    static ArrayList<String[]> rosterList = new ArrayList<String[]>();
    private JPanel tablepanel;
    private JPanel buttonpanel;
    private JFrame tableframe;
    private JScrollPane scrollPane;
    int [] rowIndex;
    int [] colIndex;

    public panel()
    {
        setTitle("Roster Sorter");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        textRead();
        jTable();
        jButton();
        add(buttonpanel,BorderLayout.SOUTH);
        add(scrollPane,BorderLayout.NORTH);
        pack();
        setVisible(true);
             
        
        
    }
    
    
     private void jTable()
        {
            
            TableModel myData = new MyTableModel(); 
            table = new JTable(myData);
            scrollPane = new JScrollPane(table);
            scrollPane.setPreferredSize(new Dimension(1400, 700));
            JTableHeader header = table.getTableHeader();
            header.setBackground(Color.yellow);
            table.setAutoCreateRowSorter(true);
            rowIndex = table.getSelectedRows();
            colIndex = table.getSelectedColumns();
            
            for(int index = 1; index == 26; index++)
        {
        TableColumn col = table.getColumnModel().getColumn(index);
        int width = 150;
        col.setPreferredWidth(width);
        

        }
        int index0 = 0;
        TableColumn col = table.getColumnModel().getColumn(index0);
        int width = 250;
        col.setPreferredWidth(width);
            
        }
    
    
    private class MyTableModel extends AbstractTableModel {
        private String[] columnNames = { "Name" , "Age", "Pos", "Nat", "St", "Tk", "Ps", "Sh", "Ag", "Kab", "Tab", "Pab", "Sab", "Gam", "Sub", "Min", "Mom", "Sav", "Con", "Ktk", "Kps", "Sht", "Gls", "Ass", "DP", "Inj", "Sus"};
        

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

        public int getRowCount() {
            return rosterList.size();
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

      
        public Object getValueAt(int row, int col) 
        {    
             return rosterList.get(row)[col];
                              
        }

        
    }
    
    private static void textRead() 
  {
     BufferedReader br = null;
     
     try
    {
      br = new BufferedReader(new FileReader("rosters.txt"));
      String line = br.readLine();
        
      while (line != null )
      {
        String [] rowfields = line.split("[ ]+");
        rosterList.add(rowfields);
        line = br.readLine();
       }
     
    }
    catch (FileNotFoundException e)
    {
      // can be thrown when creating the FileReader/BufferedReader
      // deal with the exception
      e.printStackTrace();
    }
    catch (IOException e)
    {
      // can be thrown by br.readLine()
      // deal with the exception
      e.printStackTrace();
    }
        
}


private void jButton()
{
    buttonpanel = new JPanel();
    button = new JButton("Save selected to .txt");
    button.setMnemonic(KeyEvent.VK_C);
    buttonpanel.add(button);
    buttonpanel.setSize(50,20);
    button.addActionListener(new ValueButtonListener());
    
}

 private class ValueButtonListener implements ActionListener{
            
             public void actionPerformed (ActionEvent event){
                 
      int[] selectedRows= table.getSelectedRows();
      System.out.println("Rows Selected :"+ selectedRows.length);
      for(int i=0;i<selectedRows.length;i++) {
            System.out.println("Row index : " + selectedRows[i]);
             String []t=rosterList.get(selectedRows[i]);
             for(int j=0;j<t.length;j++)
             System.out.println("Values : " + t[j]);
      }
  
  
}
    
    
    
}
 public static void main(String []args) {
   new panel();
  }
}

i think that works... using your ActionPerformed method: am i able to output the selected row data into a .txt file in the same format as the original .txt file which is like this:
(i need all the spacings and stuff to be exact)

B.Friedel     38 LRC usa 20  1  1  1 21 410 300 300 300  18   0 1673   0  76  26   0   0   0   0   0   0   0   0
M.Fulop       26 LRC hun 17  1  1  1 26 308 300 300 300   1   1   16   0   1   0   0   0   0   0   0   0   0   0
B.Guzan       25 LRC usa 15  1  1  1 23 300 300 300 300   0   0    0   0   0   0   0   0   0   0   0   0   0   0
M.Laursen     32   C den  1 24  6 12 25 300 493 409 373  17   0 1619   0   0   0  17   7  15   2   0  14   0   0
C.Davies      24   C eng  1 23  9  6 25 300 302 430 304  15   0 1457   0   0   0   8   9   6   0   0   4   0   0
C.Cuellar     28  RC esp  1 23 12  8 24 300 302 412 320  14   1 1273   0   0   0  10  10  13   0   0   8   0   0
L.Young       30  RC eng  1 23 10  6 22 280 328 435 316  11   1 1006   0   0   2  14   9   5   0   0   0   0   0

If you're asking if you can output the contents of the JTable into the file, then yes, you can. Using two for loops (nested/one inside the other) go through each row and each column and print whats at the index into the text file. Use a PrintWriter to output to the file.

wouldnt i need to have a 2D array or an ArrayList to be able to get values at certain points:

eg row 1, column 10

ok so far i have this... dont worry about the printwriter, i just put it in to test..
i selected 3 rows and 2 colums and this is wat it returns:

Rows Selected :3
Cols Selected :2
rows 0 cols0
rows 0 cols1
rows 1 cols0
rows 1 cols1
rows 2 cols0

where is row 2 column 1?? howcome that doesnt show up?
also when i select more colums i get this:

Rows Selected :2
Cols Selected :5
rows 0 cols0
rows 0 cols1
rows 0 cols2

it doesnt go past 2 columns and doesnt do row 2 at all :S

for(int i=0;i<=selectedRows.length ;i++ )
      {
            
            for(int j = 0; j<selectedColumns.length; j++)
            {
            System.out.println( "rows " +selectedRows[i] + " cols" + selectedColumns[j]);
            
             String []t=rosterList.get(selectedRows[j]);
             String []k=rosterList.get(selectedColumns[i]);
            
            
             try {
                  PrintWriter pw = new PrintWriter("printWriterOutput.txt");
                  pw.println(k[j]);
                  pw.close();
                }catch (IOException e) {
                }
            
            }
            }

Nearly there. You have the loop with i and j ( = row & column numbers). Remember the datamodel? You already have a method there that returns the data for any specified row & column. You can use the same 1 line expression if you just change the variable names a bit.
ps Open the printwriter before you go into the loops, and close it after you've come out. Otherwize you open/close it for every single value.

ok this works.. but how would i put the spaces inbetween the text?

try {
                  PrintWriter pw = new PrintWriter("printWriterOutput.txt");
                  pw.println( "rows ");
                  
                
                     
      
      for(int i=0;i<selectedRows.length ;i++ )
      {
          
            
            for(int j = 0; j<selectedColumns.length; j++)
            {
            System.out.print( rosterList.get(selectedRows[i])[selectedColumns[j]]);
                                          
            }
      
    }

pw.close(); 
       }catch (IOException e) {
                }

i tried putting a + " " in the System.out.print but that gives equal spacing between each string:

B.Friedel    38    LRC    usa    20    1    1    1    21    410    300    300    300    18    0    1673    0    76    26    0    0    0    0    0    0    0    0

i need more spaces after B.Friedel and the rest to remain like that :S

ohh and theres different names to B.Friedel some shorter some longer, i need the age to be displayed in the same columnevery time. how would i go about that? :S

I'm not going to give you the code (against the rules!), but here's the plan:
You want to treat the first column differently from the rest, so in your "i" loop first do col 0 with its own special code, then start the column loop from col 1.
You want to pad out the name (column 0) to a fixed length (say 20) by adding blanks, so get the name, add a string of 19 blanks on the end, then use the substring method to get the first 20 chars from that.

howcome its against the rules? just incase its someones homework or something? :S... you've helped heaps anyway.. slowly getting there :)

ok i got this:

for(int i=0;i<selectedRows.length ;i++ )
      {
          pw.print( rosterList.get(selectedRows[i])[selectedColumns[0]]+ "            ");
            
            for(int j = 1; j<selectedColumns.length; j++)
            {
            pw.print( rosterList.get(selectedRows[i])[selectedColumns[j]]+ "    ");
                                          
            }

what do you mean by:

You want to pad out the name (column 0) to a fixed length (say 20) by adding blanks, so get the name, add a string of 19 blanks on the end

String column 0 = " " <---- is that what you mean?

String name = selectedColumns[0]+"              ";  
          String name1 = name.substring(0,14);

i think this is what you ment? but how can i put a string in my rosterList.get (selectedRows) [ .........]

Have a look at this, it may help you (!)
String name = "James";
String paddedString = name + " ";
String fixedLength = paddedString.substring(0,19);

thank you! got it for the first part..
the spaces in the original file vary. does that mean im gonna have to do it manualy for each row that changes? do a simular thing to what i did now?

ORIGINAL:
B.Friedel     38 LRC usa 20  1  1  1 21 410 300 300 300  18   0 1673   0  76  26   0   0   0   0   0   0   0   0
MYOUTPUT:
B.Friedel     38    LRC    usa    20    1    1    1    21    410    300    300    300    18    0    1673    0    76    26    0    0    0    0    0    0    0    0

If the original is aligned to fixed columns, then yes, you can pad them all to the same length just like you did for the name. If the originals have random spacing and you want to keep the original number of blanks, you'll have to store the original lines in a separate ArrayList<String>, and retrieve them from there by row number.

yeah the orignal is aligned to fixed columns i think :

B.Friedel     38 LRC usa 20  1  1  1 21 410 300 300 300  18   0 1673   0  76  26   0   0   0   0   0   0   0   0
M.Fulop       26 LRC hun 17  1  1  1 26 308 300 300 300   1   1   16   0   1   0   0   0   0   0   0   0   0   0
B.Guzan       25 LRC usa 15  1  1  1 23 300 300 300 300   0   0    0   0   0   0   0   0   0   0   0   0   0   0
M.Laursen     32   C den  1 24  6 12 25 300 493 409 373  17   0 1619   0   0   0  17   7  15   2   0  14   0   0
C.Davies      24   C eng  1 23  9  6 25 300 302 430 304  15   0 1457   0   0   0   8   9   6   0   0   4   0   0
C.Cuellar     28  RC esp  1 23 12  8 24 300 302 412 320  14   1 1273   0   0   0  10  10  13   0   0   8   0   0
L.Young       30  RC eng  1 23 10  6 22 280 328 435 316  11   1 1006   0   0   2  14   9   5   0   0   0   0   0

^^ thats fixed columns yes? ill give it a shot and see what i can come up with. thanks for the help again mate.

I notice that the extra columns are padded at the front, not at the end like the name was. So you'll need a slightly different version of the padding code where you add the blanks at the beginning of the string, then substring the last "n" characters. You should be able to do that with a little thought. You'll need the length() of the string to work out where the last "n" characters start.

hey mate... ive done some of it , dont laugh its the only way i could think of ahahah im sure theres a much easier way but oh well.

im stuck on the last one in the list (bolded one)

it should look like (also bolded)

B.Friedel     38 LRC usa 20  1  1  1 21 [B]410[/B] 300 300 300  18   0 1673   0  76  26   0   0   0   0   0   0   0   0

i can get the single spacing between the 21 and 410 but if 410 was to be reduced to say 10... i cant get it to do double spacing between 21 and 10.. ive tried everything :S

for(int i=0;i<selectedRows.length ;i++ )
      {
          String name = rosterList.get(selectedRows[i])[selectedColumns[0]]; 
          String padName0 = name+"               ";
          String fixedlength = padName0.substring(0,14);
          pw.print( fixedlength);
          pw.print(rosterList.get(selectedRows[i])[selectedColumns[1]]); 
          
           
          String col2 = rosterList.get(selectedRows[i])[selectedColumns[2]];
          String padName1 = "     "+ col2;
          String fixedL = padName1.substring(4);
          pw.print(fixedL);
          
          String col3 = rosterList.get(selectedRows[i])[selectedColumns[3]];
          String padName2 = "     "+ col3;
          String fixedL1 = padName2.substring(4);
          pw.print(fixedL1);
          
          String col4 = rosterList.get(selectedRows[i])[selectedColumns[4]];
          String padName3 = "     "+ col4;
          String fixedL2 = padName3.substring(3);
          pw.print(fixedL2);
          
          String col5 = rosterList.get(selectedRows[i])[selectedColumns[5]];
          String padName4 = "   "+ col5;
          String fixedL3 = padName4.substring(1);
          pw.print(fixedL3);
          
          String col6 = rosterList.get(selectedRows[i])[selectedColumns[6]];
          String padName5 = "   "+ col6;
          String fixedL4 = padName5.substring(1);
          pw.print(fixedL4);
          
          String col7 = rosterList.get(selectedRows[i])[selectedColumns[7]];
          String padName6 = "   "+ col7;
          String fixedL5 = padName6.substring(1);
          pw.print(fixedL5);
          
          String col8 = rosterList.get(selectedRows[i])[selectedColumns[8]];
          String padName7 = "   "+ col8;
          String fixedL6 = padName7.substring(1);
          pw.print(fixedL6);
          
          [B]String col9 = rosterList.get(selectedRows[i])[selectedColumns[9]];
          String padName8 ="    "+col9;
          String fixedL7 = padName8.substring(5,1);
          pw.print(fixedL7);[/B]
            
            
           
            
    }

The number of spaces you put before each number needs to be based on the length of the number. So you have to get the length of the String that you want to print out and do some sort of subtraction to figure out how many spaces you need to put before it. Can you post your print method? Also, are these the rules for the output file:

1. there must be at least one space between each column of numbers in the output?
2. The number of spaces between two columns is 3-(number of digits in the second column)?

B.Friedel     38 LRC usa 20  1  1  1 21 410 300 300 300   0   [B]0    0[/B]   0   0   0   0   0   0   0   0   0   0   0

1. basically yes but it does vary from 1 space to a max of 4 spaces at one spot.
2. not sure, i was given it dont know how they made it :S

this is my print method?

try {
                  PrintWriter pw = new PrintWriter("printWriterOutput.txt");
                 
                  
                
                     
      
      for(int i=0;i<selectedRows.length ;i++ )
      {
          String name = rosterList.get(selectedRows[i])[selectedColumns[0]]; 
          String padName0 = name+"               ";
          String fixedlength = padName0.substring(0,14);
          pw.print( fixedlength);
          pw.print(rosterList.get(selectedRows[i])[selectedColumns[1]]); 
          
           
          String col2 = rosterList.get(selectedRows[i])[selectedColumns[2]];
          String padName1 = "     "+ col2;
          String fixedL = padName1.substring(4);
          pw.print(fixedL);
          
          String col3 = rosterList.get(selectedRows[i])[selectedColumns[3]];
          String padName2 = "     "+ col3;
          String fixedL1 = padName2.substring(4);
          pw.print(fixedL1);
          
          String col4 = rosterList.get(selectedRows[i])[selectedColumns[4]];
          String padName3 = "     "+ col4;
          String fixedL2 = padName3.substring(3);
          pw.print(fixedL2);
          
          String col5 = rosterList.get(selectedRows[i])[selectedColumns[5]];
          String padName4 = "   "+ col5;
          String fixedL3 = padName4.substring(1);
          pw.print(fixedL3);
          
          String col6 = rosterList.get(selectedRows[i])[selectedColumns[6]];
          String padName5 = "   "+ col6;
          String fixedL4 = padName5.substring(1);
          pw.print(fixedL4);
          
          String col7 = rosterList.get(selectedRows[i])[selectedColumns[7]];
          String padName6 = "   "+ col7;
          String fixedL5 = padName6.substring(1);
          pw.print(fixedL5);
          
          String col8 = rosterList.get(selectedRows[i])[selectedColumns[8]];
          String padName7 = "   "+ col8;
          String fixedL6 = padName7.substring(1);
          pw.print(fixedL6);
          
          String col9 = rosterList.get(selectedRows[i])[selectedColumns[9]];
          String padName8 ="      "+col9;
          String fixedL7 = padName8.substring(9,6);
          pw.print(fixedL7);
            
            
           
            
    }
    
    pw.close(); 
       }

Why are there so many spaces between the last 0's but not between the first numbers? In your other post, there are much fewer spaces. Perhaps you should identify some patterns in the input file before attempting to produce the corresponding output file. Either way, I don't really understand what your rules are for the output file; as far as I know, it's a random number of spaces, so I'll stop wasting your time. It seems like James knows whats going on here though so you'll just have to wait until he gets back on. Good luck & happy coding.

Hi, I'm back! AFIK the rule is that the numbers are padded with leading blanks to a fixed total width, so they all line up in columns, right-aligned.
1. The rule, therefore the code, is the same for all columns except the first - there's no need to replicate all those code fragments.
2. Suppose the desired width is 5 chars. You stick 5 chars on the front OK, but the you need to substring the last 5 chars. So substring(1) isn't going to work. The substring need to start 5 chars before the end of the string. You're going to need the length() method (as I said before). Try writing down a couple of examples on a piece of paper and see if you can work out the correct sunstring expression before trying to code it.
(Hi, BJSJC!)

commented: Hi to you too! And you gave a lot of good info in this thread :) +5

i recreated the nested for loop again and used the length() method. this gives me equal 1 spacing through out the whole output. much quicker than what i did haha :) thanks.

for(int i=0;i<selectedRows.length ;i++ )
      {
          String name = rosterList.get(selectedRows[i])[selectedColumns[0]]; 
          String padName0 = name+"               ";
          String fixedlength = padName0.substring(0,14);
          pw.print( fixedlength);
          pw.print(rosterList.get(selectedRows[i])[selectedColumns[1]]);
          for(int j = 2; j<selectedColumns.length; j++)
            {
            int len = rosterList.get(selectedRows[i])[selectedColumns[j]].length();
            String name1 = rosterList.get(selectedRows[i])[selectedColumns[j]]; 
            String padName1 = " "+name1;
            String fixedlength1 = padName1.substring(0,len+1);
            pw.print(fixedlength1);
                                         
            }
        }

i got it working mate, prints out perfectly for the whole set of data :D thanks!!!... i have 3 more questions:

1:
i have a JTable right with all the information in it.... i print out what i highlight.. i have 27 columns and lets say 10 rows... in order for me to print out all the data to the .txt i need to selected Cell 0 column 0 and drag to cel 26 column 9... is there anyway i can make it so that i can just drag down from column 0 and it selects all of the columns?

2:
i use the line below to sort out my rows... so when i click on a column it sorts alphabetically or by highest number.

table.setAutoCreateRowSorter(true);

it works untill a point i get to a column which has numbers ranging from 1 to 25....

sorts out the 9's then 7,6,5,4,3....
and the number 25 isnt recognised as a whole number which is larger then 9 and gets sorted in where the 2's are. how can that be fixed? :S

3:

when i sort the data and select what i want to print to .txt it doesnt print out the sorted but prints out what originally was in the position

ok part 3 has been solved. the other 2 i still need help with.

1. Just use the selected rows, and ignore the selected columns and print all the columns regardless of which were selected.
2. To get the sorting in numeric order probably best way is to store the data as numbers in the first place - but that's going to mean quite a few code changes, maybe not worth the bother? Maybe someone with more experience of JTables can point you to a way of using a custom sort ode?

oh ok... yeh i probably wont bother with it... thanks for your help again!! im going to attempt another app.

hey mate i got another question ahahah, i just cant stop.

umm i made an app which asks a user if they want to do something, the user inputs either a "y" or a "n"... i want to put an exception catcher around it but i dont know which one to use?.

change1 = JOptionPane.showInputDialog("Change Silo 1 dimensions? (y/n)");
        change2 = JOptionPane.showInputDialog(null,"Change Silo 2 dimensions? (y/n)");
        
 
   
     
              
        if (change1.equalsIgnoreCase("y"))
    {
        do
        {
        String input = JOptionPane.showInputDialog("Enter Radius of Silo:1");
        try
            {
               num = Double.parseDouble(input);
            }   
         catch (NumberFormatException nfe)
            {
                JOptionPane.showMessageDialog(null,"That is not an integer, enter radius again");   
            }
         } while(num < 0 );
         Sone.setRadius(num);
         
        do
        {
        String input = JOptionPane.showInputDialog("Enter height of Silo:1");
        try
            {
               num = Double.parseDouble(input);
            }   
         catch (NumberFormatException nfe)
            {
                JOptionPane.showMessageDialog(null,"That is not an integer, enter height again");   
            }
         } while(num < 0);
         Sone.setHeight(num);
    }
         else if(change1.equalsIgnoreCase("n"))
         {
             System.exit(0);
            
            }
            
                     
        
        if (change2.equalsIgnoreCase("y")) 
   {
        do
        {
        String input = JOptionPane.showInputDialog("Enter Radius of Silo:2");
        try
            {
               num = Double.parseDouble(input);
            }   
         catch (NumberFormatException nfe)
            {
                JOptionPane.showMessageDialog(null,"That is not an integer, enter radius again");   
            }
         } while(num < 0);
         Stwo.setRadius(num);
         
        do
        {
        String input = JOptionPane.showInputDialog("Enter Height of Silo:2");
        try
            {
               num = Double.parseDouble(input);
            }   
         catch (NumberFormatException nfe)
            {
                JOptionPane.showMessageDialog(null,"That is not an integer, enter height again");   
            }
         } while(num < 0);
         Stwo.setHeight(num);
   }
   
   else if(change2.equalsIgnoreCase("n"))
         {
             System.exit(0);
            
            }
   
        if (change1.equalsIgnoreCase("y"))
        {
            JOptionPane.showMessageDialog(null,"Silo 1 Dimensions:\n" + "Radius = " + fmt.format(Sone.getRadius()) +"\nHeight = " + fmt.format(Sone.getHeight()) + "\nVolume = " + fmt.format(Sone.getVolume()) + "\nSurface Area = " + fmt.format(Sone.getSurfaceArea()));
        }
        
        if (change2.equalsIgnoreCase("y"))
        {
            JOptionPane.showMessageDialog(null,"Silo 2 Dimensions:\n" + "Radius = " + fmt.format(Stwo.getRadius()) +"\nHeight = " + fmt.format(Stwo.getHeight()) + "\nVolume = " + fmt.format(Stwo.getVolume()) + "\nSurface Area = " + fmt.format(Stwo.getSurfaceArea()));
        }
    }


}

this is part of the code. change1 and change2 is where i need the exception.. like if the user puts in a number or " ; , . / etc...

Sorry - I've no idea what exception you want to catch, or why! If you need to catch an exception, the compiler will tell you.
ps: Suggest you start a new thread for the next Q
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.