scoob
scoob

Reputation: 367

Change an array of ints to a series of strings

I'm trying to achieve the following output from the following input:

Sample input:
3 4
2 1
4 5
-1

Sample output:
7
3
9

So far, I have my program doing all of the math and I'm getting it to terminate when the user puts in a negative number. My problem is that I'm having trouble getting it to print in the above format. A sample of my input/output is below:

9 9
8 8
9 -8
[18, 16]

Here's my code so far:

import java.util.ArrayList;
import java.util.Scanner;
    public class sums{
     public static void main(String[]args){
         int addition = 0;
         int previous = 0;
         ArrayList<String> sums = new ArrayList<String>();
         String sumsy = new String("");
         for (int i=0; i<=i;){
             Scanner in = new Scanner(System.in);
             previous = in.nextInt();
             if (previous<0){
                 break;
             }
             else {
                 addition = in.nextInt();
                 if (addition<0) {
                     break;
                 }
                 else {
                     addition += previous;
                     sums.add(addition+"");
                 }
             }
         } System.out.println(sums);
     }
}

I believe the answer lies in somehow using a delimiter ("/n") somewhere, and getting my array of ints to print out just as strings.

I'm sorry, these small things elude me completely. Can anyone point me in the right direction?

Thank you! -Helen

Upvotes: 3

Views: 99

Answers (4)

CCC
CCC

Reputation: 151

System.out is a PrintStream object, and its println methods are overloaded for a variety of different types.

In this case, because sums is an ArrayList, you're calling println(Object x), which works by calling String.valueOf(x) on the passed object (which in turn calls x.toString() if x does not equal null), and printing the resulting String.

Essentially, when you pass something other than a String or a primitive to println, you're delegating the details of how it gets printed to the class of the object. In this case, since ArrayList is a library class, you have no control over this.

To make it work the way you want, you need to iterate over the values in sums, something like:

for (String someSum : sums) {
    System.out.println(someSum);
}

If you're not familiar with that loop syntax, see the second half of this page: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83587

Do you really have to use an ArrayList? IMO, this makes it much more complicated than necessary. From your example input, it appears that you simply want to sum each pair of numbers. This doesn't seem to require an array. You could just add the two numbers together and immediately print out the result.

Upvotes: 0

BostonJohn
BostonJohn

Reputation: 2681

sums is an list of strings, not a string, so printing it isn't going to give you what you want. You have two options, you could do for(String i : sums){ System.out.println(i); }

or you could make sums a stringbuilder and do sums.append(addition) and then System.out.println(sums.toString())

Upvotes: 0

jdevelop
jdevelop

Reputation: 12306

for (String sum : sums) System.out.println(sum)

use this instead of

System.out.println(sums)

Upvotes: 3

Related Questions