06 Java Fundamentals | Output in Java | By Dummy for Dummies


OUTPUT IN JAVA

So, fellow monkeys we learned taking input, now let's talk about giving output on screen. We are already familiar with print though, i.e:

  System.out.println("Hello, World!");  

This same line is used for every output but with a bit modifications for different purposes. They are as follow (we will be focusing on golden lines):


1. SIMPLE OUTPUT:


public class Main {

    public static void main(String[] args){

        System.out.print("Hello, World!");  

    }

}

OUTPUT:

Hello, World! 
  

 System.out  = System gives output

 print  = Print the content ahead

 ("Hello, World!")  = The content inside bracket is printed. 


Another way to print it is:


public class Main {

    public static void main(String[] args){

      System.out.print("Hello"); 

      System.out.print("World");

    }

}


OUTPUT:
HelloWorld
Therefore we need a way to print the content on different lines for better representation.


2. OUTPUT WITH NEXT LINE:


public class Main {

    public static void main(String[] args){

      System.out.println("Hello, World!"); 

    }

}

OUTPUT: 

Hello, World!

 println  = Print the content ahead and go to next line

3. OUTPUT WITH VARIABLE:

public class Main {

    public static void main(String[] args){

       int age = 20;  

       System.out.println("Your age: " + age);  

    }

}

OUTPUT:

Your age: 20

 "Your age: "  = Prints this as it is

 +  = join the content on right to content on left (Called concantenation)

 age  = joins whatever is inside in variable as it is no matter what datatype

So final output is  Your age: 20  


4. FORMATED OUTPUT:

Using formated output you can output anything in specific design. Here the concatenation using  +  does not work. Here is a demonstration:

public class Main {

    public static void main(String[] args){

       int age = 20;  

       System.out.printf("Your age: %d", age);  

    }

}


OUTPUT:

Your age: 20

 "Your age: "  = Prints this as it is

 %d  = reserve space for integer variable

 ,  = Tells java to place the variables on right in reserved spaces

 age  = Variables that is to be placed in reserved places

So final output is   Your age: 20  


To reserve space for each variable a placeholder for that specific datatype is used. Here is a list for them all:

Placeholders for each datatype: 

 %d  → Integer placeholder

 %f  → Double / Float placeholder

 %c  → Character placeholder

 %s  → String placeholder

 %b  → Boolean placeholder


Extra things for styling:

 \n  → Since println does not work here we use  \n  for next line.

 \t  → Prints tab space 

 \"  → Since quotations are used to represent string, so to print quotation inside string use backslash with them

 \\  → Prints tab space similarly for printing backslash use another backslash with it


Let's write a simple code for all....



public class OutputDemo {

    public static void main(String[] args) {

        

        // Variables for each datatype

        int age = 20;

        double gpa = 3.75;

        char grade = 'A';

        String name = "Hassan";

        boolean pass = true;


        // Demonstrating placeholders with escape sequences

        System.out.printf("Student Details:\n");

        // \n = next line


        // %s for string

        System.out.printf("Name:\t%s\n", name); 

        //prints tab space + name + new line

        

        // %d for integer

        System.out.printf("Age:\t%d\n", age);

        //prints tab space + age + new line


        // %f for double

        System.out.printf("GPA:\t%.2f\n", gpa);  

// prints tab space + GPA + new line    (%.2f = 2 numbers after decimal)


        // %c for character

        System.out.printf("Grade:\t%c\n", grade);

        //prints tab + grade + new line


        // %b for boolean

        System.out.printf("Passed:\t%b\n", pass);

        //prints tab + pass +new line


        // Special escape sequences

        System.out.printf("Quote Example:\t\"Work hard, dream big!\"\n");

        //prints Quote Example:    "Work hard, dream big!"

        System.out.printf("Backslash Example:\tC:\\Users\\Hassan\n");    

        //prints Quote Example"    C:\Users\Hassan"

    }

}

OUTPUT:

Student Details: Name: Hassan Age: 20 GPA: 3.75 Grade: A Passed: true Quote Example: "Work hard, dream big!" Backslash Example: C:\Users\Hassan


EXERCISE

We have covered enough topics to go for an exercise. In this exercise I want you to study this code, break it down, analyze it and identify the concepts used here. It would be even better if you write down your observations and make a mini report on it. You can extend the program to accept a second student and print both reports

import java.util.Scanner;


public class Exercise {

    public static void main(String[] args) {

       

        // --- Introduction + First Program ---

        System.out.println("Welcome to Java Fundamentals Exercise!");

        System.out.println("Let's practice everything we have learned so far.\n");


        // --- Variables & Data Types ---

        String name;

        int age;

        double gpa;

        char grade;

        boolean pass;


        // --- Input ---

        Scanner input = new Scanner(System.in);


        System.out.print("Enter your name: ");

        name = input.nextLine();


        System.out.print("Enter your age: ");

        age = input.nextInt();


        System.out.print("Enter your GPA: ");

        gpa = input.nextDouble();


        System.out.print("Enter your grade (A-F): ");

        grade = input.next().charAt(0);


        System.out.print("Did you pass the last exam? (true/false): ");

        pass = input.nextBoolean();


        System.out.println("\n--- Generating Report ---\n");


        // --- Operators ---

        int nextYearAge = age + 1; // arithmetic operator

        boolean isAdult = age >= 18; // relational operator

        boolean eligible = isAdult && pass; // logical operator


        // --- Output ---

        System.out.printf("Student Report for %s\n", name);

        System.out.printf("Age: %d (Next year: %d)\n", age, nextYearAge);

        System.out.printf("GPA: %.2f\n", gpa);

        System.out.printf("Grade: %c\n", grade);

        System.out.printf("Passed: %b\n", pass);


        System.out.println("\nExtra Info:");

        System.out.println("Are you an adult? " + isAdult);

        System.out.println("Eligible for next level? " + eligible);


        System.out.printf("\nQuote of the Day:\t\"Work hard, dream big!\"\n");

        System.out.printf("File Path Example:\tC:\\Users\\%s\n", name);


        input.close();

    }

}

OUTPUT:

Welcome to Java Fundamentals Exercise! Let's practice everything we have learned so far. Enter your name: Hassan Enter your age: 20 Enter your GPA: 3.75 Enter your grade (A-F): A Did you pass the last exam? (true/false): true --- Generating Report --- Student Report for Hassan Age: 20 (Next year: 21) GPA: 3.75 Grade: A Passed: true Extra Info: Are you an adult? true Eligible for next level? true Quote of the Day: "Work hard, dream big!" File Path Example: C:\Users\Hassan


MINI PROJECT: (Student Report Card)

Your project is to create a simple Java program that worls like a student report card. 

The program should ask the user for the student’s name and marks of three subjects (integers or decimals). It should calculate the total and the average of the marks. It should decide whether the student has passed or failed. The rule is: Pass if the average is 40 or more, and none of the marks is zero.

Finally, the program should print a formatted report using  printf  The report must include:

⦿ Student’s name
⦿ Each subject mark
⦿ Total marks
⦿ Average (2 decimal places)
⦿ Result: PASS or FAIL

While writing the program, make sure you use a  scanner  method for each datatype you use plus Arithmetic, relational, logical operators and   printf  with placeholders like   %s ,  %d ,  %.2f  


EXAMPLE OUTPUT:

--- Student Report Card --- Name: Tayyab Subject 1: 50 Subject 2: 60 Subject 3: 70 Total Marks: 180 Average: 60.00 Result: PASS


CLOSING

That’s it for Output in Java! We now know how to make our program talk back, whether it’s a single word, a full sentence, or a nicely formatted report.