aspose file tools*
The moose likes Beginning Java and the fly likes how to use if statment with not condition in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to use if statment with not condition in Java" Watch "how to use if statment with not condition in Java" New topic
Author

how to use if statment with not condition in Java

deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
hi Good evening,

i am writing an code using "if" condition. i am having an input like this rule_no are as follows
Rule1.1.1
Rule1.2.1
Rule1.3.1
Rule1.4.1
Rule1.5.1
Rule2.1.1
Rule3.1.2
Rule3.2.1
Rule3.3.1
Rule4.1.1
Rule4.2.2
Rule4.3.1
Rule5.1.1
Rule6.1.1
Rule7.1.1
Rule8.1.1
Rule12.1.2


the expected output should be like this
output in "if" part

Rule1.1.1~~Rule1.1.2
Rule1.1.1~~Rule1.1.2~~Rule1.1.3
Rule1.1.1~~Rule1.1.2~~Rule1.1.3~~Rule1.1.4
Rule1.1.1~~Rule1.1.2~~Rule1.1.3~~Rule1.1.4~~Rule1.1.7
Rule1.1.1~~Rule1.1.2~~Rule1.1.3~~Rule1.1.4~~Rule1.1.7~~Rule1.1.8
Rule1.1.1~~Rule1.1.2~~Rule1.1.3~~Rule1.1.4~~Rule1.1.7~~Rule1.1.8~~Rule1.1.9
Rule1.2.1~~Rule1.2.3
Rule1.3.1~~Rule1.3.2
Rule1.3.1~~Rule1.3.2~~Rule1.3.4
Rule1.3.1~~Rule1.3.2~~Rule1.3.4~~Rule1.3.6
Rule1.3.1~~Rule1.3.2~~Rule1.3.4~~Rule1.3.6~~Rule1.3.7
Rule1.3.1~~Rule1.3.2~~Rule1.3.4~~Rule1.3.6~~Rule1.3.7~~Rule1.3.8
Rule1.3.1~~Rule1.3.2~~Rule1.3.4~~Rule1.3.6~~Rule1.3.7~~Rule1.3.8~~Rule1.3.9
Rule1.5.1~~Rule1.5.2
Rule1.5.1~~Rule1.5.2~~Rule1.5.3
Rule2.1.1~~Rule2.1.2
Rule2.1.1~~Rule2.1.2~~Rule2.1.3
Rule2.1.1~~Rule2.1.2~~Rule2.1.3~~Rule2.1.4
Rule2.1.1~~Rule2.1.2~~Rule2.1.3~~Rule2.1.4~~Rule2.1.5
Rule2.1.1~~Rule2.1.2~~Rule2.1.3~~Rule2.1.4~~Rule2.1.5~~Rule2.1.6
Rule2.1.1~~Rule2.1.2~~Rule2.1.3~~Rule2.1.4~~Rule2.1.5~~Rule2.1.6~~Rule2.1.8
Rule2.1.1~~Rule2.1.2~~Rule2.1.3~~Rule2.1.4~~Rule2.1.5~~Rule2.1.6~~Rule2.1.8~~Rule2.1.9
Rule2.1.1~~Rule2.1.2~~Rule2.1.3~~Rule2.1.4~~Rule2.1.5~~Rule2.1.6~~Rule2.1.8~~Rule2.1.9~~Rule2.1.10
Rule2.1.1~~Rule2.1.2~~Rule2.1.3~~Rule2.1.4~~Rule2.1.5~~Rule2.1.6~~Rule2.1.8~~Rule2.1.9~~Rule2.1.10~~Rule2.1.11
Rule3.1.2~~Rule3.1.4
Rule3.1.2~~Rule3.1.4~~Rule3.1.5
Rule3.1.2~~Rule3.1.4~~Rule3.1.5~~Rule3.1.8
Rule3.2.1~~Rule3.2.2
Rule3.3.1~~Rule3.3.2
Rule3.3.1~~Rule3.3.2~~Rule3.3.3
Rule4.1.1~~Rule4.1.2
Rule4.1.1~~Rule4.1.2~~Rule4.1.3
Rule4.1.1~~Rule4.1.2~~Rule4.1.3~~Rule4.1.4
Rule4.2.2~~Rule4.2.3
Rule4.2.2~~Rule4.2.3~~Rule4.2.4
Rule4.2.2~~Rule4.2.3~~Rule4.2.4~~Rule4.2.5
Rule4.3.1~~Rule4.3.2
Rule4.3.1~~Rule4.3.2~~Rule4.3.3
Rule4.3.1~~Rule4.3.2~~Rule4.3.3~~Rule4.3.4
Rule4.3.1~~Rule4.3.2~~Rule4.3.3~~Rule4.3.4~~Rule4.3.5
Rule5.1.1~~Rule5.1.2
Rule5.1.1~~Rule5.1.2~~Rule5.1.3
Rule5.1.1~~Rule5.1.2~~Rule5.1.3~~Rule5.1.4
Rule5.1.1~~Rule5.1.2~~Rule5.1.3~~Rule5.1.4~~Rule5.1.5
Rule6.1.1~~Rule6.1.2
Rule6.1.1~~Rule6.1.2~~Rule6.1.3
Rule7.1.1~~Rule7.1.2
Rule7.1.1~~Rule7.1.2~~Rule7.1.3
Rule8.1.1~~Rule8.1.2
Rule8.1.1~~Rule8.1.2~~Rule8.1.3
Rule12.1.2~~Rule12.1.3
Rule12.1.2~~Rule12.1.3~~Rule12.1.4
Rule12.1.2~~Rule12.1.3~~Rule12.1.4~~Rule12.1.5


