-2

I'm trying to get the multiple of 5 println on the same line as the number, but it's not working a.t.m., it prints on the next line. The code uses a for loop which makes it more difficult for me anyone have any suggestions.

public static void main(String[] args)
{
  for(int i=1 ; i<101; i++)
  {

    System.out.println(" " +i);

    if (  i % 2 != 0 && i % 5 == 0 )

       System.out.print("This is an odd number and a multiple of 5 \n " );

   }

  }//main
Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
user2980885
  • 113
  • 2
  • 7
  • 6
    Use `System.out.print(" " +i);` Using `println()`, you end the line with the line separator. As the [doc](http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println(java.lang.String)) says : _"Prints a String and then terminate the line. This method behaves as though it invokes print(String) and then println()"_ – Alexis C. Jan 10 '14 at 12:43
  • `System.out.println` adds `\n` so use `System.out.print` for print on same line – Darshan Patel Jan 10 '14 at 12:44

3 Answers3

5

Just use System.out.print(" " +i); instead of System.out.println(" " +i);

println() terminate the line with \n

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • 3
    Note that `System.out.print("This is an odd number and a multiple of 5 \n ");` could be simplified with `System.out.println("This is an odd number and a multiple of 5");` – Alexis C. Jan 10 '14 at 12:47
  • 2
    _terminate the line with `\n`_ isn't always true, since the line break is system dependant. – Keppil Jan 10 '14 at 12:48
  • 1
    I agree with that @Keppil. And again, this is specified in the doc : _"The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n')."_ – Alexis C. Jan 10 '14 at 12:52
2

System.out.println always ends the current line. In your code, after println(" " + i) the cursor is already moved to new line. You should print the number with print, then print space character, whatever string you want, and then, end the line. You can do it with println. If you don't want to print anything specific on the same line (e.g. because number is not a multiple of 5), then you still need to end the line with an empty println.

Please don't print \n character. Use methods like println to end the line instead, as they are cross-platform compatible.

public static void main(String[] args)
{
  for(int i=1 ; i<101; i++)
  {
    System.out.print(i);

    if(i % 2 != 0 && i % 5 == 0)
       System.out.println(" This is an odd number and a multiple of 5");
    else
       System.out.println();
  }
}//main
ciamej
  • 6,918
  • 2
  • 29
  • 39
-1
public static void main(String[] args){
  for(int i=1 ; i<101; i++){    
     System.out.print(" " +i);    
    if (  i % 2 != 0 && i % 5 == 0 ) {   
       System.out.print("This is an odd number and a multiple of 5 \n" );    
       }
    }
}

println -> prints in a new line. so remove the ln if you want to print in same line

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Next time format your code properly when you post an question or answer. – Ruchira Gayan Ranaweera Jan 10 '14 at 12:53
  • the best thing to do is to add an else statment, got it working now, thanks! – user2980885 Jan 10 '14 at 12:53
  • _"println -> prints in a new line"_ This is not true. It does not print in a new line. It appends the line separator and the next call to `print()` (or `println()`) will be in a new line. As you suggest, it will mean that `print();println();` will have the same behaviour as `println();print();`, which is false. – user2336315 Jan 10 '14 at 13:01