Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.


Learning Java: How to Take Input and Display Output

Java Programming: Take input and display Output
As we know, computer programs consist of three main components, namely: input, process, and output.
  • Input: the value we input into the program
  • Process: step by step that is done to manage input becomes something useful
  • Output: processing results
All programming languages ​​have provided functions for input and output.
Java itself has provided three classes for taking input:
  1. Class Scanner;
  2. BufferReader class;
  3. and Class Console.
The three classes are to take input in a text-based program (console) . While for the GUI, use other classes such as JOptionPane and inputbox on the form.
As for output, Java provides the functions print()println(), and format().

Take Input with a Class Scanner

Scanner is a class that provides functions to take input from the keyboard.
So that we can use the Scanner, we need to import it into the code:
import java.util.Scanner;
For further details…

Let's Practice

Now is the 2nd meeting, so make a new package called meetings in source packages .
Right-click on souce packages , then select new package :
Create a new package
Then fill in the package name with the meeting , then click Finish :
Fill in the package name
After that, continue by creating a new class (java class) in the meeting package2 :
Create a new class
Give name: Employee Data . Then click Finish :
After that, please follow the following code:
package pertemuan2;

// mengimpor Scanner ke program
import java.util.Scanner;

public class DataKaryawan {

    public static void main(String[] args) {
        // deklarasi variabel
        String nama, alamat;
        int usia, gaji;

        // membuat scanner baru
        Scanner keyboard = new Scanner(;

        // Tampilkan output ke user
        System.out.println("### Pendataan Karyawan PT. Petani Kode ###");
        System.out.print("Nama karyawan: ");
        // menggunakan scanner dan menyimpan apa yang diketik di variabel nama
        nama = keyboard.nextLine();
        // Tampilkan outpu lagi
        System.out.print("Alamat: ");
        // menggunakan scanner lagi
        alamat = keyboard.nextLine();

        System.out.print("Usia: ");
        usia = keyboard.nextInt();

        System.out.print("Gaji: ");
        gaji = keyboard.nextInt();

        // Menampilkan apa yang sudah simpan di variabel
        System.out.println("Nama Karyawan: " + nama);
        System.out.println("Alamat: " + alamat);
        System.out.println("Usia: " + usia + " tahun");
        System.out.println("Gaji: Rp " + gaji);

It should be noted, use the function to retrieve data depending on the data type used.
For example, the data type is a String , then the function or method used is nextLine().
Likewise with other data types, Integers use nextInt()Doubleuses nextDouble(), etc.
After completing the program, please run. Right-click then select Run File or press the [Shift]button [F6].
Program output with a scanner

Take Input with the BufferReader Class

The class is BufferReaderactually not only for taking input from the keyboard.
This class can also be used to read input from files and networks.
This class is located in the package
Please import to use the class BufferReader.

Let's try ...

Please create a new class named ContohBufferReader, then fill in the following code.
package pertemuan2;


public class ContohBufferReader {

    public static void main(String[] args) throws IOException {

        String nama;

        // Membuat objek inputstream
        InputStreamReader isr = new InputStreamReader(;

        // membuat objek bufferreader
        BufferedReader br = new BufferedReader(isr);

        // Mengisi varibel nama dengan Bufferreader
        System.out.print("Inputkan nama: ");
        nama = br.readLine();

        // tampilkan output isi variabel nama
        System.out.println("Nama kamu adalah " + nama);


It turns out that class BufferReadercannot work alone. He also needs friends, namely: class InputStreamReaderand class IOException.
Now let's try running the program:
Running a Java program
The difference BufferReaderwith is Scannerseen from the function or method used.
Scanneruse next(), while BufferReaderusing readLine().
Then for integer data types, BufferReaderuse read()only functions We can see this in hint autocomplete .
hint buffer
Tip : Press CtrlSpasiwhen writing code to display hint autocomplete .

Take Input with the Class Console

Class is Consolealmost the same as BufferReaderHe also uses a function readLine()to take input.
This class can only be used in console environments, such as Terminal and CMD.
Class Consolecannot be used directly in Netbeans.
Therefore, we must compile manually.
To use this class, we need to import it first.

Let's try ...

Make a new file named InputConsole.javawith the contents as follows:

public class InputConsole {
    public static void main(String[] args) {

        String nama;
        int usia;

        // membuat objek console
        Console con = System.console();

        // mengisi variabel nama dan usia dengan console
        System.out.print("Inputkan nama: ");
        nama = con.readLine();
        System.out.print("Inputkan usia: ");
        usia = Integer.parseInt(con.readLine());

        // mengampilkan isi variabel nama dan usia
        System.out.println("Nama kamu adalah: " + nama);
        System.out.println("Saat ini berusia " + usia + " tahun");
Note : in the code above, we use functions Integer.parseInt(con.readLine())for integer data types. That is, we change the String data type to Integer.
Because Consoleit has no return value in the form of an integer for the function read().
Continue ...
After that, compile through Terminal or CMD. Open the terminal, then go to the directory where the code is stored.
Type the command: javac InputConsole.javato compile.
After that, type the command java InputConsoleto run it:
Java input console

Display Output

We already know several ways to take input from the keyboard for text-based programs.
Now what about the output?
There are several functions that have been provided by Java:
  1. Function System.out.print()
  2. Function System.out.println()
  3. Function System.out.format()
What are the differences in these functions?
Let's discuss ...

print()Vs functionprintln()

The function print()and println()both are used to display text.
Then what's the difference?
The function print()will display text as is. Whereas println()will display text with added new line.
Let's try in the code:
package eksperimen;

public class PrintVsPrinln {

    public static void main(String[] args) {

        System.out.print("ini teks yang dicetak dengan print()");
        System.out.println("sedangkan ini teks yang dicetak dengan println()");
        System.out.print("pake print() lagi");


Now look at the output:
Difference in print function with println

Combining Strings

When using functions print()or println(), sometimes we need to take text from variables and combine them with other text.
For example like this:
We have variables namaDepanand namaBelakang:
String namaDepan = "Petani";
String namaBelakang = "Kode";
Then we want to display it with a function print(), so we only need to enter it there.
The code will produce: PetaniKode
Actually we don't need to use two functions print(), because we can combine them with operators +.
System.out.print(namaDepan + namaBelakang);
To have a space, just add a space:
System.out.print(namaDepan + " " + namaBelakang);

String format

Whereas for combining more complex strings, we can use functions format().
package eksperimen;

public class FormatString {

    public static void main(String[] args) {

        String namaDepan = "Petani";
        String namaBelakang = "Kode";

        System.out.format("Nama saya %s %s %n", namaDepan, namaBelakang);


Note : there we use symbols %sto take the value of the variable next to it. %smeaning string.
Besides %s, there are also other symbols:
  • %d for decimals or numbers;
  • %f for fractions;
  • %nfor new lines, you can also use it \n;
  • and many more, check the java documentation .
String format
The code above will produce: Nama saya Petani Kode
0 Komentar untuk "Learning Java: How to Take Input and Display Output"

Silahkan berkomentar sesuai artikel

Template By Kunci Dunia
Back To Top