I am trying to get a list of all users where the access level is between 0 and 10. The developer users have am higher access level, because they should be able change very critical data.
The current version returns all users.
List<UserType> userList = UserType.find.all();
Is it possible to do like that:
select *
from user
where access_level >= 0
and
access_level <=10
;
The comparision value is:
UserType.getAccessLevel
The table is called 'acces_level' in the database.
I'm not at home therefore I'm not able to test it but I'm sure you will need ge()
List<UserType> userList = UserType.find.where().ge("access_level", 10).findList();
I think it's a combination of Junction and greater than equals to
Having a quick glance at the JPA examples for Playframework, it seems that this is perfectly within the realm of possibility.
To express it, you'd need to do something like this:
List<UserType> userList = UserType.find("access_level >= 0 AND access_level < 10");
This is a version witch works, but I want to know how it works with the find Method.
for(int i = 0; i< userList.size(); i++){
if(userList.get(i).getAccessLevel() > 10){
userList.remove(i);
i--;
}
}
Related
I am trying to get the last ID inserted on the Java Derby DataBase.
This is a little program that i did, but i think that could be better ways to do that:
Query queryUsuarios = em.createNamedQuery("Usuario.findAll");
List<Usuario> listUsuario = queryUsuarios.getResultList();
int i=0;
for (i = 0; i < listUsuario.size(); i++) {
Usuario verUsuario = em.find(Usuario.class, i);
if(verUsuario != null) {
System.out.println(i+")"+verUsuario.getNombres());
}
}System.out.println("The last ID is "+i);
The main question is, there is a better and more secure way to do this?. Because i think on this way i could get errors in the future...
Thank you!.
You can do it using LIMIT and order by.Check below:
SELECT * FROM `table_name` ORDER BY `table_id` desc LIMIT 1;
I am having a weird issue with java list. Please see the code below:
for ( int i=0; i < tripList.size(); i++ ) {
ModeChoiceTrip trip = tripList.get(i);
int newUniqueId = tripListIds[trip.uniqueId];
int newLinkedId = trip.linkedId >= 0 ? tripListIds[trip.linkedId] : -1;
int jointTripNum = trip.linkedId >= 0 && trip.tourType != TourTypes.SPECIAL_EVENT ? jointTripListIds[trip.linkedId] : 0;
trip.uniqueId = newUniqueId;
trip.linkedId = newLinkedId;
trip.jointTripNum = jointTripNum;
}
In the above code, the values in tripList seem correct but after executing a few iterations (up to i = 6), the values in tripList changes for all the positions.
I cannot provide the whole source code here but showing the snippet of the code where I have an issue.
I found that there are some duplicate trips in tripList. When one of the trips is changed, the copy of it (located at different position) is also changed.
I am guessing this piece of code is executed by multiple threads, Then there is every chance that List could be modified by another thread while this loop is going on.
you could try synchronizing the loop and see if issue gets resolved.
Also, you could try using for-each loop instead of the loop with counter.
for (ModeChoiceTrip trip : tripList) {
.....
}
The issues was the duplicate values in the list. Thus, when I update a value in list the copy to that value changes as well
You set the unique id to -1. So if the trip list id comes in as -1, you grab the index like tripListIds[-1]; which might be the second to the last item in the list.
Hey im very new at java so please bear with me :)
I have objects v1, v2, v3 with each one containing details of soccer players stats
int LessThan25 = 0;
for (int i = 0; i < topscorer.size(); i++)
{
while (v**[i]**.getGoals() < 25)
{
LessThan25++;
}
}
My question is that how do I make this loop work? I want to be able to use the integers in the for loop for my while v loop which links to my class definition. (bolded part)
Appreciate the help :)
I get what you mean now, you can do this to access each topScorer object:
for(int x=0; x<topScorer.size(); x++) //iterate through all elements in topScorer
if(topScorer.get(x).getGoals() < 25)
lessThan25++;
The syntax to access the elements within an ArrayList is:
list.get(x) where x is the element you wanted to access (starts from 0)
Alternatively:
Certainly, enhanced for-loop (for each loop works too):
for(Scorer s : topScorer) //Scorer is the object type in your list
if(s.getGoals() < 25)
lessThan25++;
Use 'if' condition instead of 'while', i think that is what you looking.
Use the enhanced for loop - a better idiom if using Java 5+.
Variable names should start lower case by convention.
for(Player player : players) {
if(player.getGoals < 25)
lowerThan25Goals++;
}
The answer of user3437460 is what you're looking for
for(Scorer s : topScorer)
if(s.getGoals() < 25)
lessThan25++;
An example of a Player would have been nice,
sth. like
Player cRonaldo = new Player(age, size, avgGoals, (total)goals);
Player lMessi = new Player(age, size, avgGoals, (total)goals);
Please, take a look at your while-loop
while (topScorer[i].getGoals() < 25)
{
LessThan25++;
}
This is an infinit loop. Becaue if u ever reach the point where a topScorer aka v [i] has less than '25' goals, u never get that loop to stop, and your counter LessThan25 breaking the limit of your ALU ;)
( lower case variables! )
Could be a mistake, or missing code you provided.
So just for you, being 'new' to Java, to know.
If you're using Java 8, you can easily solve this problem using Stream API in only one line of code:
long lessThan25 = topScorer.stream()
.filter(element -> element.getGoals() < 25).count();
Moreover, using Streams the JVM can improve performance in background.
Does anyone know how I can take the synonyms of a word using JWNL (Java Wordnet Library) ordered by estimated frequency? I know this can be done somehow, because Wordnet's application can do it. (I don't know if it matters, but I am using Wordnet 2.1)
Here is my code of how I get synonyms, could anyone please tell me what I should add...
(completely different ways of doing it are also welcomed!)
ArrayList<String> synonyms=new ArrayList<String>();
System.setProperty("wordnet.database.dir", filepath);
String wordForm = "make";
Synset[] synsets = database.getSynsets(wordForm,SynsetType.VERB);
if (synsets.length > 0) {
for (int i = 0; i < synsets.length; i++) {
String[] wordForms = synsets[i].getWordForms();
for (int j = 0; j < wordForms.length; j++) {
if(!synonyms.contains(wordForms[j])){
synonyms.add(wordForms[j]); }
}
}
}
Since noone answered, I suppose there must be more people wondering the same think and not knowing the answer.
Well, I figured out that there is the function Synset.getTagCount(String), which returns the value of the estimated frequency of every synset relating to the word(String). So, all I had to do was to sort the ArrayList with the synonyms according to this.
But it was proved that the synsets are by default returned sorted, so what I get using the code I wrote at the question is already ordered by estimated frequency!
I hope this will help somebody in the future :)
I'll try to explain this as best I can. I have an ArrayList of String's. I am trying to implement server-side paging for a webapp. I am restricted to the number of items per page (6 in this case) which are read from this ArrayList. The ArrayList is, lets say, the entire catalog, and each page will take a section of it to populate the page. I can get this working just fine when there are enough elements to fill the particular page, its when we hit the end of the ArrayList where there will be less than 6 items remaining for that pages segment. How can I check if the ArrayList is on its last element, or if the next one doesn't exist? I have the following code (in pseudo-ish code):
int enterArrayListAtElement = (numberOfItemsPerPage * (requestedPageNumber - 1));
for (int i = 0; i < numberOfItemsPerPage; i++) {
if (!completeCatalog.get(enterArrayListAtElement + i).isEmpty() {
completeCatalog.get(enterArrayListAtElement + i);
}
}
The if in the code is the problem. Any suggestions will be greatly appreciated.
Thanks.
It sounds like you want:
if (enterArrayListAtElement + i < completeCatalog.size())
That will stop you from trying to fetch values beyond the end of the list.
If that's the case, you may want to change the bounds of the for loop to something like:
int actualCount = Math.min(numberOfItemsPerPage,
completeCatalog.size() - enterArrayListAtElement);
for (int i = 0; i < actualCount; i++) {
// Stuff
}
(You may find this somewhat easier to format if you use shorter names, e.g. firstIndex instead of enterArrayListAtElement and pageSize instead of numberOfItemsPerPage.)
Can't you just get
completeCatalog.size()
and compare it to i? i.e to answer the question "is there an ith element" you say
if (i<completeCatalog.size())
You just need to add a second expression to look whether the end of the list was reached already:
int enterArrayListAtElement = (numberOfItemsPerPage * (requestedPageNumber - 1));
for (int i = 0; i < numberOfItemsPerPage; i++) {
if (enterArrayListAtElement + i < completeCatalog.size() && !completeCatalog.get(enterArrayListAtElement + i).isEmpty() {
completeCatalog.get(enterArrayListAtElement + i);
}
}
An ArrayList has the method of size(), which returns the number of elements within the List.
Therefore, you can use this within the if statement to check you've not went too far.
For example,
if(enterArrayListAtElement + i < completeCatalog.size()) {
...
}