Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My program won't stop when I enter "done"

I asked a question earlier and since then i've edited my code a bit but now my code won't stop when i reads in done it does not stop.

public class Done {

    public static void main(String[] args){

       Scanner kb = new Scanner(System.in);
       ArrayList<String> sal = new ArrayList<String>();
       int count = 0;
       while (true){
             sal.add(kb.next());
             if (sal.equals("done"))
                 break;
             count++;
      }
      display(sal);
      displayb(sal);
    }

    public static void display(ArrayList<String> sal){

       for (int i=0; i<sal.size(); i++)
            System.out.print(sal.get(i)+ " ");
       System.out.println();
     }

    public static void displayb(ArrayList<String> sal){

       for (int z = sal.size(); z >= 1; z--)
            System.out.print(sal.get(z-1) + " ");
       System.out.println();
    }
 }

My code won't stop when I enter the phrase "done." Anyone know what I might be doing wrong?

like image 308
user2920249 Avatar asked Oct 25 '13 16:10

user2920249


1 Answers

You're checking if the ArrayList sal is equal to the string "done" -- this will never be true. Perhaps you want to check if the latest input is equal to that string:

while (true)
{
    String input = kb.next();

    if (input.equals("done"))
        break;

    sal.add(input);
    count++;
}
like image 84
arshajii Avatar answered Oct 03 '22 14:10

arshajii