In Python/C++, I normally use _("string") for i18n string text.
for Java, I use bundle.getString("string"). Obviously, it is uglier than Python/C++.
How to write such code shorter?
Create your own method:
public String _(String key){
return bundle.getString(key);
}
Or something similar. Underscore is a valid method name in Java. Of course, you can use any other single character, if you prefer, say l like localize.
So, now you can call it the same way as in Python.
Thats the way Java is, you can call it ugly, though.
I would pretty much stick with the Java convention and use the bundle.getString(...) version. :)
Related
Im trying to make a Regex in Java that can parse the following Strings
g1(g2,g2),g1(g2)
g1(g2(g3,g3),g2),g1(g2)
g1(g2)
I have been trying for hours but I cant make one that can split each example in the following classes.
public class G1{
List<G2> list;
}
public class G2{
String g2;
Set<String> g3;
}
Where I need one instance of g1 for each of the groups.
Thanks
EDIT
Fixed the classes.
It looks like you have a grammar to deal with, regular expressions is not really the appropriate tool to do it, instead you're better off building a simple finite state machine to do the parsing.
Another option which I don't recommend for something this simple is to use ANTLR which is a tool that is designed to do this sort of parsing. I don't recommend it because it would be overkill for the job.
Regex is not a tool that handles recursion well.
For instance, it can't easily discern that the outer parentheses is the one you want in this line
g1(g2(g3,g3),g2),g1(g2)
If you try to use a greedy regex, it would go collect the whole line g1(g2(g3,g3),g2),g1(g2). If you try to go for non-greedy, it would collect g1(g2(g3,g3). Regexes that might gather it are pretty shaky and can break pretty easy.
If the outer group is always called g1 and g1 is never nested within another group, you might be able to use something like this
g1\(.*?\)(?=,g1|$)
Really though, regex is not a tool for this task.
I'm fairly new to Java and I'm trying to use the + character as part of an enum type, but the compiler is complaining about the syntax because I believe it sees it as an operator.
I'd like to do the following:
enum mediaType{
FACEBOOK,GOOGLE+,TWITTER;
}
Any ideas?
Thanks!
Yes, the compiler will treat + as an operator. You can however choose a different name there:
enum mediaType{
FACEBOOK,
GOOGLE_PLUS,
TWITTER;
}
And if you want to use the value GOOGLE+ only, then have a field of type String, storing the value, and also a parameterized constructor.
P.S: As per proper naming convention, the enum name should be MediaType.
You can't use arithmetic symbols in identifiers. You need to find something you can use like GOOGLE_PLUS
Maybe reading the official Java tutorial's section on Naming Conventions will help you: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
You could at least define a private String myName = "Google+"; inside the enum, and define a method that a UI can use to get the value you want rather than just displaying the enum's variable itself.
public String myName() {
return myName;
}
Many people believe that you are limited to ASCII in Java just like C and C++.
Actually you have the full Unicode character set.
This is perfectly good Java:
enum Plus {
Google,
GooglePlus,
Googleᚋ,
Googleᐩ;
};
Not quite a + (which you cannot have in an enum because it will be confused with the + operator) but it will still carry the impression of a plus.
It seems you can use the Ogham character called muin which looks a bit like a plus character. Alternatively it seems the Canadian syllabics final plus is also acceptable alhough a number of the other possibilities seem not to be acceptable.
In my java code I did something like that:
int sleep = 0;
sleep(sleep);
sleep++;
It was pointed out by my bos that it is not good. That it won't work in php correctly.
Is there any issue / danger using above code in java?
There's no danger, since methods will never be referred to without the parentheses for arguments, so the names will never clash.
However it's generally clearer and more intuitive if method names are verbs and variable names are nouns, for example:
int sleepMillis = 0;
sleep(sleepMillis);
sleepMillis++;
There isn't a syntax reason why you can't do it, but as a general development principle, it's generally clearer if you don't use the same names, for clarity.
I've never tried that before but I assume it would work correctly since Java should be able to distinguish the different between a variable and a method. It should work but code like that is confusing. Don't make give variables names that match a method you'll be using.
It's confusing, to say the least. Not really a problem, since the compiler will point to you if you try to use the method where a variable is expected (or vice versa), thanks to the fact that Java is statically typed.
The same code would be much more problematic in a dynamically typed language with first-order functions, where in fact a variable can point to a function or a value with no way to distinguish between them until runtime.
I would like to save some work on my app, is it possible to get the string, for example "level1" and then use the corresponding function, which would be level1();? my main point is not to make a huge switch-case statement, but only make a few level functions in a storage class, and whenever you level up, the string would change to "level" + number where number is the int, so lets say that right now you are in level 10, the function that would run is level10();
I hope i explained it clearly.. sorry if not.. hope you get the idea!
Thanks!
I believe you want to call a method at runtime using its name as a string.
You can do it via reflection.
Class.getMethod(String methodName, Class... parameterTypes)
Don't think of this in terms of method names, unless you want to muck around with reflection (you don't want to, and it's not necessary).
If you really do need to convert strings to method calls – and that's a big "if" – create a Map<String, Foo> where Foo implements some "callable"-like interface. Then a string-to-method lookup is simply:
Map<String, Foo> commands = /* ... */;
Foo foo = commands.get("level42");
foo.bar();
It really sounds like you should just have a
void setLevel(int level)
call. That can feel free to ignore (say) levels 11-14 or whatever... but it would be very ugly to have separate methods and invoke them by name. You can do so with reflection, but you should think about other options first.
Please see the top answer to this post:
Java dynamic function calling
I would also recommend following their advice regarding structure, to create a more object-oriented solution instead of using reflection.
I just want to know that is it possible in java to use
class{
private final static String AND="&&";
public static void main(String...args)
{
String str="subodh";
if(str!=null AND str.equals("subodh") )
{
System.out.println(str);
}
}
}
The above is not allowed but is there any such kind of way through we can use it by putting some extra efforts if it's please let me know or please put your openions.
Thanks
No, there's no way of changing the operators in Java - thankfully, IMO.
I suggest you learn to use Java as Java rather than trying to make it look like some other language. (Or use that other language which looks more like you want it to.)
If you're trying to do something other than bend the language to your personal preferences, please edit the question to explain what your purpose is.
No, There is no any way to use this. For that I have to go through different language.
No way to make this possible. && is operator and you cannot assign an operator to a String variable. Here AND acts like a String variable.I hope you will get it.I also do not think one will need anything like this in any situation. If possible make your motive clear why you want to this?
If you really wanted to you can implement a class to "prettify" logical operations using method chaining . And then use it something like:
PrettifyLogic.condition(str!=null).and(str.equals("subodh")).evaluate();
Though I don't personally find that more appealing to read/write. Also, good luck with operator precedence if you try something like this!
Nope, Java doesn't support doing this type of thing and I'm thankful for that! If it did we could get all sorts of things meaning && which would make existing code hard to read.
If you want to code in Java, you'll just have to stick with the way Java does things.