I am using Java on NetBeans 8. I have searched google and other stack overflow questions but I cannot find anything that relates to my question. Also, I have consulted the oracle documentation and their example gave me the same error I am experiencing.
I would like the array string values below to print out to the output box using a line of code like:
System.out.println(pbArray[0][0] + pbArray[1][0]);
////////////////////////////////////////////////
But no matter where I try to input that line of code I cannot make it work. NetBeans tells me there is "an identifier expected."
package com.mnlottery.console;
public class ComMnlotteryConsole {
public static void main(String[] args) {
class powerball{
String[][] pbArray = {
{"August 16th, 2014","August 13th, 2014","August 9th, 2014"},
{"07, 08, 17, 48, 69, 09, $50,000,000","08, 37, 39, 40, 52, 24,"
+ " $40,000,000","03, 12, 31, 34, 51, 24, 90,000,000"}};
}
}
}
I feel like this should be fairly simple. Thanks in advance.
I think you wanted (no inner class)
public static void main(String[] args) {
String[][] pbArray = {
{ "August 16th, 2014", "August 13th, 2014", "August 9th, 2014" },
{ "07, 08, 17, 48, 69, 09, $50,000,000",
"08, 37, 39, 40, 52, 24," + " $40,000,000",
"03, 12, 31, 34, 51, 24, 90,000,000" } };
System.out.println(pbArray[0][0] + pbArray[1][0]);
}
But, you can print the array(s) in a few ways,
Arrays.deepToString(Object[])
A loop, and Arrays.toString(Object[])
like
String[][] pbArray = {
{ "August 16th, 2014", "August 13th, 2014", "August 9th, 2014" },
{ "07, 08, 17, 48, 69, 09, $50,000,000",
"08, 37, 39, 40, 52, 24," + " $40,000,000",
"03, 12, 31, 34, 51, 24, 90,000,000" } };
// 1.
System.out.println(Arrays.deepToString(pbArray));
// or 2.
for (String [] arr : pbArray) {
System.out.println(Arrays.toString(arr));
}
Change System.out.println(pbarray[0][0] + names[1][0]); to
System.out.println(pbArray[0][0] + pbArray[1][0]);
names and pbarray do not exist. pbArray is the correct identifier.
Related
I am using Spring webflux and default project reactor. Each http request will poll the ConcurrentLinkedQueue and get a unique number, if the queue becomes empty all the request should wait for the queue to be populated (I am looking at smart way to refill the queue as part of the reactive chain of the first request which found queue is empty and make all the other requests wait for the queue to be replenished). I am find it hard to implement it in spring webflux and project reactor. By way, during the app startup I have event lister to populate the queue on app load. This problem will be prominent once the queue is empty. Any help here will be much appreciated. Here is the minified version of the code...
package com.learning;
import reactor.core.publisher.Mono;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
public class UniqueNumberGenerator {
Queue<Long> uniqueNumberCache = new ConcurrentLinkedQueue<>();
static private Long sequence = 0l;
public static void main(String arg[]) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(9);
UniqueNumberGenerator uniqueNumberGenerator = new UniqueNumberGenerator();
for (int i = 0; i < 15; i++) {
executorService.submit(() -> {
uniqueNumberGenerator.getUniqueNumber()
.doOnNext(number -> System.out.println("Consumed number : " + number))
.subscribe();
});
}
Thread.sleep(5000);
}
public Mono<Long> getUniqueNumber() {
Long orderId = uniqueNumberCache.poll();
if(orderId != null) {
return Mono.just(orderId);
} else {
return generateUniqueNumbers();
}
}
/**
* This is where the problem lies, When the queue is empty in the above method,
* All thw thread are concurrently trying to get the db sequence and generating the
* unique numbers and populating the queue,
* where I want to only one thread to populate the queue and make all other to wait for it
*
* #return
*/
public Mono<Long> generateUniqueNumbers() {
Mono<Long> sequenceMono = getSequence()
.flatMap(nextVal -> {
var list = LongStream.rangeClosed(1, 9)
.boxed()
.map(value -> Long.valueOf(nextVal + "" + value))
.collect(Collectors.toList());
System.out.println("Generated number " + list);
var nextOrderId = list.remove(0);
uniqueNumberCache.addAll(list);
return Mono.just(nextOrderId);
});
return sequenceMono;
}
/**
* This method is actually backed by db sequence
*
* #return
*/
public Mono<Long> getSequence() {
return Mono.create(longMonoSink -> {
synchronized (sequence) {
sequence = sequence + 1;
//System.out.println("sequence : " + sequence);
longMonoSink.success(sequence);
}
});
}
}
Output
Generated number [11, 12, 13, 14, 15, 16, 17, 18, 19]
Generated number [31, 32, 33, 34, 35, 36, 37, 38, 39]
Generated number [41, 42, 43, 44, 45, 46, 47, 48, 49]
Generated number [51, 52, 53, 54, 55, 56, 57, 58, 59]
Generated number [21, 22, 23, 24, 25, 26, 27, 28, 29]
Consumed number : 31
Consumed number : 11
Consumed number : 21
Consumed number : 41
Consumed number : 51
Generated number [61, 62, 63, 64, 65, 66, 67, 68, 69]
Consumed number : 61
Generated number [71, 72, 73, 74, 75, 76, 77, 78, 79]
Consumed number : 71
Generated number [81, 82, 83, 84, 85, 86, 87, 88, 89]
Consumed number : 81
Generated number [91, 92, 93, 94, 95, 96, 97, 98, 99]
Consumed number : 91
Consumed number : 17
Consumed number : 12
Consumed number : 13
Consumed number : 16
Consumed number : 14
Consumed number : 15
By looking at above example it is very clear that all the threads are populating the queue. There are around 90 (each thread generated 10 numbers ) unique numbers generated for 15 requests
Odoo : the following code returns a Array of 'java.lang.Integer' ids.
asList((Object[])models.execute("execute_kw", asList(
db, uid, password,
"res.partner", "search",
asList(asList(
asList("is_company", "=", true),
asList("customer", "=", true))))));
[7, 18, 12, 14, 17, 19, 8, 31, 26, 16, 13, 20, 30, 22, 29, 15, 23, 28, 74]
Which reverse call will return the structure and values of each Object associated to each ids?
Its the read command.
final List ids = asList((Object[])models.execute(
"execute_kw", asList(
db, uid, password,
"res.partner", "search",
asList(asList(
asList("is_company", "=", true),
asList("customer", "=", true))))));
final Map record = (Map)((Object[])models.execute(
"execute_kw", asList(
db, uid, password,
"res.partner", "read",
asList(ids)
)
))[0];
work through those examples:
https://www.odoo.com/documentation/13.0/webservices/odoo.html
I have a tree set. How to find the root element of the treeset:
TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(8);
ts.add(3);
ts.add(8);
ts.add(1);
ts.add(0);
ts.add(4);
ts.add(7);
If we are taking the below treeset, 20 is the root element:
If you absolute need (desire?) to know what the current root value is, you can use reflection.
Beware: Internal implementation of TreeSet and TreeMap may change and the following code will fail. The code has been tested with JDK 1.8u91.
Also, the code will fail with a SecurityException if a security manager is present.
The following two helper methods can be used to give you the root of a TreeSet and a TreeMap.
#SuppressWarnings("unchecked")
public static <E> E getTreeRoot(TreeSet<E> ts) {
try {
Field mField = TreeSet.class.getDeclaredField("m");
mField.setAccessible(true);
return getTreeRoot((TreeMap<E, Object>) mField.get(ts));
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException("Internals of TreeSet has changed", e);
}
}
#SuppressWarnings("unchecked")
public static <K,V> K getTreeRoot(TreeMap<K,V> tm) {
try {
Field rootField = TreeMap.class.getDeclaredField("root");
rootField.setAccessible(true);
Map.Entry<K,V> root = (Map.Entry<K,V>) rootField.get(tm);
return (root == null ? null : root.getKey());
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException("Internals of TreeMap has changed", e);
}
}
Test Helper
private static void test(int... values) {
TreeSet<Integer> ts = new TreeSet<>();
System.out.println("Root is " + getTreeRoot(ts) + " for " + ts);
for (int v : values) {
ts.add(v);
System.out.println("Root is " + getTreeRoot(ts) + " for " + ts);
}
}
Test 1 (values in question)
test(8, 3, 8, 1, 0, 4, 7);
Root is null for []
Root is 8 for [8]
Root is 8 for [3, 8]
Root is 8 for [3, 8]
Root is 3 for [1, 3, 8]
Root is 3 for [0, 1, 3, 8]
Root is 3 for [0, 1, 3, 4, 8]
Root is 3 for [0, 1, 3, 4, 7, 8]
Test 2 (values in graph in ascending order)
test(5, 15, 17, 18, 20, 22, 25, 27, 30);
Root is null for []
Root is 5 for [5]
Root is 5 for [5, 15]
Root is 15 for [5, 15, 17]
Root is 15 for [5, 15, 17, 18]
Root is 15 for [5, 15, 17, 18, 20]
Root is 15 for [5, 15, 17, 18, 20, 22]
Root is 15 for [5, 15, 17, 18, 20, 22, 25]
Root is 18 for [5, 15, 17, 18, 20, 22, 25, 27]
Root is 18 for [5, 15, 17, 18, 20, 22, 25, 27, 30]
Test 3 (values in graph in descending order)
test(30, 27, 25, 22, 20, 18, 17, 15, 5);
Root is null for []
Root is 30 for [30]
Root is 30 for [27, 30]
Root is 27 for [25, 27, 30]
Root is 27 for [22, 25, 27, 30]
Root is 27 for [20, 22, 25, 27, 30]
Root is 27 for [18, 20, 22, 25, 27, 30]
Root is 27 for [17, 18, 20, 22, 25, 27, 30]
Root is 22 for [15, 17, 18, 20, 22, 25, 27, 30]
Root is 22 for [5, 15, 17, 18, 20, 22, 25, 27, 30]
As you can see, the root depends on the order of insertion.
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class Java87String {
public static void main(String[] args) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
//byte[] b = {-101, 53, -51, -26, 24, 60, 20, -31, -6, 45, 50, 103, -66, 28, 114, -39, 92, 23, -47, 32, -5, -122, -28, 79, 22, -76, 116, -122, -54, -122};
//byte[] b = {-76, -55, 85, -50, 80, -23, 27, 62, -94, -74, 47, -123, -119, 94, 90, 61, -63, 73, 56, -48, -54, -4, 11, 79};
byte[] b = { -5, -122, -28};
System.out.println("Input Array :" + Arrays.toString(b));
System.out.println("Array Length : " + b.length);
String target = new String(b,StandardCharsets.UTF_8);
System.out.println(Arrays.toString(target.getBytes("UTF-8")));
System.out.println("Final Key :" + target);
}
}
The above code returns the following output in Java 7
Input Array :[-5, -122, -28]
Array Length : 3
[-17, -65, -67]
Final Key :�
The Same code returns the following output in Java 8
Input Array :[-5, -122, -28]
Array Length : 3
[-17, -65, -67, -17, -65, -67, -17, -65, -67]
Final Key :���
Sounds like Java8 is doing the right thing of replacing with the default sequence of [-17, -65, -67].
Why is there a difference in output and Any Known bugs in JDK 1.7 which fixes this issue?
Per the String JavaDoc:
The behavior of this constructor when the given bytes are not valid in the given charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.
I think (-5, -122, -28) is a invalid UTF-8 byte sequence, so the JVM may output anything in this case. If it were a valid one, maybe the different Java versions could show the same output.
Does this specific byte sequence have a meaning? just curious
As part of my computer science IA I am creating a tool that reads match history and details of dota games and generates stats and hero stats. To do this I have accessed the valve API and grabbed a few jsons of matches and match history from it, then cut them down slightly so they only contain the information I need in the json.
Below is a sample of the details of one of the matches in a json format:
"result": {
"players": [
{
"account_id": 40884464,
"player_slot": 0,
"hero_id": 31,
"kills": 8,
"deaths": 8,
"assists": 14,
"last_hits": 72,
"denies": 0,
"gold_per_min": 304,
"xp_per_min": 412,
"level": 18,
},
{
"account_id": 70638797,
"player_slot": 1,
"hero_id": 35,
"kills": 6,
"deaths": 7,
"assists": 4,
"last_hits": 212,
"denies": 37,
"gold_per_min": 371,
"xp_per_min": 356,
"level": 17,
},
{
"account_id": 76281087,
"player_slot": 2,
"hero_id": 5,
"kills": 3,
"deaths": 13,
"assists": 10,
"last_hits": 22,
"denies": 0,
"gold_per_min": 215,
"xp_per_min": 259,
"level": 14,
},
{
"account_id": 4294967295,
"player_slot": 3,
"hero_id": 28,
"kills": 11,
"deaths": 11,
"assists": 11,
"last_hits": 166,
"denies": 18,
"gold_per_min": 413,
"xp_per_min": 485,
"level": 20,
},
{
"account_id": 81692493,
"player_slot": 4,
"hero_id": 2,
"kills": 1,
"deaths": 9,
"assists": 7,
"last_hits": 135,
"denies": 8,
"gold_per_min": 261,
"xp_per_min": 314,
"level": 16,
},
{
"account_id": 10101141,
"player_slot": 128,
"hero_id": 30,
"kills": 7,
"deaths": 8,
"assists": 25,
"last_hits": 90,
"denies": 2,
"gold_per_min": 382,
"xp_per_min": 421,
"level": 18,
},
{
"account_id": 62101519,
"player_slot": 129,
"hero_id": 7,
"kills": 6,
"deaths": 8,
"assists": 20,
"last_hits": 305,
"denies": 0,
"gold_per_min": 556,
"xp_per_min": 585,
"level": 22,
},
{
"account_id": 134700328,
"player_slot": 130,
"hero_id": 4,
"kills": 17,
"deaths": 2,
"assists": 13,
"last_hits": 335,
"denies": 16,
"gold_per_min": 729,
"xp_per_min": 724,
"level": 25,
},
{
"account_id": 35357393,
"player_slot": 131,
"hero_id": 83,
"kills": 4,
"deaths": 4,
"assists": 23,
"last_hits": 16,
"denies": 4,
"gold_per_min": 318,
"xp_per_min": 407,
"level": 18,
},
{
"account_id": 4294967295,
"player_slot": 132,
"hero_id": 101,
"kills": 13,
"deaths": 8,
"assists": 12,
"last_hits": 57,
"denies": 3,
"gold_per_min": 390,
"xp_per_min": 405,
"level": 18,
}
]
,
"radiant_win": false,
"duration": 2682,
"start_time": 1461781997,
"match_id": 2324299045,
"match_seq_num": 2036251155,
"cluster": 133,
"game_mode": 1,
"flags": 0,
"engine": 1,
"radiant_score": 30,
"dire_score": 48
}
Using an intelliJ plugin I have created 3 Java classes, one with the match result, one for the details of the result, and one for the details of the players within the result, each with the variables gets sets in:
TestMatch fields:
private TestMatchResult result;
TestMatchResult fields:
private int duration;
private int start_time;
private int cluster;
private boolean radiant_win;
private int match_seq_num;
private int engine;
private TestMatchResultPlayers[] players;
private long match_id;
private int dire_score;
private int flags;
private int game_mode;
private int radiant_score;
TestMatchResultPlayers fields:
private int kills;
private int gold_per_min;
private int last_hits;
private int account_id;
private int assists;
private int level;
private int player_slot;
private int xp_per_min;
private int hero_id;
private int denies;
private int deaths;
I have downloaded and added the gson library as a dependency into the intelliJ project.
I am trying to parse the json into the java classes as an object and would like to do that for all the match jsons, however I am not quite sure how to do that at the moment, all I have is:
public static void getMatch()
{
Gson gson = new Gson();
}
Could someone who understands gson better than myself give me a little bit of guidance as to how I'd go about parsing that json into the class(es) as an object for several match jsons? Once I've done that the rest of what I need to do is easy since it's just a case of taking the variables and running calculations on them then displaying them. If it's not possible or practical I can make a test CSV and read from that instead as I know how to use them, but only just come across jsons as that is what the valve API returns requests in so figured I may as well learn how to use them.
Thanks!
you need to use the method Gson.fromJson()
Example:
public static void getMatch()
{
Gson gson = new Gson();
TestMatch tm = gson.fromJson(jsonString, TestMatch.class);
}