1

Cant seem to figure out why my array isnt outputting the correct value. The sum prints out as the first integer in the array. I wanted the sum of all the integers in the array. Any ideas what could be wrong? I attempted to convert the int to a string to be sent out.

  //Add values of Integers
  int i; int sum = 0; 
  for(i = 0; i < intarray.length; i++){  
      sum = sum + intarray[i];
  }

  String sumOut =  Integer.toString(sum);
  System.out.println( "to Client: " + sumOut);
  toclient.writeBytes("Sum = " +sumOut+'\n');
4
  • 6
    Where have you defined your array? Commented Jul 26, 2012 at 0:14
  • 1
    You can drop String sumOut = ... and use sum when printing, the toString method is called implicitly anyway. Commented Jul 26, 2012 at 0:16
  • 1
    does the array by chance have one value? Commented Jul 26, 2012 at 0:16
  • try to print out sum by using System.out.println(sum); immediately after the for loop Commented Jul 26, 2012 at 0:17

2 Answers 2

2

Here's how I'd do it:

package cruft;

/**
 * IntegerSum description here
 * @author Michael
 * @link
 * @since 7/25/12 8:14 PM
 */
public class IntegerSum {
    public static void main(String[] args) {
        int [] values = new int[args.length];
        for (int i = 0; i < args.length; ++i) {
            values[i] = Integer.valueOf(args[i]);
        }
        System.out.println(String.format("sum = %d", sum(values)));
    }

    public static int sum(int [] values) {
        int sum = 0;
        for (int value : values) {
            sum += value;
        }
        return sum;
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Which is essentially what the OP posted (other than not with a for-each loop). Unfortunately, the OP didn't post how he is initializing the array.
Not really - what's all that stuff about converting the sum to a String and whatnot? I think mine is cleaner: the sum is encapsulated into a single method that can be called in other contexts, and it doesn't mingle summing and output. These are the small details of which coding is made.
Granted, your design is cleaner. I was commenting on the logic. (And yes, I agree that there is no need to convert the int to a String explicitly...see my answer.)
The loop isn't written using the standard Java idiom; it fails to leverage the += operator. That's a lot of commentary for such a small snippet.
Which is all syntactic sugar that still doesn't help answer the question of why the original code doesn't work in the first place. The problem is very likely in the array initialization.
|
2

Your code looks correct. Try printing out the contents of intarray to be sure what it contains. Also, how do you put values into intarray. Post some code to show us. Perhaps the array is not populated correctly.

Also, there is no need for the line

String sumOut =  Integer.toString(sum);

Java will convert an int to a String automatically when you use the + operator, for example:

System.out.println("to Client:" + sum);

1 Comment

Thanks this fixed the problem by its self. The problem was that when I used .toString it only displayed the first park of the array. When I used just sum it displayed the total. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.