Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Question Help with writing a class

    I have to write a class where it handles Arraylist<String>. In the class I have to store integers in type String. Where I can only take 10 integers ( and have to take into consideration negative numbers). Afterwards I have to create another method where it adds up the sums of the integers and divides them by 5. This is all that I have so far:

    import java.util.ArrayList;
    /**
    * I want it to take only numbers but as Strings, have to take into condideration that the abc can be a
    * possible choice as well as symbols, but I don't want those, If the person inputs a negative number,
    * then I want the compiler to count it as one not as two spaces.
    * I want a method that converts all of my data into interger and adds them together, and then divides it
    * by 5.
    *
    */
    public class IntergerFun
    {
    private ArrayList<String> data;
    private ArrayList<Integer> adding;

    /**
    * Constructor for objects of class IntergerFun
    */
    public IntergerFun()
    {
    data = new ArrayList();
    adding = new ArrayList();
    }

    /**
    * This method will only take intergers as Strings, but will only accept numbers.Take into consideration negative numbers.
    */
    public void store(String s)
    {
    if(s.length() > 10){
    System.out.println("Invalid Entry");
    }
    else{if(s.length() == 10){
    int j = Integer.parseInt(s);
    data.add(s);
    adding.add(j);
    }
    else{
    System.out.println("This is not a valid entry");
    }
    }
    }



    public void print()
    {
    System.out.println(data);
    }

    public void printInt()
    {
    System.out.println(adding);
    }
    }

    I created a second arraylist to try and use that to add my integers, since I created it as type int, but I don't know how to go about that. Any help would be much appreciated.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    If I were you I think I would take things one step at a time and test whether the lists are ending up with the correct values.

    For instance you could create a main() method - in this or another class - and use it to initialise an IntergerFun instance, use that to store() some strings and finally use the print methods to see if the lists are correct.

    Don't forget to include strings like "abc": basically at this stage you should be *trying* to make it fail. If you get incorrect program behaviour and you can't see how to correct it (or why it occurs) then you will have a specific question to ask.

    (When you post code, don't forget to put [code] at the start and [/code] at the end so that it's properly formatted.)

Similar Threads

  1. writing to text Area through Class
    By dilpreet28 in forum New To Java
    Replies: 3
    Last Post: 07-06-2010, 01:11 PM
  2. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  3. Writing in XLS
    By selva.bics in forum Advanced Java
    Replies: 1
    Last Post: 11-09-2009, 10:09 AM
  4. writing array values to another class
    By ronald christian in forum New To Java
    Replies: 27
    Last Post: 11-07-2008, 05:08 PM
  5. writing an array class
    By wardd85 in forum New To Java
    Replies: 5
    Last Post: 07-16-2008, 11:59 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
  •