if(word.equals(" ")){
}
I have this IF statment, what i would like it to also do is the following..
if(word.equals(" ") OR word.equals(".") ){
}
I know the above code will not work, but im looking how to implement something like that? Anyone know how to do an if or, or do i have to use else if?
Android uses Java, and the boolean OR operator in Java (and in pretty much every single language with a C-inspired syntax) is ||.
if (word.equals(" ") || word.equals("."))
Here is a document describing the Java operators. You may also want to read it from the start since it covers the basics of Java.
There are thousands of tutorials on Google explaining the basics of the Java language that you could also use.
Probably, the reason the question was asked is that you must do this test BEFORE trying to do a .parseDouble on 'word' or else the app will crash
Related
When looking at some code on one or another git, sometimes I can see that devs use bitwise inclusive OR compound assignment operator (|=) where simple assignment would be enough. Unfortunately, I don't have any code with this solution at hand, so I'll try to describe it as best I can.
Let's say, we have the following code in java:
boolean found = false;
for (String s : stringSet) {
if (s == null || s.equals("")) {
found |= true; // <== this line
}
}
if (!found) {
System.out.println("No interesting string found");
}
I ask only about the pointed line. Why people do it this way? I understand that we can have a really great amount of data, billions or trillions to iterate over. But does the pointed line changes the efficiency so dramatically? Would it be noticeably slower for a lot of data, if I change it to simple assignment: found = true;?
I don't exclude the possibility that not a speed is the main argument, but it seemed most meaningful to me.
And yes, I know this code can be converted to method or streams, but it's only a simplification of a code where it would be far more complicated etc. We can assume that before the pointed line (or even before if), there are tons of code that do something meaningful. Please, don't suggest something like "use streams instead", because I know how to java advanced already. I'd like to understand the phenomenon of this somehow enigmatic solution using bitwise inclusive OR.
I trying to learn how to write formatted strings in java for console apps. but, no one really explained this subject very well so it gets a bit messy.
I need to know where to start, i can't learn it from java doc (The doc explains this topic really terribly).
So, i surfed a lot in Wikipedia values like:
Template Languages,
scanf blala,
Data models.
I just can't understand even what this subject is?, why do we need it? and just why...
and what "fomat" / "formatted" means? design?
By the way, printf and scanf are string formats?
I have a vague memory of my professor from way back ~10 years ago saying that it would be a good practice to write conditions which check a boolean value, with double brackets, unless if you specifically check if it is true or is false.
So instead of
if(isColdOutside) {}
You should do
if((isColdOutside)) {}
And the reason is that if you do single-bracket, the compiler is going to issue a warning to tell you that maybe you forgot to check the value, since there is no == in the condition. So by using double-brackets, you tell the compiler "This is what i meant to do, just check if this is truthy".
He mostly taught Java stuff, that's why I thought it might be a Java thing.
But now, I wanted to look for it, and I can't find anything about this. I tried searching for variations of stuff like "double bracket condition" or "boolean condition warning", and I got no significant results.
Does this syntax/convention have a name? Is it even real, or did I just dream it?
After 20 years of working with Java I have never heard of this. I don't think it is a thing. And if it is a thing, then no one I know would think it was something other than a typo.
Other languages like the Bash shell in linux have multi-bracket differences, but Java, not so much.
I have the following piece of code called Code1.
http://pastebin.com/tc0Vd8xh
When I run this, the sketch does not work.
However when I replace "i=+50" for "i = i + 50" the code works.
My question is why the "i=+50" bit does not work?
As far as I know "i=+50" is proper Java and Processing is based on Java.
I tried to Google about "i=+50" but Google does not process non-alphanumeric characters.
So I came here and I searched in previous questions before asking here. Anyone, any idea why "i=+50" does not work?
The statement i=+50 is the assignment of positive 50 to i. That is why it compiles, but doesn't add 50 to i on each loop. As #RoelHarbers and #ByoTic mentioned, you actually want i += 50
You're using =+, which is not a java operator (or an operator in any other language I know of)
The proper syntax is:
i+=50
Because it's i+=50 and not i=+50.
i =+ 50 is not going to do what you want, it is going to initialize i with 50. Instead, use i+=50, this going to add the 50 to whatever value that i holds.
Are there any Java API(s) which will provide plural form of English words (e.g. cacti for cactus)?
Check Evo Inflector which implements English pluralization algorithm based on Damian Conway paper "An Algorithmic Approach to English Pluralization".
The library is tested against data from Wiktionary and reports 100% success rate for 1000 most used English words and 70% success rate for all the words listed in Wiktionary.
If you want even more accuracy you can take Wiktionary dump and parse it to create the database of singular to plural mappings. Take into account that due to the open nature of Wiktionary some data there might by incorrect.
Example Usage:
English.plural("Facility", 1)); // == "Facility"
English.plural("Facility", 2)); // == "Facilities"
jibx-tools provides a convenient pluralizer/depluralizer.
Groovy test:
NameConverter nameTools = new DefaultNameConverter();
assert nameTools.depluralize("apples") == "apple"
nameTools.pluralize("apple") == "apples"
I know there is simple pluralize() function in Ruby on Rails, maybe you could get that through JRuby. The problem really isn't easy, I saw pages of rules on how to pluralize and it wasn't even complete. Some rules are not algorithmic - they depend on stem origin etc. which isn't easily obtained. So you have to decide how perfect you want to be.
considering java, have a look at modeshapes Inflector-Class as member of the package org.modeshape.common.text. Or google for "inflector" and "randall hauch".
Its hard to find this kind of API. rather you need to find out some websservice which can serve your purpose. Check this. I am not sure if this can help you..
(I tried to put word cacti and got cactus somewhere in the response).
If you can harness javascript, I created a lightweight (7.19 KB) javascript for this. Or you could port my script over to Java. Very easy to use:
pluralizer.run('goose') --> 'geese'
pluralizer.run('deer') --> 'deer'
pluralizer.run('can') --> 'cans'
https://github.com/rhroyston/pluralizer-js
BTW: It looks like cacti to cactus is a super special conversion (most ppl are going to say '1 cactus' anyway). Easy to add that if you want to. The source code is easy to read / update.
Wolfram|Alpha return a list of inflection forms for a given word.
See this as an example:
http://www.wolframalpha.com/input/?i=word+cactus+inflected+forms
And here is their API:
http://products.wolframalpha.com/api/