Saverio
Saverio

Reputation: 99

Java: Trying to get my code to print on one line instead of five

My code below is working but I would like for the letters to be printed on one line instead of five. Does anyone have any recommendations on how to do that?

Code:

import javax.swing.JOptionPane;

public class Problem67 {

    public static void main(String[] args) {

        //Get input from user
        String code = JOptionPane.showInputDialog( null, "Enter your secrete message");

        //Printing even characters
        for(int i = 0; i < code.length(); i = i + 2){
            System.out.println(code.charAt(i));
        }

    }

}

Test Data: Hiejlzl3ow

Printed Results:

H
e
l
l
o

Thank you!

Upvotes: 1

Views: 43

Answers (1)

Juned Ahsan
Juned Ahsan

Reputation: 68715

Use print instead of println. print method prints everything without adding a line feed.

replace this

        System.out.println(code.charAt(i));

with

        System.out.print(code.charAt(i));

Upvotes: 2

Related Questions