Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boolean method linked to an array? Please help!!

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

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 = file.readInt();
}

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

}

Any help would be appreciated!!!
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


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.



The answer to both questions is actually quite simple. Just iterate through your numbers array for both methods. For the boolean method look for the specified number and for the toString() append it to a String object which you return at the end. Hope that helps.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

Thanks for the help. I'll have a go and see how things develop.

Many thanks.
 
Christopher Elkins
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
No problem. BTW I'd take Joannes advice from the Beginners forum as well. And you probably shouldn't double post. That way all of the responses are in one place.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

Yes, I agree with you. Sorry for the double posting!! :roll:
 
    Bookmark Topic Watch Topic
  • New Topic