0

Apologize for the nob question. I tried unsuccessfully to go through other similar questions but most of them were way more complex.

The problem I have is within the while loop. Even though I write "test" which is supose to be the end of the loop statement, stucks on it. Does anyone know what I'm doing wrong? Thanks in advance!

    public static void main(String[] args) {
    Metodos consulta = new Metodos();
    int x;
    String a=null, b=null;
    Scanner n;



    while(true){
        while(a!="test")
        {
            System.out.println("Escriba la unidad de persistencia con la que quiere trabajar/n");
            n = new Scanner(System.in);
            a = n.nextLine ();
            consulta.nombrePersistencia(a);
            System.out.println("The value of a is: " +a);
        }

Note: The command shows the value of a is test. This even cross me more!.

birdman
  • 211
  • 1
  • 5
  • 13

1 Answers1

0

Try:

    public static void main(String[] args) {
Metodos consulta = new Metodos();
int x;
String a=null, b=null;
Scanner n;



while(true){
    while(!a.equals("test"))
    {
        System.out.println("Escriba la unidad de persistencia con la que quiere trabajar/n");
        n = new Scanner(System.in);
        a = n.nextLine ();
        consulta.nombrePersistencia(a);
        System.out.println("The value of a is: " +a);
    }

You have to use .equals when you check strings for their content. You can't use == nor !=.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chris Cromer
  • 126
  • 6
  • Yet another example of an answer being posted **AFTER** the question gets closed... how did you workaround it? – gparyani Aug 29 '14 at 20:24
  • The question was open and without any answers when I was typing my response. – Chris Cromer Aug 29 '14 at 20:25
  • But the "Post Your Answer" button must have been disabled for it... did it not disable for you, did you manually workaround it using the browser developer tools, or did you use the mobile site? The server does not actually block answers to a question until 4 hours after closure. – gparyani Aug 29 '14 at 20:27
  • The "Post Your Answer" button was there when I opened the question. I pressed it before the question was closed... so I had a textbox on my screen and was typing. The message about it being closed was NOT on my screen when I submitted the post. – Chris Cromer Aug 29 '14 at 20:30
  • So, I guess you hit the submit button before closure, and then it was closed moments before the actual posting of the answer. I guess that's why we have a grace period to work around race conditions like these... – gparyani Aug 29 '14 at 20:34
  • Most likely that is the case. Either that or it's a bug in this website's code that allows the post even though it was closed. – Chris Cromer Aug 29 '14 at 20:38