Displaying equation and text on same line JAVA

Hey all, I’m taking Java Programming in school right now. Our first assignment is to display the area and perimeter of a circle, with a known radius

The desired result is for the output to be:

Perimeter = 34
Area= 95

I don’t have an issue getting the equation to calculate, I just cant get the text “Perimeter” and the answer “34” on the same line.

Please help!

public class Assign1 {

public static void main(String[] args) {
	// Display output
	System.out.println( 2 * 5.5 * 3.1415);
	System.out.println( 3.1415 * 5.5 * 5.5);

}

}

Can’t you concatenate the string and the answer together? Like

"Perimeter = " + (2 * 5.5 * 3.1415)

This automatically adds a newline:

System.out.println()

This does not:

System.out.print()

So you could do this:

System.out.print("H");
System.out.print("e");
System.out.print("l");
System.out.print("l");
System.out.print("o");
System.out.print(" ");
System.out.print("W");
System.out.print("o");
System.out.print("r");
System.out.print("l");
System.out.println("d");

to get output that looks like
Hello World

1 Like

What is System.out.println

System.out.println is a Java statement that prints the argument passed, into the System.out which is generally stdout.

  • System – is a final class in java.lang package. As per javadoc, “…Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array…“

  • out – is a static member field of System class and is of type PrintStream. Its access specifiers are public final. This gets instantiated during startup and gets mapped with standard output console of the host. This stream is open by itself immediately after its instantiation and ready to accept data.

  • println – is a method of PrintStream class. println prints the argument passed to the standard console and a newline. There are multiple println methods with different arguments (overloading). Every println makes a call to print method and adds a newline. print calls write() and the story goes on like that.