Different if statements Java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have some questions in which I would like to ask.
First off:
int id = 1;
So, we can have:
if (id == 1)
{
//TODO:
}
But we can also have:
if (id == 1)
//TODO
I was wondering what's the difference between these two?

In the second if statement only first statement after if is inside if block. In the first if statement you can put as many statements as you like between { and } and all of them are inside if block.

Related

Iterator NPE in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm having an issue with multiple NullPointerExceptions, I'm getting one for my if statements within iterators and I can't figure out why
one of the problem blocks:
public int removeAllBooks(String author, String title){
Iterator<Book> itr=library.iterator();
int i=0;
while(itr.hasNext()){
Book book=itr.next();
if(b.getAuthor().equals(author)&&(b.getTitle().equals(title))){
itr.remove();
i++;
}
if(i>0){
return i;
}
}
return 0;
}
The NPE error points to my if-statement line for some reason.
Thanks.
What is b? Where is it declared/initialized? Your itr.next() is assigned to the book variable, not b.
You code should probably be :
Book book=itr.next();
if(book.getAuthor().equals(author)&&(book.getTitle().equals(title))){

Regular expression for negative values in flower brackets [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I need a regex for patterns like
1) {-1,},
2) {-1,-2},
3) {,-2}
Please help me out.
try this,
String regex="\\{-?(\\d+)?,-?(\\d+)?},?";
String str="{-2,}";
System.out.println(str.matches(regex));
Try this,
^\{(\-\d+)?,(\-\d+)?\}$
Hope it works for you.

Get column name from where condition in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying below:
I am trying to process where condition of sql in java. How can I get column name in where condition as a string ?
Ex: String s = "Name_id>=11"
now I want to get "Name_id", as it is a column name and i want to do processing on it.
I know it can be done using regex but I am new to regex and don't know how to do it. can someone guide me??
help will be appreciated, thanks
You can try this
s = s.replaceAll("^(\\w+).+", "$1");

Why can this not be done? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to shorten my code using a loop. I have for example 5 zombies in my game. So I thought I could do this
Image zombie;
for(int i = 0; i < 5; i++){
if (zombie.getZombieRect().intersects(zombie + i + .getZombieRect())) {
}}
Why can this not be done? adding i to the end of zombie. zombie being an image. The oother variables are zombie1, zombie2 etc.
Thanks for all help.
This is what arrays are for:
Zombie zombies[] = {zombie, zombie1, zombie2, zombie3, zombie4};
for (int i = 0; i < zombies.length; i++) {
if (zombie.getZombieRect().intersects(zombies[i].getZombieRect())) {
}
}
Make an Array of Objects and then u can call them by using zombie[i] etc whatever you want to do.
The thing of adding you are trying to do is suitable in case of strings only
"zombie"+i;
etc.
To answer the question,
zombie + i
Is a compile-time error because java does not allow an Image object to be used in combination with an int in the '+' operator.

how to make an if statement then send a message [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How would i make it so that if the input from the user is yes, then send the messaage You Successfully hunted a cow!
System.out.println("Would you like to hunt a cow?");
System.out.print("yes or no?");
String a = kbReader.next();
System.out.println("You successfully hunted a cow!")
if (a.equalsIgnoreCase("yes")) {
...
}
(or equals if you want to match "yes" exactly)
Is it really what you need:
if ("yes".equals(a)) {
System.out.println("You successfully killed a cow!")
}
Note the reverse check of string literal versus the input to avoid NPE.
user the equals method. for Strings, it simply checks if the strings have the same value
if(a.equals("yes")){
System.out.println("You successfully killed a cow!")
}
else{
//do whatever
}

Categories