output : in "else" part
Rule1.1.1
Rule1.2.1
Rule1.3.1
Rule1.4.1
Rule1.5.1
Rule2.1.1
Rule3.1.2
Rule3.2.1
Rule3.3.1
Rule4.1.1
Rule4.2.2
Rule4.3.1
Rule5.1.1
Rule6.1.1
Rule7.1.1
Rule8.1.1
Rule12.1.2




the code is working fine. But in the code in if statement i am not getting the result as expected. I am getting an output like this

~~Rule1.1.1
~~Rule1.1.2
~~Rule1.1.3
~~Rule1.1.4
~~Rule1.1.7
~~Rule1.1.8
~~Rule1.1.9
~~Rule1.2.1
~~Rule1.2.2
~~Rule1.2.3
~~Rule1.3.1
~~Rule1.3.2
~~Rule1.3.4
~~Rule1.3.6
~~Rule1.3.7
~~Rule1.3.8
~~Rule1.3.9
~~Rule1.4.1
~~Rule1.5.1
~~Rule1.5.2
~~Rule1.5.3
~~Rule2.1.1
~~Rule2.1.2
~~Rule2.1.3
~~Rule2.1.4
~~Rule2.1.5
~~Rule2.1.6
~~Rule2.1.8
~~Rule2.1.9
~~Rule2.1.10
~~Rule2.1.11
~~Rule3.1.2
~~Rule3.1.4
~~Rule3.1.5
~~Rule3.1.8
~~Rule3.2.1
~~Rule3.2.2
~~Rule3.3.1
~~Rule3.3.2
~~Rule3.3.3
~~Rule4.1.2
~~Rule4.1.3
~~Rule4.1.4
~~Rule4.2.1
~~Rule4.2.2
~~Rule4.2.3
~~Rule4.2.4
~~Rule4.2.5
~~Rule4.3.1
~~Rule4.3.2
~~Rule4.3.3
~~Rule4.3.4
~~Rule4.3.5
~~Rule5.1.1
~~Rule5.1.3
~~Rule5.1.4
~~Rule5.1.5
~~Rule6.1.1
~~Rule6.1.2
~~Rule6.1.3
~~Rule7.1.1
~~Rule7.1.2
~~Rule7.1.3
~~Rule8.1.1
~~Rule8.1.2
~~Rule8.1.3
~~Rule12.1.2
~~Rule12.1.3
~~Rule12.1.4
~~Rule12.1.5

.can any body tell where iam going wrong and correct my mistakes will be of great help indeed
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

I don't know if it's the only problem, but never use == and != to compare Strings. Use str.equals(str2) and !str.equals(str2).
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
i wanted to use an rule like this



this code gives some error so i tried to change the code and get the output like that yaar Use of str.equals(str2) and !str.equals(str2) is not useful operation for this code . so any suggestions please ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
I think you need to explain what you actually want, which is not obvious or clear from your posts.
Also consider whether you can put your rules as keys in a Map, with some object or other which can implement that rule as the value. You can us the containsXXX methods of the Map interface to find whether a rule exists.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

deepika deepi wrote:this code gives some error

What error? The error message gives you (and us) a big clue as to what the problem is.

And why do you have those '$'s? You shouldn't have those in Java.
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199
i have an input file . in that some rule with numbers are given.

that rule is assigned by rule_no

i use an if statement in that

if rule_exists does not exists it should give the value of the rule number like this

Rule1.1.1~~Rule1.1.2
Rule1.1.1~~Rule1.1.2~~Rule1.1.3
Rule1.1.1~~Rule1.1.2~~Rule1.1.3~~Rule1.1.4
Rule1.1.1~~Rule1.1.2~~Rule1.1.3~~Rule1.1.4~~Rule1.1.7


