Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-11-2007, 06:13 PM
Member
 
Join Date: Dec 2007
Posts: 1
wats is on a distinguished road
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"; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-12-2007, 09:08 AM
Member
 
Join Date: Dec 2007
Posts: 12
spoon! is on a distinguished road
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,
Code:
ar[count]
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.
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
Code:
return ant;
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
array problem oceansdepth New To Java 3 04-05-2008 04:25 AM
Array problem.. help needed please! SCS17 New To Java 3 03-07-2008 12:30 AM
Array List Problem khamuruddeen New To Java 1 12-22-2007 10:10 AM
Problem with array Copy coco New To Java 1 08-07-2007 09:46 AM
array problem Albert Advanced Java 2 07-01-2007 03:13 AM


All times are GMT +3. The time now is 04:08 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org