Passing information to other method

Kevin Nathaniel Tiscareno :

I have trouble with a little section of my code.

I asked the user to give me an answer (using scanner), and know I want to use that information in other method. Then I want to compare it to see if the answer is correct. Her is a little part of my code:

public void enterYourName(){
    System.out.println("What is your name?");
    name = input.nextLine();
}

And I want to use this information in another method:

public void check(String personName){
  if(name.contentEquals("Eric")){
    /* TODO */
  }
}

I'm receiving these error messages:

  • The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files.

  • The method contentEquals(java.lang.StringBuffer) in the type java.lang.String is not applicable for the arguments (java.lang.String).

What I am doing wrongly? Also, feel free to correct me if the if the statements are wrong.

peterh - Reinstate Monica :
  1. java.lang.CharSequence should be available for you even without any import - it is part of the JDK. It can't be said easily, why it won't work, but it should.
  2. As per its documentation page, String.contentEquals can be parametrized by a StringBuffer or by a CharSequence. You want to call it by a String, it won't work.

(2) can be solved very easily:

  • As @AndréMartins says in comment, simply you can use String.equals(...) or String.equalsIgnoreCase(...).
  • Both StringBuffer and CharSequence classes have a toString() method, what converts them to a String.

I also suspect, something might be problematic with the JDK/JRE installation of your machine.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=418205&siteId=1