Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Help with ArrayList ( Adding certain elements together)

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by williamsant; September 23rd, 2011 at 07:15 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with ArrayList ( Adding certain elements together)

    I have moved your post to a more appropriate forum.

    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.

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with ArrayList ( Adding certain elements together)

    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.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with ArrayList ( Adding certain elements together)

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

     
    ArrayList tal = new ArrayList();
    FileInputStream fstream = new FileInputStream("chinese.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine
    while ((strLine = br.readLine()) != null)
    {
     tal.add(strLine);
    }
    for (int i=0;i<tal.size();i++)
    {
    system.out.println(tal.get(i));
    }

    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.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with ArrayList ( Adding certain elements together)

    The number inside the file chinese.txt is random numbers
    strLine = br.readLine()
    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

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?

  7. #7
    Junior Member shia's Avatar
    Join Date
    Sep 2011
    Location
    Manchester, UK
    Posts
    19
    My Mood
    Nerdy
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Help with ArrayList ( Adding certain elements together)

    Quote Originally Posted by williamsant View Post
    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 )

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?

  9. #9
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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.

    	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.
    Last edited by tarkal; September 27th, 2011 at 04:14 AM.

  10. #10
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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?

  11. #11
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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!

  12. #12
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with ArrayList ( Adding certain elements together)

    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.

    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!

  13. #13
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Post 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!

  14. #14
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Help with ArrayList ( Adding certain elements together)

    ok I've moved over to here:

    http://www.javaprogrammingforums.com...html#post44016

Similar Threads

  1. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM
  2. Duplicate elements in 2 Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 12:56 AM
  3. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 09:48 PM
  4. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 06:11 PM
  5. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM