-3

The code below takes user inputted integers and converts them into ASCII symbols.

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int charCount = in.nextInt();
        for (int i = 0; i < charCount; i++) {
            System.out.println((char) in.nextInt());
        }
    }
}

Right now, it prints each character on a new line:

Input: 097 098 099
Output:
a
b
c

How can I print all the characters onto a single line?

Input: 097 098 099
Output: abc
Ibraheem Ahmed
  • 11,652
  • 2
  • 48
  • 54

2 Answers2

1

If I understand what you're after, simply print each character as you decode it (and you don't need all the temporary variables) and then print a new line after the loop. Something like,

Scanner in = new Scanner(System.in);
int charCount = in.nextInt();
for (int i = 0; i < charCount; i++) {
    System.out.print((char) in.nextInt());
}
System.out.println();

Example input / output

7 69 108 108 105 111 116 116
Elliott
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

You can use System.out.print() instead of System.out.println() - which automatically appends your output with new line character. The other way is to create an array or an ArrayList to store all of your inputs and then simply print its content.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21