How can I get rid of the last line break in the output? Could that be the problem?
package com.codegym.task.task07.task0715;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
/*
More Sam-I-Am
1. Create a list of the words: "Sam", "I", "Am".
2. After each word, insert the word "Ham" into the list.
3. Display the result on the screen, each list element on a new line.
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
//ArrayList<String> string = new ArrayList<>(Arrays.asList("Sam", "I", "Am"));
ArrayList<String> string = new ArrayList<>();
string.add("Sam");
string.add("I");
string.add("Am");
/*
for (int i = 0; i < string.size(); i++) {
System.out.println(string.get(i));
}*/
for (int i = 0; i < string.size(); i++) {
string.add((i+1),"Ham");
i++;
}
for (int i = string.size()-1; i >= 0; i--) {
System.out.println(string.get(i));
}
}
}