1

In Java, I am trying to print a word that the user inputs 100 times but instead of having it print each instance on a new line, I am trying to print as many as I can on one line and then go to a new line. Would this be easy to do in java? I am new to java so any help would be greatly appreciated! Here is what I have so far below.

public class Main {

    public static void main(String[] args) {

        String name= "TEST"; //defined for debugging purposes

        int i=0;

        while (i < 100)
        {
            System.out.println(name + " ");
            i++;
        }
}

}

John
  • 139
  • 1
  • 12

4 Answers4

2

You should use the System.out.print instead of the System.out.println statement for printing on the same line.

Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • 2
    But how do you have automatic line breaks when the terminal runs out of space? – tim May 18 '15 at 01:56
  • 1
    @tim You don't, Java doesn't support it. That could possibly be a requirement of the terminal itself. Your other choice is to put in a hard limit margin and manually break the lines – MadProgrammer May 18 '15 at 01:57
2
public class Main {
public static void main(String[] args) {

    String name= "TEST"; //defined for debugging purposes

    int i=0;

    while (i < 100)
    {
        System.out.print(name + " ");
        i++;
    }
    System.out.println();
}
}
Don Lee
  • 465
  • 2
  • 11
0

If you wish to follow the 72 - 80 characters per line (CPL) standard, http://en.wikipedia.org/wiki/Characters_per_line, you could just append your data into a single StringBuilder and count each append as an iteration.

public static void main(String[] args) throws Exception {
    final int CHARACTERS_PER_LINE = 72;

    String name = "TEST"; //defined for debugging purposes
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 100; i++) {
        sb.append(name).append(" ");

        // Check if the next append exceeds the CPL
        if (sb.length() + name.length() + 1 > CHARACTERS_PER_LINE) {
            System.out.println(sb); // Print the line
            sb.setLength(0); // Clear for a new line
        } 
    }
    // Print what is left
    System.out.println(sb);
}

Results (Each line should have less than or equal to 72 characters):

TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST 
TEST TEST 
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • Don't forget to add an element to the StringBuilder in the else block; so there are as many elements added as there are iterations. – Woodrow May 18 '15 at 02:27
0

If you want neat line breaks; you can schedule them to automatically occur every 10 iterations or so using the modulus % operator.

public class Main {

    public static void main(String[] args) {

        String name= "TEST"; //defined for debugging purposes

        int i=0;

        while (i < 100)
        {
            System.out.print(name + " ");
            if(i%10 == 0 && i != 0) {
                System.out.println();
            }
            i++;
        }
}

Likewise, you can always use the newline character \n to print on a new line. In java escape sequences in String literals start with the \ character, followed by an escape character. Doing \n is a newline, \t is a tab, \' is a single quote, \" double quote, 2 \, backslash - those are the common ones.

Using newlines & a ternary operator:

while (i < 100)
{
    String line = i%10 == 0 && i != 0 ? name + "\n" : name + " ";
    System.out.print(line);
    i++;
}

If you aren't familiar with the ternary operator; it's a simplified if/else statement. The ternary operator above is the same as saying:

String line = "";
if(i%10 == 0 && i != 0) {
  line = name + "\n";
}
else {
  line = name + " ";
}

That's a simple way of formatting that doesn't take into account character length.

TNT
  • 2,900
  • 3
  • 23
  • 34
Woodrow
  • 136
  • 2
  • 10