0

How can I use objects in a switch statement? I am obliged to use objects in my code. I need to know how to compare objects, by using and If statement or a switch statement.

    userInput = userLnr.readLine();

        if (userInput !=null) {
            clerk1.username = userInput;

            userInput = userLnr.readLine();
            clerk2.username = userInput;

            userInput = userLnr.readLine();
            clerk3.username = userInput;

            userInput = userLnr.readLine();
            clerk4.username = userInput;

            userInput = userLnr.readLine();
            clerk1.password = userInput;

            userInput = userLnr.readLine();
            clerk2.password = userInput;

            userInput = userLnr.readLine();
            clerk3.password = userInput;

            userInput = userLnr.readLine();
            clerk4.password = userInput;
        }

    System.out.println("Enter username: ");
    String inputUsername = Keyboard.readString();

    System.out.println("Enter password: ");
    String inputPassword = Keyboard.readString();

    switch(inputUsername){
        case clerk1.username:
    }
FelipeP
  • 69
  • 8
Tex_Dex
  • 3
  • 4
  • 1
    I assume that this is for a school assignment or something similar. If it's not then this is not how you want to be doing any of this. – Matthew Brzezinski Nov 24 '16 at 18:01
  • Yes its for a school project. – Tex_Dex Nov 24 '16 at 18:04
  • 1
    Even for a school project this is not how you want to be doing this. More importantly this is not how you *can* do this. While `switch` does work with `Strings` (these days), you need compile time constants for `case`. So `case "johndoe":` will work, but `case clerk1.username:` isn't possible. However it's a bit unclear what you're even attempting to do. – Kayaman Nov 24 '16 at 18:32
  • I knew about the method you said by doing ex. case"johndoe": . But is there any way to compare objects? as I am obliged to use objects in my project – Tex_Dex Nov 24 '16 at 18:35
  • Well, considering that Java is an object oriented language, it would be hard *not* to use objects. Are you familiar with the `equals()` method? – Kayaman Nov 24 '16 at 18:39
  • Yes I am, but I tried doing - if (inputusername.equals(clerk1.username){} But it seems that it doesn't work either – Tex_Dex Nov 24 '16 at 18:41
  • That should work. Seems you maybe doing something else incorrectly. – Ole V.V. Nov 24 '16 at 18:43
  • I get the error (cannot find symbol - variable clerk1) by doing: if (inputUsername.equals(clerk1.username)){ } – Tex_Dex Nov 24 '16 at 18:45
  • I see no declaration of `clerk1` in the code you've posted. Either you're trying to use it outside its scope or you haven't declared it at all. – Ole V.V. Nov 26 '16 at 08:01
  • See [Please explain the following about the "Cannot find symbol" error:](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean). – Ole V.V. Nov 26 '16 at 08:05
  • Yes I do have clerk1 declared in the Code as shown below: User clerk1 = new User(); – Tex_Dex Nov 26 '16 at 17:49
  • 1
    Thanks. I found the problem. I was declaring clerk1 in the try{} – Tex_Dex Nov 26 '16 at 18:18

0 Answers0