Help with ArrayList ( Adding certain elements together)

I will put this very simple. i am using arraylist to store 20 values. Suppose if i want to add 2 of those values together. The name of my arraylist is balance. So if i want to get out of my 20 elements 2 values and add them, for example

a = balance.get(20) + balance.get(18);

That does not work i get the error, can anyone try and help me out. I figured out how to add all arrays but not 2 specific ones.

I also wanted to know how to convert an object to an int.

Re: Help with ArrayList ( Adding certain elements together)

I have moved your post to a more appropriate forum.

Quote:

That does not work i get the error,

Please post the full error message, and remember that arrays and List's are 0 indexed - meaning the first item is at index 0. For a size of 20, the last index in the List is 19.

Re: Help with ArrayList ( Adding certain elements together)

Quote:

I also wanted to know how to convert an object to an int.

The only sure way is to use Object.hashCode(), but perhaps you're not asking the right question.

It depends on what the Object is. If the Object is a java.lang.Integer, then there's a handy method that'll get you an int. If the Object is a java.lang.String, you may still use a method from java.lang.Integer (look in the API docs), but you should probably also validate your String first. Try to be more specific.

Re: Help with ArrayList ( Adding certain elements together)

Ok to put things alittle more clear, ill post some code.

Basically afterwards i will i will have values in lines 1-20 which is 0-19.

Suppose i want to add tal.get(1) and tal.get(2) and store that added value into another variable, what will i have to do? The number inside the file chinese.txt is random numbers, lets say its 5 and 6 so the added value would be 11.

Re: Help with ArrayList ( Adding certain elements together)

Quote:

The number inside the file chinese.txt is random numbers

You're reading Strings from the file. If you want to store / manipulate numbers, you should be converting each String you read from your input to a number. If the numbers are integer, then java.lang.Integer has a method to convert String to int. If the numbers are not integers, then you may need to use java.lang.Float, java.lang.Double or even java.math.BigDecimal. If the numbers are decorated in any way (such as "£2,045.67") than you'll need to use something that 'understands' funny formats such as (possibly - I've never used it) the 'parse' method in java.text.DecimalFormat

Re: Help with ArrayList ( Adding certain elements together)

Is it possible you can give me some code to help me our so what should i do, do that line, then convert each line to int, then i am able to add them?

Re: Help with ArrayList ( Adding certain elements together)

Quote:

Originally Posted by

**williamsant**
Is it possible you can give me some code to help me our so what should i do, do that line, then convert each line to int, then i am able to add them?

See Integer (Java Platform SE 7 )

Re: Help with ArrayList ( Adding certain elements together)

Ok i figured it out, what i wanted to ask also is i wanted to create a simple GUI with that program, and display the values in a textbox of some sort, also a simple one. Any suggestions?

Re: Help with ArrayList ( Adding certain elements together)

I'm working on a similar problem at the moment, however I have taken a slightly different approach and as such I now have a related but slightly different problem. I have been given a very large number (1000 digits long) and I need to find the highest product of any 5 consecutive numbers. My basic approach is as follows:

1. To use a String for the large number.

2. Convert the String to a char[].

3. Convert the char[] to an int[].

4. Use a couple of for loops to iterate through the int[] showing the next 5 consecutive numbers for every number.

5. Store the product of each 5 consecutive numbers in an int[].

6. Find the largest value in the int[].

I'm currently foxed on the for loops. I can't work out how to 'breakup' the loop so that each int[] index holds just the product of the last 5 numbers. At the moment all my attempts have lead to simply generating the continual product of all the groups of 5.

Code :

public int multiplyConsecutiveNumbers() {
int product = 0;
for (int i = 0; i < mainDigitArray.length - 4; i++) {
for (int j = i; j < i + 5; j++){
// This is the part that I am stuck on
}
}
return product;
}

Thanks

PS. Sorry I am relatively new to forums. Not sure if its right to join in to a thread like this asking a similar question or whether I should start my own. If its the latter then appolagies williamsant.

Re: Help with ArrayList ( Adding certain elements together)

That's a different problem, so I'd say you should have a new thread. Why aren't you using BigInteger for your problem?

Re: Help with ArrayList ( Adding certain elements together)

Thanks Sean4u, Is there a way to move it or is it just a case of delete it here and start it again?

N.B. I'm not using BigInteger because up until this moment I had never heard of it! :)

Re: Help with ArrayList ( Adding certain elements together)

Quote:

Is there a way to move it

I think I've seen it happen, so maybe there is. I might even (unbeknownst to me) have the power to do it myself, though I tend to be better at theory than implementation. :D

Your loop-of-loops should work, but I would look at this problem and see *intermediate results*. The product of the second 5 integers is the product of the last four (from the result before) and the sixth element in the list. Instead of potentially multiplying each element 5 times, you could 'slide a 5 element window' over the list, doing something appropriate with the element that falls out of the trailing edge of the window and something appropriate with the element that slides under the leading edge of the window.

It's pencil and old envelope time!

Re: Help with ArrayList ( Adding certain elements together)

Genius idea... as you have undoubtably guest I'm a java newbie so the practical implementation of such a windo might be out of my imediate reach but I love the idea and will see were it takes me.

Im just going to try and move this post before continuing however having looked around I think that only forum moderators can move posts! #-o

Re: Help with ArrayList ( Adding certain elements together)