0

I am trying to create a file system/file commander in java, and I want to make the following loop a quit system that triggers when I type dc.

public static void main(String[] args) 

 boolean x; x=true;
 String dc; dc="dc";    
    while (x=true) {

        System.out.println("_____________________");
        System.out.println("local disk C:");
        System.out.println("bin");      
        System.out.println("_____________________");
        String ltstcmdddd; ltstcmdddd = ltstcm.nextLine();
        if (ltstcmdddd==dc) {
            break;

        }
    }

So this is the code for the file commander, it's part of a game so ltstcm is a scanner, and lstcmd is a string you use to input commands for the game (Can't re-use it, I kept adding d's.), like I said before I want to leave this loop when I write dc, I made an if that checked lstcmdddd, I tried with checkingif (lstcmdddd=="dc") and that didn't work. I suspected that changing the value of the boolean x wouldn't work after discovering 'break', that failed. I then tried defining the string dc which contained "dc", and that didn't work either. I searched Stack Overflow about quitting loops, quitting loops failing, and changing values after defining a variable correctly. Nothing relevant to my problem, nothing I could salvage to solve the problem. (I AM NOT ASKING ABOUT COMPARISON!)

3rd Battalion
  • 25
  • 1
  • 7
  • 1
    How is this code compiling? `x=true` – Prashant Nov 21 '18 at 09:02
  • 1
    PS: be carefull with ``while (x=true)``, this is not only a comparison, but an affectation then a comparison. You most likely would prefer to use ``==`` which is the comparison-only operator. – spi Nov 21 '18 at 09:04
  • 1
    `=` is assignment `==` is comparison. – Pshemo Nov 21 '18 at 09:04
  • 1
    @Prashant Because assignment operator not only assign value but also *return* that value from the expression. Because of it we can have `a=b=1` which is `a=(b=1)`. First to `b` is assigned `1` but that value is also returned from `b=1` expression which results in `a=(1)`. Here `while(x=true)` assigns `true` to `x` but also returns that value which effectively is `while(true)`. – Pshemo Nov 21 '18 at 09:08
  • 2
    "(I AM NOT ASKING ABOUT COMPARISON!)" but it is comparison which seems to be main problem here. Can you clarify why you think it is not? – Pshemo Nov 21 '18 at 09:26
  • @Pshemo The answers have clarified the problem I thought it was an issue with the IF or the contents of the IF. – 3rd Battalion Nov 21 '18 at 09:36

3 Answers3

1

You cannot use the == comparison for strings, you have to use .equals, ie: lstcmdddd.equals("dc").

In Java, Strings are objects, so you cannot compare them using the double equal operator. As you are doing that however, your conditional will always return false and the break statement will never execute.

MrD
  • 4,986
  • 11
  • 48
  • 90
0

You should rather use .equals than ==. == is used to compare a single char or number, while .equals is used to compare strings.

[...]
String ltstcmdddd; ltstcmdddd = ltstcm.nextLine();
if (ltstcmdddd.equals(dc)) {

[...]
dnsiv
  • 520
  • 4
  • 20
  • This only applies to strings correct? – 3rd Battalion Nov 21 '18 at 09:08
  • To equal strings and equal objects. – dnsiv Nov 21 '18 at 09:09
  • @3rdBattalion no, this apply to ALL objects. ``==`` checks for primitive type values (int, byte, short, boolean, char, ...), or reference value (the memory address of a reference, and would return false for equivalent objects) – spi Nov 21 '18 at 09:10
  • @spi Maybe I expressed myself incorrect. `.equals` should be used if someone wants to compare two equal Strings or equal objects. But `.equal` can be used on all Strings and all Objects. – dnsiv Nov 21 '18 at 09:12
  • 1
    @3rdBattalion This applies to all objects and since Strings are objects it also applies to them. `==` operator compares values held by variable. Primitive type hold as bits direct values, so `==` works for it without problems, but *reference type* variables don't hold entire objects, they hold *identifier/reference* of an object which allows JVM to locate it and access it when needed. Now when you use `str1==str2` then you are checking if both variables hold reference to *same* object, while `str1.equals(str2)` is checking if even two separate objects are equal (represent same state). – Pshemo Nov 21 '18 at 09:21
0

Use ltstcmdddd.equals(dc) instead of ltstcmdddd==dc

The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal

Sandeepa
  • 3,457
  • 5
  • 25
  • 41