I am trying to write a program that lets the user input the size of their class into an array, and then find the average of all the classes.
When I run the program, it prints out the stuff I input into the array, but then it gives me a NullPointerException
.
Code:
public void getinput() { //
String dog="dog";
boolean done=true;
int x=0;
int avg=0;
int z=0;
Scanner scan = new Scanner(System.in);
String [] cs = new String [100];
while(done){
System.out.println("Enter the size of one class. When you are done, enter 'quit'");//ask for class sizes
dog=scan.nextLine();
cs[x]=dog;
x++;
if(dog.equalsIgnoreCase("Quit")){
done=false;
}
}
for(z=0;z<cs.length;z++) {
if (!(cs[z].equalsIgnoreCase("quit"))&& !(cs[z]==null)){
System.out.print(cs[z]+", ");
}
}
for(z=0;z<cs.length;z++) {
if (!(cs[z].equalsIgnoreCase("quit"))&& !(cs[z]==null)){
avg=Integer.parseInt(cs[z])+avg;
System.out.println("");
System.out.println("The average number of students in each classroom is "+avg);
}
}