-4

I am trying to get an input from console, assign it to a string variable. Then I'd like to concatinate it with another variable. Provided if the user enters the right character each time, soon it'll make up a word. Once this word matches the desired one the loop stops. Need your help though.

public class expl {

    public static void main(String[] args) {
        String consatinate = "a";
        String needed = apple;
        while (!consatinate.equals(needed)) {
            System.out.println("Enter a letter");
            String input = System.console().readLine();
            consatinate = consatinate.concat(input);        
            System.out.println(consatinate);
        }
    }
}

Error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: apple cannot be resolved to a variable at expl.main(expl.java:6)

falsarella
  • 12,217
  • 9
  • 69
  • 115
  • 2
    What is the error you are facing? Also what is `apple` (you seem to know how to create String literals since you already created one - `"a"` - so did you perhaps forgot to surround it with `"`)? – Pshemo Feb 23 '15 at 21:36
  • Exception in thread "main" java.lang.Error: Unresolved compilation problem: apple cannot be resolved to a variable at expl.main(expl.java:6) sorry should have given it – Honest Learner Feb 23 '15 at 21:39
  • Don't post important informations like error messages as comment. Instead put it in question using [edit] option (placed below your post). – Pshemo Feb 23 '15 at 21:40

1 Answers1

1

Apple is a literal string, so it should have quotations around it:

public class expl {

    public static void main(String[] args) {
        Scanner inputScanner = new Scanner(System.in);
        String consatinate = "a";
        String needed = "apple";
        while (!consatinate.equals(needed)) {
            System.out.println("Enter a letter");
            String input = inputScanner.nextLine();
            consatinate = consatinate.concat(input);        
            System.out.println(consatinate);
        }

        inputScanner.close();
    }
}

I would also asume that "consatinate" should be named concatenate, but that's just a guess.

ThePerson
  • 3,048
  • 8
  • 43
  • 69
  • after correcting apple it says Exception in thread "main" java.lang.NullPointerException at expl.main(expl.java:9) And I know about spelling, I did it in purpose because it's just a variable name. Maybe I am wrong. – Honest Learner Feb 23 '15 at 21:42
  • 1
    @HonestLearner that is different problem which deserves different question. But to avoid it take a look at http://stackoverflow.com/questions/26470972/trying-to-read-from-console-java/26473083#26473083 – Pshemo Feb 23 '15 at 21:44
  • Your problem with the null pointer is related to the question here: http://stackoverflow.com/questions/104254/java-io-console-support-in-eclipse-ide However, I have updated the answer for you to use a Scanner. – ThePerson Feb 23 '15 at 21:45
  • If it works for you, please remember to mark it as the answer by clicking the green tick - i'd much appreciate it. – ThePerson Feb 23 '15 at 21:49
  • If I had required reputation.))))))))))) Not this time unfortunately. – Honest Learner Feb 23 '15 at 21:53
  • Oh how stack overflow robs me :D. Oh well, glad it helped :). – ThePerson Feb 23 '15 at 21:54
  • Sure than, but I can't see the green tick nor accept a question. Come on it's almost there))) – Honest Learner Feb 23 '15 at 21:59
  • @HonestLearner You don't accept question, but answer :) – Pshemo Feb 23 '15 at 22:01
  • :) got it. Welcome to stack overflow. – ThePerson Feb 23 '15 at 22:10