Results 1 to 2 of 2
Thread: Help with writing a class
- 10-07-2010, 11:09 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 1
- Rep Power
- 0
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.
- 10-08-2010, 12:53 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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
-
writing to text Area through Class
By dilpreet28 in forum New To JavaReplies: 3Last Post: 07-06-2010, 01:11 PM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 02:29 AM -
Writing in XLS
By selva.bics in forum Advanced JavaReplies: 1Last Post: 11-09-2009, 10:09 AM -
writing array values to another class
By ronald christian in forum New To JavaReplies: 27Last Post: 11-07-2008, 05:08 PM -
writing an array class
By wardd85 in forum New To JavaReplies: 5Last Post: 07-16-2008, 11:59 PM
Bookmarks