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?
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++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With