i am facing the problem how to retrieve the value from ArrayList. I pass in the value with this code to remove duplicate:
Set<List<String>> allTCP=new LinkedHashSet<List<String>>();
for(int x=0; x < TCPSourceIP.size();x++){
allTCP.add(Arrays.asList(new String[]{TCPSourceIP.get(x),TCPSourcePort.get(x),TCPDestIP.get(x),TCPDestPort.get(x)}));
}
But now the problem i facing is i do not know how to retrieve the value. The value i pass in is 10.1.1.1, 123, 10.1.1.2, 234 If i use System.out.println(allTCP), then i cannot use each of the value inside the List.I need to get the single value from the array to do proper output such as, The TCP Source IP is 10.1.1.1 with port 123 to Destination IP 10.1.1.2 with port 234. Previously i use this code to do output but it did not remove duplicate.
for(int x=0; x < TCPSourceIP.size(); x++){
jTextArea1.append(x+1+")IP " + TCPSourceIP.get(x) +" is sending packet using TCP Port "+
TCPSrcPort.get(x) + " to IP " + TCPDestIP.get(x) + "(" + TCPDestPort.get(x) +")" +
" which is non-standard port\n");
Can any one give me suggestion on how to retrieve the value from the ArrayList in order for me to do a proper output. Help would be appreciated.
In cases like that I prefer to have the values encapsulated in to an object with getters/setters and put instances of those to the lists / hashes:
class Route {
private String sourceIp;
private Long sourcePort;
private String destIp;
private Long destPort;
public Route(Sting src, Long sport, String dest, Long dport) {
setSourceIp(src);
...
... etc.
And then
ArrayList<Route> allTcp = new ArrayList<Route>();
...
allTcp.add(new Route(src, sport, dst, dport) );
This concept would then enable an easy way to find any source, destination, port etc. by putting the complex comparison methods inside class Route.
Any changes to the inner set of Route doesn't interfere with the caller(s).
Related
My need is to read the color of a text with PDFlib TET.
As a basis I'm using this PDFlib example: https://www.pdflib.com/tet-cookbook/tet_and_pdflib/search_and_replace_text/
Before both result.add(new rectangle(...)) calls I'm trying to read the color like this:
String csname = tet.pcos_get_string(doc, "colorspaces[" + tet.colorspaceid + "]/name");
if ("Separation".equals(csname)) {
String type = tet.pcos_get_string(doc, "type:colorspaces[" + tet.colorspaceid + "]/colorantname");
System.out.println(type);
if (StringUtils.equalsIgnoreCase("name", type)) {
System.out.println(tet.pcos_get_string(doc, "colorspaces[" + tet.colorspaceid + "]/colorantname"));
}
}
Unfortunately tet.colorspaceid is always 0.
But the correct colorspaceid is 6 (with "correct" = the index of the color the text actually is written with). I know the indexes because I iterated over all colorspaces like this and for i=6 the system prints the name of the intended color:
String type = tet.pcos_get_string(doc, "type:colorspaces[" + i + "]/colorantname");
if (StringUtils.equalsIgnoreCase("name", type)) {
System.out.println(tet.pcos_get_string(doc, "colorspaces[" + i + "]/colorantname"));
}
What do I need to do for tet.colorspaceid being the id of the colorspace of the currently found word fragment?
Or am I completely wrong and TET reads the color somehow else?
Found it - the solution is method print_color_value in this example: https://www.pdflib.com/tet-cookbook/text/glyphinfo/
Just copy method print_color_value, return csname (or colorantname in the if blocks) and rename the method to e.g. getColorValue.
If needed throw away the formatter stuff.
So, I started learning Java and was wondering how parallel arrays of string and int type could be stored exactly once from the source arrays. For example, I have two arrays parallel to each other, one stores the Phone number as a string and the other stores the duration of the calls as a/an int gotten from each phone number.
String[] phoneNumbers;
phoneNumbers = new String[100];
int[] callDurations = new int[phoneNumbers.length];
int size = 0;
phoneNumbers[0] = "888-555-0000";
callDurations[0] = 10;
phoneNumbers[1] = "888-555-1234";
callDurations[1] = 26;
phoneNumbers[2] = "888-555-0000";
callDurations[2] = 90;
phoneNumbers[3] = "888-678-8766";
callDurations[3] = 28;
size = 4;
I wrote a method to find the details of a specific phone number, such as the duration of the specific call "888-555-1234"
Here is the method and how I called it:
public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber) {
int match;
System.out.println("Calls from " + targetNumber + ":");
match = find(phoneNumbers, size, 0, targetNumber);
while (match >= 0) {
System.out.println(phoneNumbers[match] + " duration: " + callDurations[match] + "s");
match = find(phoneNumbers, size, match + 1, targetNumber);
}
}
System.out.println("\n\nAll calls from number: ");
findAllCalls(phoneNumbers, callDurations, size, "888-555-1234");
The output of this code is:
All calls from number:
Calls from 888-555-1234:
888-555-1234 duration: 26s
888-555-1234 duration: 28s
Process finished with exit code 0
Whereas,the output I want to get instead is:
All calls from number:
Calls from 888-555-1234:
888-555-1234 duration: 54s
Process finished with exit code 0
(26s + 28s)
How is it possible in java to make sure there are no duplicates stored in a parallel array and get total duration for each phone number instead of having them separately in the arrays?
As already stated in the answers before, you can use a map - will avoid duplicates in both phoneNumber and callDuration (Java code to Prevent duplicate <Key,Value> pairs in HashMap/HashTable).
Or, if you want to stick with the String implementation, you can change the logic in the findAllCalls() method.
public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber)
{
int match;
System.out.println("Calls from " + targetNumber + ":");
//match = find(phoneNumbers, size, 0, targetNumber);
int i = 0, duration = 0;
while (i<size)
{
if(phoneNumbers[i].equals(targetNumber))
duration+=callDurations[i];
i++;
//System.out.println(phoneNumbers[match] + " duration: " + callDurations[match] + "s");
//match = find(phoneNumbers, size, match + 1, targetNumber);
}
System.out.println(targetNumber+" duration : "+duration+"s");
}
The question was: "How is it possible in java to make sure there are no duplicates stored in a parallel array and get total duration for each phone number instead of having them separately in the arrays?"
The answer is: There is no (inexpensive) way.
Use a hash map instead. Have a look at java.utils.HashMap. A hash map is a concept to store values (of any kind) associated to a specific key. In your case the values would be the durations, the keys would be your phone number. Therefor you should use a String-Integer hash map here.
On insert do the following:
For each phone number-duration pair do:
Is there already an element in the HashMap of the specified key?
No -> Add phone number and duration
Yes ->
Get the duration stored
Add the current duration to the stored duration
Overwrite the existing item with the new duration calculated
Later you efficiently can perform a lookup.
A Map is an object that maps keys to values
In your case, you want phone numbers (stored in a String) to correspond to call duration (ints). Therefore, you'd declare your HashMap as follows (Note you can't instantiate Map, it is an interface):
Map<String, Integer> callRecords = new HashMap<String, Integer>();
This is a better version because you no longer need to keep track of two different arrays. Now, instead of
phoneNumbers[0] = "888-555-0000";
callDurations[0] = 10;
You can write:
callRecords.put("888-555-0000", 10);
I am trying to change the URL of google maps directions from directions which have multiple waypoints to directions where these intermediate waypoints are deleted but the route remains the same.
Specifically from: https://www.google.nl/maps/dir/51.804323,5.8061076/51.8059489,5.7971745/51.8095767,5.8032703/#51.8068221,5.806553,16.5z/data=!4m2!4m1!3e2
to:
https://www.google.nl/maps/dir/51.804323,5.8061076/51.8095767,5.8032703/#51.8069622,5.8023697,17z/data=!4m9!4m8!1m5!3m4!1m2!1d5.7971218!2d51.8060231!3s0x47c7061292e15b39:0x4d7bcd7484c71cf3!1m0!3e2
EDIT: because I had to drag the route manually in the second URL the coordinates of the middle marker are not exactly the same as in the first URL; this difference can be ignored.
the start part of these URLS seem pretty obvious as to what they are doing, however the data parameter is still unclear to me (without it the route is not correct). I tried the Google Maps API, but these return an XML or JSON file, but I just need the corresponding URL which I would also get using the webinterface of Google Maps.
How can I tranform the first URL to the second??
So after a long time trying to figure out how the URL scheme works, I finally figured out how it works (for the directions interface).
The URL consists of the following steps:
You start off with "https://www.google.nl/maps/dir/"
This is followed by the start coordinates in the form "[LAT],[LONG]", the coordinates of intermediate waypoints in the same format,and then the coordinates of end points. All these coordinates are seperated by a "/" character.
This is followed by "#[LAT],[LONG],[ZOOM]/" where LAT LONG are the coordinates of the viewbox and ZOOM is the level of zoom (lower means more zoomed out).
This is followed by "data=" and then "!4m[(5x+4+y)]!4m[(5x+3+y)]!" where x is the amount of VIA-points and y is the amount of intermediate waypoints in the route. So if you have a route from A to D with intermediate destinations B and C and VIA points Q, W and R you have x=3 and y=2 so you get the string "!4m21!4m20"
Next we get all VIA points. This done in the following scheme: you append "!1m[(5x)]" where x is the amount of VIA-points between the current waypoint and the next. So "!1m5...[data]...!1m0" means that between the start and first waypoint there is one VIA-point and between the first waypoint and the end there are no VIA-points. Each "!1m[(5x)]" is followed by x instances of "!1d[LONG]!2d[LAT]!3s[COORDINATE]". I am not entirely sure what COORDINATE does, but is has to be in the format "0x[HEX]:0x[HEX]" where HEX is a hexadecimal number; I simply take the number 0 for this. This seems to work in all my test cases and does not seem to influence anything.
This is then followed by "!1m0". I believe this is necessary to indicate that after the last waypoint (the finish) there are no more VIA points, which is useless information but needed nevertheless.
Finally, we get the last parameter which looks like "!3e[n]" where n is a discrete variable to indicate the type of navigation: n=0 for driving by car,n=1 is for bicycle riding, n=2 is for walking, and n=3 for public transportation.
That is mostly it for what I found out about the URL scheme by testing it relentlessly. There are more parameters you can add, but that needs more work testing.
Finally, I included my implementation for transforming a URL with 0 or more waypoints and 0 or more VIA-points to a URL containing only VIA-points. Feel free to use it and please let me know if you have found any mistakes so I can fix them.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter URL: ");
String originalURL = br.readLine();
//get start of URL
String start = "https://www.google.nl/maps/dir/";
//get navigation type
String type = "!3e1";
Matcher t = getMatcher(originalURL, "!3e\\d");
if (t.find()) {
type = t.group();
}
//get viewbox parameter
Matcher v = getMatcher(originalURL, "#[-]?[\\d]+\\.[\\d]+,[-]?[\\d]+.[\\d]+,[-]?[\\d]+[[.]+[\\d]+]*z");
v.find();
String viewbox = v.group();
//get order of points when using VIA
String data = originalURL.substring(originalURL.indexOf("/data=") + 6);
ArrayList<String> order = new ArrayList<>();
Matcher o = getMatcher(data, "!1m[\\d]+");
while (o.find()) {
order.add(o.group());
}
if (order.size() > 0) {
//remove the last element which is always m0 as this should not be
//displayed in the VIA-list
order.remove(order.size() - 1);
}
//!1m2 does not represent the order but indicates that coordinates that are coming up
order.removeIf(a -> a.equals("!1m2"));
//get coordinates of via-points
ArrayList<String> originalViaPoints = new ArrayList<>();
Matcher c = getMatcher(data, "!1d[-]?[\\d]+.[\\d]+!2d[-]?[\\d]+.[\\d]+");
while (c.find()) {
String[] g = c.group().substring(3).split("!2d");
originalViaPoints.add(g[1] + "," + g[0]);
}
//get coordinates of start, end and intermediate points
originalURL = originalURL.substring(0, v.start());
ArrayList<String> waypoints = new ArrayList<>();
Matcher p = getMatcher(originalURL, "[-]?[\\d]+\\.[\\d]+,[-]?[\\d]+.[\\d]+");
while (p.find()) {
waypoints.add(p.group());
}
//start and end must be displayed seperately
String bound = waypoints.get(0) + "/" + waypoints.get(waypoints.size() - 1);
//add intermediate waypoints and via-points to a list of VIA points
ArrayList<String> viaPoints = new ArrayList<>();
//we have VIA points to process
if (!order.isEmpty()) {
int via_index = 0;
int wp_index = 1;
for (String step : order) {
int iter = Integer.valueOf(step.substring(3)) / 5;
for (int i = 0; i < iter; i++) {
viaPoints.add(originalViaPoints.get(via_index++));
}
viaPoints.add(waypoints.get(wp_index++));
}
} else //There are only waypoints in the URL
{
for (int i = 1; i < waypoints.size() - 1; i++) {
viaPoints.add(waypoints.get(i));
}
}
//calculate prefix according to the amount of nodes of the via points
int nodes = viaPoints.size();
String prefix = "!4m" + (5 * nodes + 4) + "!4m" + (5 * nodes + 3) + "!1m" + (5 * nodes);
//get nodes string
String viaString = "";
for (String node : viaPoints) {
viaString += "!3m4!1m2";
String[] pieces = node.split(",");
viaString += "!1d" + pieces[1]; //ALERT: the coordinates are flipped!
viaString += "!2d" + pieces[0];
viaString += "!3s0x0:0x0";
}
String url = start + bound + "/" + viewbox + "/data=" + prefix + viaString + "!1m0" + type;
According to this site, in the old url scheme, there should be 3 ways to add a via point or a route, and they are:
https://www.google.com/maps?dirflg=w&saddr=51.804323,5.8061076&daddr=51.8059489,5.7971745+to:51.8095767,5.8032703
https://www.google.com/maps?dirflg=w&saddr=51.804323,5.8061076&daddr=51.8095767,5.8032703&mrad=51.8059489,5.7971745
https://www.google.com/maps?dirflg=w&saddr=51.804323,5.8061076&daddr=51.8095767,5.8032703&via=51.8059489,5.7971745
But it seems they dropped support to mrad and via. And for using to, it shows the address as if it would be shown in the new url scheme.
For the new URL scheme.. it does not seems to have a lot of documentation on it, so I am not sure if Google wants you to play with it. but... here it is: How to do it with the new scheme.
according to this blog post:
the !xx, is a separator. Looking at your url:
data=
!4m9
!4m8
!1m5
!3m4
!1m2
!1d5.7971218
!2d51.8060231
!3s0x47c7061292e15b39:0x4d7bcd7484c71cf3
!1m0
!3e2
it is really unclear what it is doing, but, at least we see your via lat, and via lng in the !1d and !2d fields;
Also the !3s, in a hex format looks like some kind of lat/lng, might be the area of search. This is how it looks like in dec 5172109373901724473:5583282063383403763
Well, in short, just change the !1d and !2d fields and it seems to work fine. like this:
https://www.google.nl/maps/dir/51.804323,5.8061076/51.8095767,5.8032703/#51.8769532,5.8550939,7.58z/data=!4m9!4m8!1m5!3m4!1m2!1d5.871218!2d52.8060231!3s0x47c7061292e15b39:0x4d7bcd7484c71cf3!1m0!3e2
As the title says , for example I have the value 02 I want the first zero to stay there so I can control my pendingIntent.
my program basically has a list , every item has it's own options , the only way I can control alertmanager is by knowing the exact unique id so I can cancel the right notification , but I couldn't come up with a solution other than this:
int FirstListPosition = getArguments().getInt(EXTRA_FirstListPosition);
int InnerListPosition = getArguments().getInt(EXTRA_PASSEDPOSITION);
String Merge = Integer.toString(FirstListPosition) + Integer.toString(InnerListPosition);
int FinalValue = Integer.parseInt(Merge);
so if I click the first item in my main list , the FirstListPosition will be 0 and if I click the third item in my list inside of the main list the InnerListPosition will be 2
so the string merge will be = 02 , this way the unique id will never be the same and I can actually get the position of the two if the user wanted to cancel a specific pendingintent " notification "
hopefully you guys understood what I meant
Have a look at NumberFormat. This class is intended to be used to format number according to a specific pattern.
If you want to encode the position in two lists into a single int value (and the number of choices in the second list is, say, less than 1000 you might use:
int encodedListPositions;
int list1Position;
int list2Position;
int encodedListPosition = list1Position*1000 + list2Position;
...
list1Position = encodedListPosition / 1000;
list2Position = endocedListPosition - 1000*list1Position;
I'm getting a "Cannot be resolved or is not a field" error on a variable in one of my Java classes, and I don't understand why... I've had a look online, but can't find anything that really explains why I'm getting it. The error is happening in the following for loop:
int i;
getFilterConditions();
for(i = 0; i < sitesToBeFiltered.size(); i++){
if(sitesToBeFiltered.get(i) == filter1Value){
Gui.displayFilteredOutput.append("\n");
Gui.displayFilteredOutput.append("EID: [" + sitesToBeFiltered.get(i) + ", " + applicationsToBeFiltered.get(i) + ", " + IDsToBeFiltered.get(i) + "]. ");
Vector3Double filteredEntityPosition =
Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + sitesToBeFiltered.get(i).positionsToBeFilteredX.get(i));
}
}
It's being generated on the positionsToBeFilteredX.get(i) variable at the end of the for loop. I have defined that variable as a global variable at the top of the class using the line:
public static ArrayList<Double> positionsToBeFilteredX = new ArrayList<Double>();
To explain what I'm trying to do here:
I have a program that is reading the PDUs that are being sent/ received over a network, and storing both the PDUs themselves, and information held by each of the PDUs in a number of ArrayLists. What I'm trying to do with this code, is to take a value entered by the user on a form (stored in the filter1Value integer variable), and check whether that value is equal to any of the elements in a particular ArrayList (sitesToBeFiltered).
So, I am looping through the sitesToBeFiltered ArrayList, and checking every element to see whether it is exactly equal to the value of filter1Value. If it is, I am then appending some text about the matching ArrayList element to a JTextArea (displayFilteredOutput).
One of the things I want to add to the JTextArea is the X position of the matching element (which was added to the positionsToBeFilteredX when it was found that that the element matched the user's search criteria.
So what I'm trying to do with the last line of code is to append the X coordinate (stored in an array of X coordinates) of the matching element in the sitesToBeFiltered ArrayList, to the displayFilteredOutput JTextArea, but for some reason, I'm getting this "cannot be resolved, or is not a field" compile error on the variable.
Can anyone explain to me why this is? I suspect that I am not referencing the X coordinate of the element matching the filter value correctly, but I'm not sure how I should be doing this... Could someone point me in the right direction?
Your code is written as though positionsToBeFiltered is a field in the object returned by sitesToBeFiltered.get(i). Evidently it isn't.
Should have seen it sooner: the issue was because I was trying to assign the value to a variable that was of an incompatible type. To solve this, I just needed to append the value to the JTextArea in the Gui without assigning it to a variable first: i.e. instead of writing
Vector3Double filteredEntityPosition = Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + positionsToBeFiltered.get(i);
I just needed to write:
Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + positionToBeFiltered.get(i) + "]. ");