Reputation: 47
I've looked online for help so far and nothing is working for me. This is what I have right now, can you please tell me what is wrong with it?
import java.util.*;
class employeeProgram{
public static void main(String[] args){
for (int count = 0; count < 1; ){
Scanner input = new Scanner(System.in);
ArrayList<String> employeeNames = new ArrayList<String>();
String commnd;
print("What would you like to do? (Add, Remove, List, Exit): ");
commnd = input.next();
if ("Add".equals(commnd)){
print("Enter the name of the employee you'd like to add to the list: ");
employeeNames.add(input.next());
} else if ("Remove".equals(commnd)){
print("Enter the name of the employee you'd like to remove from the list: ");
employeeNames.remove(input.next());
} else if ("List".equals(commnd)){
for (int j = 0; j < employeeNames.size(); j++){
println(employeeNames.get(j));
println(j);
}
//println(employeeNames);
} else if ("Exit".equals(commnd)){
input.close();
break;
} else {
println("Invalid command.");
}
}
}
public static void println(Object x){
System.out.println(x);
}
public static void print(Object x){
System.out.print(x);
}
}
The for loop isn't beginning, I know that much because the "println(j)" wasn't working either. When I tried the part that I've commented out, all I got from typing list was "[]" and nothing else. I'm a beginner, and this is my first program using ArrayLists, so thanks in advance to anyone who decides to help me out :)
Upvotes: 0
Views: 16652
Reputation: 1045
I would try something else instead of the for loop:
Although what you have here might technically work, for...loops are for deterministic processes. Here you are using it as an infinite loop that you hope to force break at a later point. This is dangerous and could lead to bugs.
I'd change this:
for (int count = 0; count < 1; ){
to this:
bool done = false;
while(!done) {
Then, later, when you want to break out. issue this line:
done = true;
Upvotes: 0
Reputation: 2712
You are creating a new ArrayList everytime you ask a question. Create it outside of the initial loop.
ArrayList<String> employeeNames = new ArrayList<String>();
Upvotes: 0
Reputation: 6909
You need to define your arraylist outsode of your loop, otherwise every iteration creates a new arrayList and the previous changes made to the previous arraylist is garbage collected.
import java.util.*;
class employeeProgram{
public static void main(String[] args){
ArrayList<String> employeeNames = new ArrayList<String>();
for (int count = 0; count < 1; ){
Scanner input = new Scanner(System.in);
String commnd;
print("What would you like to do? (Add, Remove, List, Exit): ");
commnd = input.next();
if ("Add".equals(commnd)){
print("Enter the name of the employee you'd like to add to the list: ");
employeeNames.add(input.next());
} else if ("Remove".equals(commnd)){
print("Enter the name of the employee you'd like to remove from the list: ");
employeeNames.remove(input.next());
} else if ("List".equals(commnd)){
for (int j = 0; j < employeeNames.size(); j++){
println(employeeNames.get(j));
println(j);
}
//println(employeeNames);
} else if ("Exit".equals(commnd)){
input.close();
break;
} else {
println("Invalid command.");
}
}
}
public static void println(Object x){
System.out.println(x);
}
public static void print(Object x){
System.out.print(x);
}
}
Also, there are a few basic changes I would make to this program, first of all - a for loop should be used where you know how many iterations you will do. In your case, a while loop (or better still a do-while loop) is better suited.
Finally creating one liner functions to encapsulate system.out.print and println seem more effort than is necessary.
Upvotes: 0
Reputation: 68715
Define your employeeNames
List outside the first for
loop. ArrayList is getting re-intialized with every iteration of the loop.
Upvotes: 2