PiGamma
PiGamma

Reputation: 157

To remove commas the end of list of numbers

The question is to print out the elements having even actual position of even index starting from 1 and their sum in the following format.For example, if number of elements is 6 and the elements are 10, 20, 30, 40, 100 and 200, the output is obtained as

20, 40, 200
260

where the second line of output represents the sum of the even indexes but I got the output as

20, 40, 200
260

How can I get rid of the comma at the end?

import java.util.*;

class Main {
    public static void main(String args[]) {
        int n, i, sum = 0;
        Scanner input = new Scanner(System.in);
        n = input.nextInt();
        int[] data = new int[n];
        for (i = 0; i < n; i++) {
            data[i] = input.nextInt();
        }
        for (i = 0; i < n; i++) {
            if ((i + 1) % 2 == 0) {
                System.out.print(data[i] + ",");
                sum += data[i];
            }
        }
        System.out.print("\n");
        System.out.println(sum);
    }
}

Upvotes: 1

Views: 377

Answers (4)

Jordi Castilla
Jordi Castilla

Reputation: 26961

In this cases I use a simple trick:

String SEPARATOR = "";
for(i = 0; i < n; i++) {
    data[i] = input.nextInt();
}
for(i = 0; i < n; i++) {
    if((i + 1) % 2 == 0) {
        System.out.print(SEPARATOR + data[i]);
        sum += data[i];
        SEPARATOR = ",";
    }
}

Upvotes: 3

Adriaan Koster
Adriaan Koster

Reputation: 16209

Here is a Java 8 solution using streams:

import java.io.*;
import java.util.*;

public class Adder {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringJoiner numbers = new StringJoiner(",");
        final boolean[] flipFlop = new boolean[]{true};
        int size = Integer.parseInt(in.readLine());
        int sum = in.lines()
                .limit(size)
                .mapToInt(Integer::parseInt)
                .filter(i -> {
                    flipFlop[0] = !flipFlop[0];
                    return flipFlop[0];
                })
                .peek(i -> numbers.add(String.valueOf(i)))
                .sum();
        System.out.println(numbers);
        System.out.println(sum);
    }
}

Upvotes: 0

Adriaan Koster
Adriaan Koster

Reputation: 16209

Here's an alternative solution:

// create Scanner in try-with-resources block so it gets closed at the end
try (Scanner input = new Scanner(System.in)) {
    int size = input.nextInt();
    int sum = 0;
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < size / 2; i++) {
        // read and discard even index (odd position)
        input.nextInt();
        int value = input.nextInt();
        sb.append(value).append(",");
        sum += value;
    }

    // remove last separator
    sb.deleteCharAt(sb.length() - 1);

    // if the size is odd, read and discard one more number
    if (size % 2 == 1) {
        input.nextInt();
    }
    System.out.printf("%s%n%s", sb, sum);
}

Upvotes: 1

ncw
ncw

Reputation: 1733

You can also use the ternary operator to do it like this

for (int i = 0; i < n; i++) {
    if ((i + 1) % 2 == 0) {
        System.out.print(data[i] + i != n-1 ? "," : "");
        sum += data[i];
    }
}

Upvotes: 1

Related Questions