File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes If then else Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If then else " Watch "If then else " New topic

If then else

Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
Hi , I am new to java world and this might be a basic question

i want to use the below if statement in
negation form that is : if the columnname is not equal to score or summary then print not equal ( i dont want else)

String columns_name = "title";
// trying to negate but this is a syntax error
if !(columns_name.equals("score")|| columns_name.equals("summary"))

System.out.println(" NotEqual");


this works

String columns_name = "title";
if (columns_name.equals("score")|| columns_name.equals("summary"))

System.out.println(" Equal");

{System.out.println("Not Equal");
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The whole condition has to go inside the parentheses. Also be careful that the "!" means what you want, and that it applies to everything you want it to.

For example: here the "!" applies only to the first "equals()":

if (!columns_name.equals("score")|| columns_name.equals("summary"))

Here, it applies to both because of the extra parentheses:

if (!(columns_name.equals("score") || columns_name.equals("summary")))

[Jess in Action][AskingGoodQuestions]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
if !(columns_name.equals("score")|| columns_name.equals("summary"))

Just put parentheses around it like so:

if ( ! (columns_name.equals("score") || columns_name.equals("summary")))
Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
thanks a lot
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Sometime soon you'll bump into boolean algebra. Here's one transform that may interest you right now:

not ( A or B ) == not A and not B

See if this is easier to read ... or not ...

if ( !columns_name.equals("score") && !columns_name.equals("summary") )

Either way is kind of twisty. I'd be tempted to reverse the if and else blocks to write

if ( columns_name.equals("score") || columns_name.equals("summary") )

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
subject: If then else
It's not a secret anymore!