I have a string type column which includes name of Region.
People can create regions at will.
For example, you can create an area called "Paris". But if the table already has "Paris", I entrusted spring to automatically save as "Paris2".
But the problem is that spring always save it as "Paris2". I think it just check only does Paris exists. Can you recommend what to do if you want to be saved as Paris3, Paris4 in elegant way?
String toBeRegionCode = stringReplace(((String)paramBean.get("regionName")).trim());
if (expediaRegionUnionRepository.findByRegionCodeCount(toBeRegionCode) == 0) {
insert.setRegionCode(toBeRegionCode);
} else {
insert.setRegionCode(toBeRegionCode + "" + (expediaRegionUnionRepository.findByRegionCodeCount(toBeRegionCode) + 1));
}
This is the code that always makes Paris2. How to improve Paris3 Paris4 return?
Related
I would like to read data from my config.yml. It's saving data like (playerName: value). I want to check, if player's name value is equals to 0, then ban him. But still don't know how to read those values. I was searching around spigot forum but nothing worked.
#EventHandler
public void OnDeath(PlayerDeathEvent event) {
Player player = event.getEntity().getPlayer();
String playerName = player.getName();
int lives;
if (!livesMap.containsKey(player)) {
// Set the default amount of lives to 2. (3 minus 1, since the player already died once)
lives = 2;
plugin.getConfig().set(playerName, lives);
plugin.saveConfig();
} else {
// Subtract one from the player's lives
lives = livesMap.get(player) - 1;
// Saving playerName and lives
plugin.getConfig().set(playerName, lives);
plugin.saveConfig();
}
livesMap.put(player, lives);
My data is saving like this, but i want to know what method should i use to read values.
Lucky for you, bukkit already has classes you can use for this!
import org.bukkit.configuration.file.FileConfiguration;
FileConfiguration config = getConfig();
This call will inherently load your config.yml file.
You can then access values by doing something like this:
config.getString("yml.object.here");
Let me know if you have any other questions!
TIP: If you store your player names in a list you can get a list of them!
plugin.getConfig().getInt(playerName).
Bit of context...
In my project I have one embedded for loop that outputs data whereby for each category show the item and within each item show its property so in reality the output I generated is 3 columns of data in the console (headings: Category/Item/Property) The for loop to show this data looks like this (Variables are set earlier on in the method):
for... (picks up each category)
for...(picks up items in category)
for (String propertyName : item.getPropertyNames()) {
out.println(category.getName() + "\t"
+ itemDesc.getName() + "\tProperty:"
+ propertyName);
}
}
}
The purpose of the project is to provide a more dynamic documentation of the properties of set components in the system. (The /t making it possible to separate them in to individual columns on a console and even in a file in say an excel spreadsheet should I choose to set the file on the printstream (Also at the start of this method.))
The Problem
Now for the problem, after the for loops specified above I have generated another for loop separate from the data but shows the list of all the functions and operators involved in the components:
//Outside the previous for loops
for (Function function : Functions.allFunctions) {
out.println(function.getSignature());
}
What I want is to set this list as the 4th column but the positioning of the for loop and the way it is set leaves it fixed on the first column with the categories. I cant add it after property names as the functions are more generic to everything in the lists and there maybe repetitions of the functions which I am trying to avoid. Is there a way to set it as the forth column? Having trouble finding the sufficient research that specifies what I am looking for here. Hope this makes sense.
One solution, if the total amount of output is small enough to fit in memory, is to simply save all the data into an ArrayList of String, and output it all at the very end.
List<String> myList = new ArrayList<String>();
for... (picks up each category)
for...(picks up items in category)
for (String propertyName : item.getPropertyNames()) {
myList.add(category.getName() + "\t"
+ itemDesc.getName() + "\tProperty:"
+ propertyName);
}
}
}
int i = 0;
// Here we assume that the total lines output by the previous set of loops is
// equal to the total output by this loop.
for (Function function : Functions.allFunctions) {
out.println(myList.get(i) + "\t" + function.getSignature());
i++;
}
In PL/I there's a nice statement called "PUT DATA" which is used like so:
NAME := 'ROBBY'
AGE = 30
PUT DATA ( NAME, AGE )
The output from that statement would be:
NAME = ROBBY, AGE = 30
To do so in Java would require a lot of care with quotes, spaces, and plus signs:
System.out.println ( "Name = " + name + ", age = " + age ) ;
I'm far too new at Java to have a feel for how impossible making a "putData" method would be. I should think about it first, I guess, but I mainly wondered if maybe it already exists, even in a one-argument-only form.
putData (name);
would very cleanly produce
name = ROBBY
vs.
System.out.println( "name = " + name )
which is nerve-wracking even with just one "equation" to display.
What you want is not possible in Java, since a method can not - and should not(!) - have any knowledge about the expressions used to calculate the values used for arguments. What if someone would write:
putData("ROBBY")
There is no variable name - what should putData print?
If Java had a map literal, you could have something like:
putData({name:"ROBBY"})
and putData would know that name is the key and "ROBBY" is the value and know how to print it. Still not what you wanted - but close.
Anyways, I also dislike string concatenation, and prefer to use printf:
System.out.printf("name = %s\n",name);
You still need to write name twice(can't go around that), but it's less ugly.
I'm answering my own first question of many months ago.
The PL/I "put data" statement can be faked by the Netbeans code template soutv:
System.out.println("${EXP instanceof="<any>" default="exp"} = " + ${EXP});
Typing soutv (and then hitting tab) gives the following template:
Since the first occurrence of args is "boxed" in "refactor format", it's semi-obvious that, whatever it is changed to, the other args changes similarly. So if I type now in the "args box" (and then hit enter), the statement becomes:
System.out.println("now = " + now);
It's good enough. Only thing I don't get is the PL/I nicety of being able to say
put data(a,b,c)
and getting
a = ..., b = ..., c = ....
But I'm sort of happy with the development. Hope it helps others.
System.out.printf("${EXP instanceof="<any>" default="exp"} = %s\n" , ${EXP});
The Netbeans template above does what Idan's suggestion (in Answer below)
System.out.printf("name = %s\n",name);
does and does NOT require typing name twice, besides having the advantage of enabling the following output:
x+y+z = 12
merely by typing x+y+z in the "args box", which results in this code:
System.out.printf("\nx+y+z = %s\n", x+y+z);
where the following initializations are given: x = 3 , y = 4, and z = 5.
Im having a function:
private void fixTurn(int turn)
And then I have:
memory1 = memory1 + count;
Now, I would like to make, if turn is 2 it should:
memory2 = memory2 + count;
I tried this:
memory + turn = memory+turn + count;
But will it will not work, should i just go with an if statement?
No, you should use a collection of some form instead of having several separate variables. For example, you could use an array:
memory[turn] += count;
Numerical indexes in variable names are generally something to be avoided.
Wanting to access such variables via the index is usually the sign of a novice programmer who hasn't gotten the point of arrays - because an array is exactly that, a bunch of variables that can be accessed via an index:
memory[turn] = memory[turn] + count;
or, shorter (using a compound assignment operator):
memory[turn] += count;
u have to write it as
memory += turn * count
you should rephrase your quesiton but I think you want to do something like this
private void fixTurn(int turn){
if(turn == 1){//note can be replaced by a switch
memory1 +=count;
}else if(turn ==2){
memory2 +=count;
}
Edit: the solution proposed by John Skeet is better in terms of readability and adaptability and I would recommend it more
My polished crystal ball tells me, that you that you have some sort of game, that is organized in "turns" and you want to change something for a given turn ("fixTurn").
You may want to store the turns in a list. That's preferrable over an array, because a list can grow (or shrink) and allows adding more and more "turns".
Assuming, you have some class that models a turn and it's named Turn, declare the list like:
List<Turn> turns = new ArrayList<Turn>();
Then you can add turns to it:
turns.add(new Turn());
And now, if you have to change some parameter for a turn, do it like this:
private void fixTurn(int number) {
Turn memory = turns.get(number);
memory.setCount(memory.getCount()+count);
}
I am not very clear about your question but I think this is what you are looking for:
memory += turn * count
This syntax is not allowed in java
memory + turn = memory+turn + count;
First of all I just begun learning Java and i can say it more challenging then C or python. I'm not very keen on programming to so I have hard time understanding how some codes works. This one in particular
public class Pseudo
{
final int a = 2;
final int c = 3;
int address;
String list[][] = new String [100][6];
public void AddRecord(String ID, String Name, String Course, String Address, String Email, String Contact)
{
address = (a * Integer.parseInt(ID) + c) % list.length;
if((Integer.parseInt(ID)<100000||Integer.parseInt(ID)>999999)||ID.length()==0 || Name.length()==0 || Course.length()==0 || Address.length()==0)
{
showMessageDialog(null,"The ID number should be in six digit and the particular field should not be empty","",ERROR_MESSAGE);
}
else{
if(list[address][0]!=null){
showMessageDialog(null,"Collison is occur, the same address is get. Recalculating...............","",WARNING_MESSAGE);
while(list[address][0]!=null)
{
address = (a * address + c) % list.length;
}
}
list[address][0] = ID;
list[address][1] = Name;
list[address][2] = Course;
list[address][3] = Address;
list[address][4] = Email;
list[address][5] = Contact;
showMessageDialog(null,"Student Information " + ID + " will be saved in address: " + address,"",INFORMATION_MESSAGE);
}
}
The confusion come when
address = (a * Integer.parseInt(ID) + c) % list.length;
if((Integer.parseInt(ID)<100000||Integer.parseInt(ID)>999999)||ID.length()==0 || Name.length()==0 || Course.length()==0 || Address.length()==0)
What does it mean. From what I understand from this code is that inside an IF statement you can have more then 1 condition. I'm no very sure since this is my first time seeing such a code.
The second is this
if(list[address][0]!=null){
showMessageDialog(null,"Collison is occur, the same address is get. Recalculating...............","",WARNING_MESSAGE);
while(list[address][0]!=null)
{
address = (a * address + c) % list.length;
}
}
list[address][0] = ID;
list[address][1] = Name;
list[address][2] = Course;
list[address][3] = Address;
list[address][4] = Email;
list[address][5] = Contact;
showMessageDialog(null,"Student Information " + ID + " will be saved in address: " + address,"",INFORMATION_MESSAGE);
If collision occurs the address of which it is stored should be altered using a psedorandom number generator again but what I can't grasped is
list[address][0]!=null.I am just baffle with this line. I know its job is change the address if collision happens but i don't know the exact mechanics of how this part is executed.
From what I understand from this code is that inside an IF statement you can have more then 1 condition.
Well, yes and no. You can construct complex conditions based on many smaller conditions, but ultimately the whole thing has to resolve to a single boolean true/false result.
Consider the condition in this case:
(Integer.parseInt(ID)<100000||Integer.parseInt(ID)>999999)||ID.length()==0 || Name.length()==0 || Course.length()==0 || Address.length()==0
Let's break that down into its components:
(
Integer.parseInt(ID)<100000 ||
Integer.parseInt(ID)>999999
) ||
ID.length()==0 ||
Name.length()==0 ||
Course.length()==0 ||
Address.length()==0
It's really just chaining together a bunch of comparisons into one big true/false statement. You can essentially read something like this as:
If (something) or (something else) or (another thing) then...
And each something can itself contain small somethings, etc. You can build as complex a logical condition as you want, grouping sub-conditions with parentheses, as long as the whole thing resolves to a single true/false result.
what I can't grasped is list[address][0]!=null
That is just checking if a particular value is null. That value is part of a nested (jagged) array. So you have a variable called list. That variable is an array. Each element in that array is, itself, also an array. So you end up with a kind of 2-dimensional array (but a jagged one, where any given sub-array doesn't have to be the same length as any other).
That specific piece of code looks into the list array, at the address index, and looks at the 0 index of that sub-array, and checks if that value is null.
First of all, understanding any code is much easier if it's properly formatted. All good IDEs have such a function, e.g. for Eclipse the shortcut is Ctrl+Shift+F, for IntelliJ IDEA Ctrl+Alt+L.
The most important part, which might resolve your first confusion: || is the logical OR in Java, meaning the ID must be a number between 100000 and 999999 and the attributes must not be empty. Or literally, if the ID is smaller than 100000 or larger than 999999 or any of the values are empty, there will be an error message and nothing will be done.
For the second part: null means that a variable is not set, so to prevent overwriting an entry you can check if it's already set, i.e. not equal to null. So the code changes the address variable until an address is found for which no data is set yet and then uses it to store the given data.
There are several potential problems in this code, among which:
several calls to the relatively slow Integer.parseInt(String) where it could be called once and stored into a variable
potential NumberFormatException if ID isn't a number (or is empty, or has some excess white spaces)
potential infinite loop if the array is full
But as it looks like some CS homework it shouldn't matter.
Thank You so much Mr David. I understand the first part where if u have a condition u can stack it on each other and from what i can understand it only works with the ||(OR) statement since using this will guarantee either a true or false ending.
while(list[address][0]!=null)
But I'm still a little confuse for part 2 of my problem. Since that line is to check the array is null meaning no value right.This is my understanding of the situation.That particular part of the code is suppose to resolve any collision if the user enters the same ID number right so shouldn't it be checking the value that's causing the collision. But the line seems to be doing is as long as a null value is detected the corresponding procedure would be implemented.