Output and Printing


Collapse Content

Until now we've just been returning data from our method, but we didn't discuss what how data can get out of a program for people to view. While regular programs for users normally have a visual interface (with things like buttons and menus), they are more complicated than is needed. The simplest way to get data from a program is to print it to the Standard Output. The Output is a place to display text to either the programmer or a command-line user. When learning programming, people usually just print to the Output instead of connecting the whole program to a visual interface.

This is the syntax to print the variable something:

System.out.print(something);

To print something on its own line you type:
System.out.println(something);

You can also print Strings directly, without making any variables: System.out.println("hello world!");

Note that System is capitalized, just like String was.

The I/O Code
All of the programming challenges required printing to the output, and if you look at the bottom of the provided code, you'll see this:

  System.out.println(result); 

result was a variable with your solution, and to print it, you type System.out.println(). Whatever is in-between the ( ) gets printed.

println stands for "Print Line", which means it will add a new line after it prints, but if you didn't want that, you can just type System.out.print().

Printing only works for Strings and characters, but Java automatically converts numbers into Strings when you try printing them, which is why the provided code was able to print your number answers .

Printing Examples

Code Output Explanation
System.out.println("hello"); hello Simple print statement.
System.out.println('a'); a Print a character.
System.out.println("ab"+"cd")'; abcd Prints the 2 Strings combined.
System.out.println(3); 3 Automatically converts a number to String for printing.
System.out.println(32 + 1) 33 Calculates sum of numbers before printing them.
System.out.println("32" + "1") 321 Combines the Strings (which happen to represent numbers) into one String for printing.
String fruit = "apple";
System.out.println(fruit)
applePrints the value of the fruit variable.

Challenge

You will be given a name as input. Please print a message "Hello [name]!" on its own line. (Replace [name] with the given name). In this challenge, you need to write your own code to print the message.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • public class Main {
    public static void doStuff(String name){
    name = name + "!";
    String hello = "Hello ";
    String message = hello + name;
    System.out.printIn(message);
    }

    How come each input doesn't print out on their own line?

  • @Ian, Sorry, the correct code is with a lowercase 'L', not an uppercase I: System.out.println. It stands for Print Line.

  • Thanks, this same issue had me stumbled for both of the Array Loop Practices too.

  • You can complete the task with only one line: System.out.printIn([Some Stuff]);

  • I agree with Cliff, that is how I did it:
    Hint:
    string + variable + char

  • nice article

  • It's just as simple as forgetting the exclamation point to be incorrect.

  • I got all the results correct, and it says "Incorrect". Why ?

  • public static void doStuff(String name){
    name = "Jim!";
    String hi1 = "Hello ";
    String hey1 = hi1 + name;
    System.out.println(hey1);
    name = "Sarah!";

    cont...
  • I don't quite understand this, can someone help me by explaining why
    when I do
    name = "Jim";
    System.out.println("Hello " + name + '!');

    it prints four
    Hello Jim
    Hello Jim
    Hello Jim
    Hello Jim

    I don't understand.

  • @AdhurimEsati, you are given the the String name each time as a parameter, you just need to print that.

  • another simple solution :
    System.out.println ("Hello "+name+"!");

  • import java.util.Scanner;
    public class Main {
    public static void main (String[] args){

        Scanner input= new Scanner (System.in);
        System.out.print ("Enter any Name: ");
        String name=input.nextLine();
        System.out.println ("Hello " + name + "!");
    }
    

    }

  • import java.util.Scanner;
    public class Main(){

    public static void main(String[] args){
    String name="Nisha";
    System.out.print("Hello" + name);
    }
    }

All Node Comments
Contact Us
Sign in or email us at [email protected]