-
array problem
I am trying to create an array of a custom class. the user is asked with util scanner ("What kind of ant? (1 for Queen, 2 for Worker and 3 for Drone or -99 to quit)"); and the choice of the array are printed and the program ends when they enter a sentinel value. Here is the code I have so far.
Code:
public class driver
{
public static void main(String Args[])
{
ant z = new ant();
z.set_ant();
ant ar[] = new ant[3];
int count = 0;
while(z.get_ant-ar() != "end")
{
ar[count] = z.get_ant_ar();
count++;
System.out.println(ar[count]);
}
}
}
Code:
import java.util.Scanner;
public class ant extends insect
{
Scanner input = new Scanner(System.in);
private int z;
private String ant;
public void set_ant()
{
System.out.println("What kind of ant? (1 for Queen, 2 for Worker and 3 for Drone or -99 to quit)");
z = input.nextInt();
}
}
public String get_ant_ar()
{
if(z == 1)
ant = "Queen";
if(z == 2)
ant = "Worker";
if(z == 3)
ant = "Drone";
return "ant";
}
}
-
wow this code has a lot of problems
1.
Code:
while(z.get_ant-ar() != "end")
this is a typo, should probably be
Code:
while(z.get_ant_ar() != "end")
2.
that while loop will never exit, because "z.get_ant_ar()" will never return "end"; count will keep incrementing; but when "count" gets to 3,
will be out of bounds
3.
Code:
ar[count] = z.get_ant_ar();
Code:
System.out.println(ar[count]);
it seems from these lines that "ar" should be a "String[]" instead of a "ant[]"
4.
the ant method get_ant_ar() always returns the literal string "ant". it sets the "ant" instance variable, but never uses it; perhaps you wanted to instead