-1

I am using cosine similarity function to compare the value between user input and the data in SQL. The highest value will be retrieved and displayed.

However, k is the value getting from comboBox and it is hard constraints which mean they need to be fulfilled. So I have set it to something like:

The highest value found in index X . Before display, it will check whether day is equal to k. If not, it will look at the second highest and so on until day is equal to k.

But this doesn't make sense at all. If day is equal to k only when it is in the ninth highest value, then I need to set until ninth highest value? Is there any method can solve this?

private void pick_highest_value_here_and_display(ArrayList<Double> value,
    int k) throws Exception {
  // TODO Auto-generated method stub
  double aa[] = value.stream().mapToDouble(v -> v.doubleValue()).toArray();
  double highest = Double.MIN_VALUE;
  double secHighest = Double.MIN_VALUE;
  int highestIndex = 0;

  for (int i = 0; i < aa.length; i++) {
    if (aa[i] > highest) {
      highest = aa[i];
      highestIndex = i;
    }
  }
  System.out.println("The highest value is " + highest + "");
  System.out.println("It is found at index " + highestIndex + "");
  String sql = "Select Day from menu where ID =?";
  DatabaseConnection db = new DatabaseConnection();
  Connection conn = db.getConnection();
  PreparedStatement ps = conn.prepareStatement(sql);
  ps.setInt(1, highestIndex);
  ResultSet rs = ps.executeQuery();
  if (rs.next()) {
    int aaa = rs.getInt("Day");
    System.out.println(aaa);
    if (aaa == k) // check whether aaa(day) is equal to k (comboBox)
    {
      String sql1 = "Select * from placeseen where ID =?";
      DatabaseConnection db1 = new DatabaseConnection();
      Connection conn1 = db1.getConnection();
      PreparedStatement ps1 = conn1.prepareStatement(sql1);
      ps1.setInt(1, highestIndex);
      ResultSet rs1 = ps1.executeQuery();
      if (rs1.next()) {
        String a = rs1.getString("place1");
        String bbb = rs1.getString("place2");
        Tourism to = new Tourism();
        to.setPlace1(a);
        to.setPlace2(bbb);
        DispDay dc = new DispDay();
        dc.setVisible(true);
      }
      ps1.close();
      rs1.close();
      conn1.close();
    } else // if not equal
    {

      int secIndex = 0;
      for (int i = 0; i < aa.length; i++) {
        if (aa[i] > secHighest) {
          secHighest = aa[i];
          secIndex = i;
        }
      }
      System.out.println("The second highest value is " + secHighest + "");
      System.out.println("It is found at index " + secIndex + "");
      String sql2 = "Select Day from menu where ID =?";
      DatabaseConnection db2 = new DatabaseConnection();
      Connection conn2 = db2.getConnection();
      PreparedStatement ps2 = conn.prepareStatement(sql2);
      ps2.setInt(1, secIndex);
      ResultSet rs2 = ps.executeQuery();
      if (rs2.next()) {
        int de = rs2.getInt("Day");
        System.out.println(de);
        if (de == k) {
          String l = "Select * from placeseen where ID =?";
          DatabaseConnection db3 = new DatabaseConnection();
          Connection conn3 = db3.getConnection();
          PreparedStatement ps3 = conn3.prepareStatement(l);
          ps3.setInt(1, secIndex);
          ResultSet rs3 = ps3.executeQuery();
          if (rs3.next()) {
            String a = rs3.getString("place1");
            String bbb = rs3.getString("place2");
            Tourism to = new Tourism();
            to.setPlace1(a);
            to.setPlace2(bbb);
            DispDay dc = new DispDay();
            dc.setVisible(true);
          }
          ps3.close();
          rs3.close();
          conn3.close();
        }
      }
    }
    ps.close();
    rs.close();
    conn.close();
  }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • 3
    Consider improving your question so that it is more easily understood. This may require quite a bit of work on your part, but it will be worth your while. – Hovercraft Full Of Eels Aug 15 '15 at 04:13
  • @HovercraftFullOfEels edited.. – John Joe Aug 15 '15 at 16:25
  • Are you asking for help with your algorithm or with your code? Generally we prefer to see the smallest piece of code that can reliably reproduce the issue, without any extra bits. – Roddy of the Frozen Peas Aug 15 '15 at 16:30
  • Before the system display the highest value, I want it to check whether the comboBox(user input) value is equal to day (k) which is located in sql. It will only display if it is equal. If not, it will find from second highest and so on... – John Joe Aug 15 '15 at 16:40
  • @user5156075 If I understand question correctly, would not it be easier to find the highest value having day equal to k and then find its index? – Kryptonian Aug 15 '15 at 17:08

1 Answers1

0

Your code is somewhat difficult to understand, bit it sounds like you are trying to obtain the "highest" database value (in some sense) whose value matching some user input.

At the most basic level, consider constructing a basic query that looks like:

SELECT MAX(value column) FROM menu JOIN placeseen ON (conditions) WHERE (condition to ensure that data matches input)

If that's possible, it's a high-performance way to ensure that the data lines up between the tables and also matches the user input.