0

I don't know why, it seems to me that Java's IF is different than a BASH's IF or C++'s IF. I might be wrong, at least I hope. This code below is one of the many version of IF I tried, I can not find the error at this moment.

    String identification = "qwerty1";
    String name = identification.substring(0,identification.length()-1); // querty
    String version = identification.substring(identification.length()-1,identification.length()); // 1

    Database db = new Database();
    if (version != "1"){
        System.out.println("\nCheck version");
        System.out.println(version+"\n");
    db.SelectVersion(name, version);
    if (db.isSelectVersion()) {
        System.out.println("\nUpdating version");
        //Database.UpdateVersion(name, version);
    }
}
    else {
        db.Select(identification);
        if (db.isSelect()) {
            System.out.println("\nThere is something");
            //Database.Update(identification, quantity1, quantity2);
        } else {
            System.out.println("\nThere is nothing");
            //Database.Insert(identification, quantity1, quantity2);
        }
    }

   db.Match(name, version);
    if (db.isMatch()) {
        System.out.println("\nQuantities match\n");
    } else {
        System.out.println("\nQuantities do not match\n");
    }
aPugLife
  • 989
  • 2
  • 14
  • 25
  • 1
    Awww come on please, just don't start to possible duplicate everything. And please, why a post about strings is duplicate of one with IF? – aPugLife Aug 18 '16 at 09:30
  • 1
    Because of this `version != "1"` this is not how you compare strings in Java, you should use equals method like this `!version.equals("1")`, so your IF is not working, please see duplication question for more details. – Pradeep Simha Aug 18 '16 at 09:32
  • `IF is different than a BASH's IF or C++'s IF`. Yeah you noticed it correctly. A reason might be that these are different programming languages who work differently. And the duplicate question is pretty clear and not a "we mark every question as a dup" as it´s a complete duplicate of your problem. Before working with java, one might learn the bascis first, which in this case is how the `=` operator does work on objects. It simply compares references here and the one for `"1"` will definitely be different to the one of `version `. – SomeJavaGuy Aug 18 '16 at 09:36
  • correct, though I am not a developer and can't get to like java programming. I need to complete this program for a server I'm managing. Thing is I used the equal() in another method, didn't imagine that equal() is the one and only. About "everything as a dup", another question of mine was marked duplicate without even reading what I was looking for. In fact I solved by myself and not thanks to the "original", totally different approach. Anyway, thanks guys. Correcting this bug, now remain only 99+ more. ;) – aPugLife Aug 18 '16 at 09:58
  • @Nihvel here´s the description about SO. [Stack Overflow is a question and answer site for professional and enthusiast programmers.](http://stackoverflow.com/tour). If you aren´t a developer but are yet looking for help on SO and do complain about the basic question beeing marked as duplicates than you might want to get more into programming. Otherwise most following question like this which are clear duplicates will also be marked as those. – SomeJavaGuy Aug 18 '16 at 10:50
  • I do develop in other language, I'm network administrator and bash is all I need. but sometimes it is not enough. I studied java, a lot, 10 years ago. I just don't like it. Marking a question with duplicate, without giving answers (happened before), is the same as "get this page and read it". How am I supposed to know that my IF breaks because of the equal(), when I was sure it was due to the parenthesis? Instead, mark it as duplicate and give some explanation. Like good guy Simze did after that I asked. – aPugLife Aug 18 '16 at 12:55

0 Answers0