else it should give the rule_no (i.e the rule alone)

in this i have assigned the rule_exists like this


if i use the code as not rule_exists



i am getting an error like this

cannot find symbol
symbol : method rule_exists()
location: class Rule
if(!rule_exists()) {

Hope i am clear. now can any one suggest me to get out of this problem?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
That first if is redundant, since you never mention undefined. It is usually bad design to use Strings instead of booleans or similar.
The compiler error is self‑explanatory. Apart from the incorrect naming conventions you are using, you have spelt rule_exists wrongly.

I suspect you should decide what you actually want; you are no clearer. Do you mean rule_exists exists, or that it is false/true? Your code looks as if you had dived in and written something without thinking what you want.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1845
    
  16

deepika deepi wrote:

if i use the code as not rule_exists


i am getting an error like this

cannot find symbol
symbol : method rule_exists()
location: class Rule
if(!rule_exists()) {

Hope i am clear. now can any one suggest me to get out of this problem?


The error message is telling you the problem: you have defined rule_exists as a String variable, but in your if statement you refer to it as a method (i.e. a boolean function): rule_exists(). Then you go back to using it as a String variable inside your if branches. There is no "rule_exists()" method, which is why you get the error.

The easiest fix is to check the contents of your rule_exists String variable in the "if" test.


No more Blub for me, thank you, Vicar.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8192
    
  23

deepika deepi wrote:i have an input file . in that some rule with numbers are given.
that rule is assigned by rule_no
i use an if statement in that
if rule_exists does not exists it should give the value of the rule number like this
Rule1.1.1~~Rule1.1.2
Rule1.1.1~~Rule1.1.2~~Rule1.1.3
Rule1.1.1~~Rule1.1.2~~Rule1.1.3~~Rule1.1.4
Rule1.1.1~~Rule1.1.2~~Rule1.1.3~~Rule1.1.4~~Rule1.1.7

No, that explains what you've implemented, NOT 'what you're trying to do'; and it plainly doesn't work.

Perhaps a better question is: Why do you need to do this?
Explain that, and the chances are you'll get better advice.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95

Of course because rule_exists before the if statement is "".
deepika deepi
Ranch Hand

Joined: Jan 23, 2012
Posts: 199

Winston Gutkowski wrote :
Perhaps a better question is: Why do you need to do this?
Explain that, and the chances are you'll get better advice.



This is a part of the coding i need to make such output. So i am using that if(!(rule_exists()).
the ultimate aim is to get such output if that rule_exists not exists in the program.
else print the rule
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Back to basics, please.
  • Why do you need this functionality in the first place?
  • What is it supposed to do?
  • How are you establishing whether a rule exists?

  • You need to explain that all simply, because we do not understand what you want. We cannot help with something unless we know what it is. Your posts are by no means clear, and simply repeating, “because I need it,” does not help.
    deepika deepi
    Ranch Hand

    Joined: Jan 23, 2012
    Posts: 199
    this is used to check whether the rule exists and need to group the rules. For example if i have numbers such as

    1.0
    1.1
    1.2
    1.3
    2.1
    2.2
    2.3
    3.1
    3.3
    4.1
    4.2
    4.4

    it should be grouped with this symbol "-" as follows

    1.0
    1.0-1.1
    1.0-1.1-1.2
    1.0-1.1-1.2-1.3
    2.1
    2.1-2.2
    2.1-2.2-2.3
    3.1
    3.1-3.3
    4.1
    4.1-4.2
    4.1-4.2-4.4

    i want to perform an grouping with the if function. that what the rule_exists need to do.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39784
        
      28
    You are simply repeating yourself. Go back to the beginning and explain what you want, in such form that we can understand.
    What does it mean that a rule exists? What do you want to do with that rule if it exists? What are you going to do with a non‑existent rule ()? Does the numbering of the rules determine how you group them?
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 8192
        
      23

    deepika deepi wrote:it should be grouped with this symbol "-" as follows:...

    Yes, we know all that. The question is WHY?
    From the look of it, it would appear to be a grouping scheme for an outline numbering system, but unless you TellTheDetails (←click), there's very little we can offer in the way of advice.

    And by 'telling us', I don't simply mean repeating what you want to see from a specific set of input.
    What if the input is different? What are the corner cases? What is NOT allowed? Can you have 'a.b' instead of '1.0'?

    Easiest by far, as Campbell just said, would be for you to back up and tell us what you're trying to do.

    Winston
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to use if statment with not condition in Java