aspose file tools*
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
Author

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");
}


else
{System.out.println("Equal");
}
-----------------------------------------------------------------

this works


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

{
System.out.println(" Equal");
}


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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Marshal

Joined: Mar 22, 2005
Posts: 42622
    
  65
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")))


Ping & DNS - my free Android networking tools app
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If then else