Results 1 to 2 of 2

Thread: array problem

  1. #1
    wats is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default 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.

    Java 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]);
    			
    		}
    	}	
    
    }
    Java 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";
    	}		
    }

  2. #2
    spoon! is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    wow this code has a lot of problems

    1.
    Java Code:
    while(z.get_ant-ar() != "end")
    this is a typo, should probably be
    Java 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,
    Java Code:
    ar[count]
    will be out of bounds

    3.
    Java Code:
    ar[count] = z.get_ant_ar();
    Java Code:
    System.out.println(ar[count]);
    it seems from these lines that "ar" should be a "String[]" instead of a "ant[]"

    4.
    Java Code:
    return "ant";
    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
    Java Code:
    return ant;

Similar Threads

  1. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 02:25 AM
  2. Array problem.. help needed please!
    By SCS17 in forum New To Java
    Replies: 3
    Last Post: 03-06-2008, 10:30 PM
  3. Array List Problem
    By khamuruddeen in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 08:10 AM
  4. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •