DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2005

    Boolean method for a class with an array...Please help!!


    I'm still new to Java, and get a bit lost sometimes implementing simple stuff.

    I am writing a class that defines an array, and needs to have two constructors, one that takes no parameters and creates an empty list, and another that takes the name of a text file as a parameter and creates a list containing the numbers listed in the text file.

    I have also added a method that adds a new number to the list at any time. However, I need a boolean method that returns 'true' if the list contains the given number and 'false' otherwise. The problem is, I'm not sure how to implement this correctly.

    Also, how do I construct a toString method that will return a string representing the numbers in the list.

    My code so far is as follows:

    package testing;

    import simplejava.SimpleReader;

    public class NumberList {

    int [] numbers;
    int arraySize, size;

    public NumberList()
    int[] numbers = new int[10000];
    arraySize = 10000;
    size = 0;

    public NumberList(String fileName)
    { SimpleReader file = new SimpleReader(fileName);
    size =0;
    int next = file.readInt();
    while (!file.finished())
    { size++;
    next = file.readInt();

    file = new SimpleReader(fileName);
    numbers = new int[size];
    arraySize = size;
    for (int i = 0; i<size; i++)
    numbers[i] = file.readInt();

    public void add(int next)
    numbers[size] = next;


    Any help would be appreciated!!!

  2. #2
    Join Date
    Dec 2004
    San Bernardino County, California
    I am going to respond only to your specific questions and not comment on the code in general.

    1. The boolean return value: your method tries to FIND a number in your array and, if it finds the number, returns TRUE otherwise returns FALSE. So, you create an indicator ( let's say private boolean flag = false; ), then implement a search of your array and, if it finds the int being searched for sets flag=true, then returns the value of flag. Your work, here, will be implementing your search.

    2. implementing your numbers.toString() - remember how to walk through your array one element at a time, and build your return string by adding the "string" value of the element to the string you are building. The primitive data types "cast themselves" to strings [actually, the compiler creates the code for you to cast an int to a string]. So, initialize your string variable, then you can use the += operator to add the numbers[i] element to the string, then return the string variable.

  3. #3
    Join Date
    Nov 2005

    re: Boolean method for a class with an array...Please help!!


    Thanks for your advice. I'll have to go back over my ref. notes and see where to go from here.

    How bad is the included code? I'm still getting used to programming in general, let alone Java.

    Many thanks!!

Similar Threads

  1. Return an array from a class method
    By nubira in forum C++
    Replies: 5
    Last Post: 11-04-2005, 02:53 PM
  2. array initializer for class member
    By jab630 in forum C++
    Replies: 2
    Last Post: 09-21-2005, 08:49 AM
  3. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  4. Replies: 6
    Last Post: 03-26-2002, 09:58 AM
  5. How To Do It - Shared Class Variables Part III
    By Patrick Ireland in forum .NET
    Replies: 5
    Last Post: 05-10-2001, 06:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.