0
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String dishName = "";
    ArrayList<Integer> st = new ArrayList<Integer>();
    int rowCount = table.getRowCount();
    int fPrice;
    int rowIndex = 0; 
    int colIndex = 4; 
    boolean emptyFlag = false;
    do {
        String price = (String) table.getValueAt(rowIndex, 4);
        fPrice = Integer.parseInt(price);
        if (price != null && price.length() != 0) {
            st.add(fPrice);
            rowIndex++;
        } else {
            emptyFlag = true;
        }
    } while (rowIndex < rowCount && !emptyFlag);
    Collections.sort(st); 
    int key = Integer.parseInt(searchPrice.getText()); 
    int low = 0;
    int high = st.size() - 1;
    int searchResult = MenuInfo.priceSearch(st, low, high, key);
    if(searchResult==-1){
        JOptionPane.showMessageDialog(this, "Could not find the dish of your price!");}
    else{
        dishName =  (String) table.getValueAt(searchResult,1); 
        JOptionPane.showMessageDialog(this, "The price you have searched can afford " + dishName);
    }
} //ends here

The above code is the code I have tried in my program. But it can display the corresponding dishName only if the data are sorted previously. If I add dish of lower price, then it displays the dishname of first row. Please do appreciate my request :)

Here is the image of my jtable

Community
  • 1
  • 1
Nishan
  • 3
  • 4
  • Just a quick look through, it looks like you're sorting only your prices, so when you try to get the lowest price, which would be the first item in your ArrayList, the corresponding name would be the first name in your table. Have you considered creating a class to represent a row in your table? – Shoikana Dec 30 '18 at 17:01
  • @Shoikana Sir actually I am literally a beginner in this field. I have no idea on how to sort the dish name too along with the prices. please do suggest me a source where I can find the soultion :) please – Nishan Dec 30 '18 at 17:57

1 Answers1

0

I wrote some code that may help. It looks like you're using Swing, so I used Swing. My code will find the corresponding value, but not sort the rows. I'll share it, but if you really need it to be sorted, let me know.

public static void main(String[] args) {

    String[] headers = {"Name","Number"};
    String[][] rowData = {{"foo","500"},{"bar","700"}};
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();


    //frame.add(panel);
    //A table is controlled by its model, so we need a variable to access the model
    DefaultTableModel model = new DefaultTableModel(rowData,headers);
    JTable table = new JTable(model);
    JScrollPane scroll = new JScrollPane(table);
    //To allow sorting, we us a TableRowSorter object
    TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
    table.setRowSorter(sorter);

    //We have to set the 2nd column to be sortable
    ArrayList<RowSorter.SortKey> sortKeys = new ArrayList<>();
    //sorting on 2nd column (with index of 1), which is Number
    sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
    sorter.setSortKeys(sortKeys);
    sorter.sort();

    //add new value, then sort
    Object[] newRow = {"baz", "600"};
    model.addRow(newRow);
    sorter.sort();

    panel.add(scroll);
    frame.add(panel);
    frame.setSize(800, 400);
    //show is deprecrated.. I shouldn't be using it!
    frame.show();

    int minIndex = findMin(table, 1);
    //use the row index of the min item in column 1 to get the corresponding value in column 0
    System.out.println("Minimum String: " + table.getValueAt(minIndex, 0));

}

//find min value in the column with index tableColumnIndex and return the row index of that item
public static int findMin(JTable table, int tableColumnIndex) {

    int minIndex = 0;
    String min = table.getValueAt(0, tableColumnIndex).toString();
    for (int i = 1; i < table.getRowCount(); i++) {
        if (table.getValueAt(i, tableColumnIndex).toString().compareTo(min) < 0) {
            min = table.getValueAt(i, tableColumnIndex).toString();
            minIndex = i;
        }
    }
    return minIndex;
}
Shoikana
  • 595
  • 4
  • 8
  • ☺️ Sir thanks alot for your contribution. But how can I implement this in my program. I have hardly sorted the price of the table, but still I am unable to put them on the table and also can't find the corresponding dish name. I really need to find the values according to sorted price in the table. – Nishan Dec 30 '18 at 19:01
  • Right now I don't have time to change my code, but I found this article: https://stackoverflow.com/questions/28823670/how-to-sort-jtable-in-shortest-way If this isn't resolved when I have time to look again, I'll see about changing my code – Shoikana Dec 30 '18 at 19:28
  • Sir, that was out of my understanding ... can't I have your contact details so that I can contact you when you are free? – Nishan Dec 30 '18 at 19:32
  • I'd rather stick with communicating here. I changed the code to use a table sorter. It adds a new row and then sorts the table, so hopefully that's what you need. – Shoikana Dec 30 '18 at 23:42
  • If you found that this answered your question, I'd appreciate it if you'd mark it as the answer. – Shoikana Dec 31 '18 at 16:07
  • Sir thanks alot but I am also trying to use the merge sort at a same time. Sir can't I send you my projects and get the guidence? – Nishan Jan 01 '19 at 06:35
  • You said nothing about a merge sort in the original problem. If you want a merge sort, you should change your question or create a new one. And I'd rather stick with this platform. People often search for questions that have already been asked, so if you don't ask for a merge sort, a future person won't know that the answer involves a merge sort. – Shoikana Jan 01 '19 at 13:46
  • I have posted another question related to the merge sort. Please help to sort out the problem according to my criteria. :) – Nishan Jan 02 '19 at 12:56
  • And yes guys :) The answer of the above question is the particular code, respected @Shoikana has given :) – Nishan Jan 02 '19 at 12:57