Reputation:
And how to compare values with that of the values retrieved from database to conclude into just 1 result?
I am new to programming, please be kind.
below is my complete function:-
public String startpgm(String x){
String p = null;
double West = 2;
double North = 3;
double South = 4;
double East = 4;
String w =null;
String n =null;
String s =null;
String e =null;
TreeMap<Double, Collection<String>> names = new TreeMap<Double, Collection<String>>();
put(names, West, "west");
put(names, North, "north");
put(names, South, "south");
put(names, East, "east");
Collection<String> maximumPriorityValues = names.lastEntry().getValue();
for(String value : maximumPriorityValues){
System.out.println("Priority:"+value);
p=value;
while(value.equals(maximumPriorityValues)){
w= value.substring("west");
n= value.substring("north");
s= value.substring("south");
e= value.substring("east");
}
}
System.out.println("Current Priority:"+w+","+n+","+s+","+e);
String qrypriority = "select priority from tbl_priority order by updated_at ASC LIMIT 1";
Statement stmt = (Statement) conn.createStatement();
ResultSet PastPriority=stmt.executeQuery(qrypriority);
if (PastPriority.equals(w)){
String qryupdate = "update tbl_priority set active=1 where priority='west'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(n)){
String qryupdate = "update tbl_priority set active=1 where priority='north'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(s)){
String qryupdate = "update tbl_priority set active=1 where priority='south'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(e)){
String qryupdate = "update tbl_priority set active=1 where priority='east'";
int b=stmt.executeUpdate(qryupdate);
}
String qrystatus = "select priority from tbl_priority order by updated_at ASC LIMIT 1";
ResultSet status=stmt.executeQuery(qrystatus);
if (!PastPriority.next()){
String qrypriority2 = "select priority from tbl_priority order by updated_at ASC LIMIT 2";
ResultSet PastPriority2=stmt.executeQuery(qrypriority);
if (PastPriority.equals(w)){
String qryupdate = "update tbl_priority set active=1 where priority='west'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(n)){
String qryupdate = "update tbl_priority set active=1 where priority='north'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(s)){
String qryupdate = "update tbl_priority set active=1 where priority='south'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(e)){
String qryupdate = "update tbl_priority set active=1 where priority='east'";
int b=stmt.executeUpdate(qryupdate);
}
}
return p;
}
I am beginner to programming, please do not disappoint me!! My problem is, i cannot obtain the data stored in "value" rather than just printing it. i want to store each data obtained in to variable for further process
What i am trying to do with this program is:-
My objective in a nutshell:- to identify the highest priority side & when multiple values are returned compare the existing priority and conclude at which side have to be provided with priority, i.e updating 'tbl_priroty' with 'active=1' for the respective side.
Thanks in advance.
Upvotes: 1
Views: 133
Reputation: 1123
Here is what I would do.
NavigableMap<Double, List<String>> priorityMap = new TreeMap<Double, List<String>>();
// create a map that contains priorities and names
Afterwards fill the map. To do so I would recommend using a different data structure for reading, e.g.,
double[] priorities = new double[] {2, 3, 4, 4};
String[] names = new String[] {"west", "north", "south", "east"};
Because if you use priorityMap.put(4, "String1");
followed by priorityMap.put(4, "String2");
you overwrite the first put.
So:
for (int i = 0; i < priorities.length; i++) {
// if the priority, i.e., the String is no key so far
if (!priorityMap.contains(priorities[i])
// we create a new List that saves the names containing
// those which belong to the priority.
priorityMap.put(priorities[i], new ArrayList<String>());
// afterwards we add the name to the priority list
priorityMap.get(priorities[i]).add(names[i]);
}
Now you have the map that contains all your priorities and their respective names.
List<String> highestPriorityNames = priorityMap.lastEntry();
for (String name : highestPriorityNames) {
// do whatever you want with the high priority name
}
Additionally, this loop makes no sense
while(value.equals(maximumPriorityValues)){
w= value.substring("west");
n= value.substring("north");
s= value.substring("south");
e= value.substring("east");
}
You cannot compare a String
with a Collection
or List
. As it will never be true
. You do not have to check whether highestPriorityNames.contains(name)
, because you loop through all names that are within this list.
Upvotes: 1