3

I would like to know how to check if a string is instance of a Class in a Server Side of ServerSocket programm. Α client gives an object of class Myclass1 and the server must check if a string that reads from client is instance of MyClass1 or another class(e.g. MyClass2, String, Integer, etc.)

That is my code:

  ObjectInputStream object_input = new ObjectInputStream(sock.getInputStream());
  String string = object_input.readLine();
  if (string instanceof MyClass1){...}
  else if(string instanceof MyClass2){...}

It makes an error and I don't know how to solve it. Please, help me

user3403572
  • 63
  • 1
  • 8
  • 1
    If it's Java (or any other language), you should add the tag to the list of tag. You can edit your post for that. – hivert Mar 29 '14 at 18:10
  • 4
    Not sure how a string would be an instance of anything but a string. Maybe I'm missing something because the language is specified..? – deceze Mar 29 '14 at 18:14
  • yes sorry. it 's java – user3403572 Mar 29 '14 at 18:23
  • Did you mean check if an object is an instance of a given class? If not, then your question does not sound right; You might want to learn more about Classes in Java – Eenvincible Mar 29 '14 at 18:25
  • My error is that I could find if a sting is instanceof Class. I must do that: ObjectInputStream object_input = new ObjectInputStream(sock.getInputStream()); Onject obj= object_input.readLine(); if (obj instanceof MyClass1){...} else if(obj instanceof MyClass2){...} – user3403572 Mar 29 '14 at 18:55
  • I think you are looking for something like if string == 'MyClass1' – Tony Hopkinson Mar 29 '14 at 21:14

3 Answers3

1

You're getting that error because that code is not logical, the compiler knows that String string is an string, that's for sure, so it prevents you from trying to do instanceOf of an string, which is final and does not implements Cloneable

You can see more information about this here

Community
  • 1
  • 1
Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
1

Rewrite your code as follows :

ObjectInputStream objectInput = new ObjectInputStream(sock.getInputStream());

        Object objectFromClient = objectInput.readObject();

        if (objectFromClient instanceof MyClass1) {

        } else if (objectFromClient instanceof MyClass2) {

        }// etc..
Visruth
  • 3,430
  • 35
  • 48
0

If you try to check if a give string (for example: "java.lang.String") represents the valid name of a class in the class loader you could use Class.forName(className). Something like in this example:

public class Demo {
    public static void main(String[] args) {

        System.out.println(isInstance("java.lang.String")); // will print true
        System.out.println(isInstance("Longssss"));         // will print false 

    }
    public static boolean isInstance(String string) {
        try {
            return Class.forName(string).getName().equals("java.lang.String");
        } catch (Exception e) {
            return false;
        }
    }
}

You will have to explore the Class.forName method

Raul Guiu
  • 2,374
  • 22
  • 37