• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to use if statment with not condition in Java

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 95
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course because rule_exists before the if statement is "".
 
deepika deepi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 199
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79179
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic