Error with Scanner variable in while

alpacalover99alpacalover99 Barcelona
edited February 2015 in Java Beginners

Hello, everybody. I'm new in Java and I find very complicated because of the errors that I come across with. The problem that arisen is from this piece of code:

Main Class:

`
public class Main {

public static void main(String[] args){

    Answer a = new Answer();

    String ans = null;

    while(ans != "A"){

        ans = a.create();

        System.out.print(ans + "\n");

    }

}

}`

Answer class:

`
import java.util.Scanner;

public class Answer {

public String create(){

    Scanner s = new Scanner(System.in);

    return s.next();

}

}`

What I want is that the program allows me to writer something down, in the case that what I wrote down was not the letter A, the program has to allow me to write down else, otherwise it would stop. But, even though I write "A", the program is still on, telling me that I have to write something else. What's wrong with the code?

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories