Minecraft Blogs

Java Programming Tutorial #4 - Data Input/Output

  • 3
  • 1
avatar SourC00kie
Retired Moderator
Level 45 : Master Blob
197
Click here to see Java Programming Tutorial #3 - Variables

Click here to see Java Programming Tutorial #5 - Strings

Data Input/Output (IO)


Introduction to Data Types

As introduced previously, when declaring a variable, besides its name, you must provide the type of information that the variable will hold. The role of this type is to tell the compiler how much memory will be needed to store the value(s) of that variable. Since your program is not the only one used in the computer, memory has to be effectively managed.

The type of value that a variable will hold is called a data type. As you ay imagine, different variables can be meant to hold different types of values. Each data type uses a Java keyword to be characterized. As a modern language, Java provides even more support for its traditional data types. To do this, a class was created for each data type. This allows you to get more information from a variable declared with the particular data type you want to use.

Data Input

Retrieving a value in Java is not particularly busy. As seen in our introduction, to do anything, you have to go through a class. To retrieve a value, you have to go through different classes.

When you type a value in a program, to retrieve it, you can the in object of the System package:

System.in

After getting that value, you must first store it somewhere. One of the classes you can use is called Scanner. Before using the Scanner class, you must import the java.util.Scanner package into your program. This would be done by writing the following in the top section of the file:

import java.util.Scanner;

To use the Scanner class to retrieve a value, use the following formula:

Scanner VariableName = new Scanner(System.in);

The only think we need to mention at this time is that, after the Scanner class, you must give a variable name. An example would be:

Scanner scnr = new Scanner(System.in);

The other details, such as why System.in is written in the parentheses after new Scanner, will be explained in future lessons.

After declaring a Scanner class, its variable is ready to receive the value. The value depends on a type. We will see in the next lessons. When getting a value, the Scanner class must be able to convert it to its appropriate type. To support this, the Scanner class is equipped with a mechanism (actually called a method) for each type of value. To retrieve a value, you will write the name of the Scanner variable, followed by a period, followed by the mechanism as we will indicate, then assign it to the variable whose value you want to retrieve. The formula will be:

VariableName = ScannerVariable.Mechanism();

Notice the parentheses and the semi-colon.

Data Output

To display a value, you can write it in the parentheses of System.out.print(). If the value is a character, include it between single-quotes. Here is an example:

public class Exercise {
public static void main(String[] args) {
System.out.print('Q');
}
}

This would produce:

Q

If the value is a word or a sentence, include it between double-quotes. Here is an example:

public class Exercise {
public static void main(String[] args) {
System.out.print("Derping around");
}
}

This would produce:

Derping around

If the value is a (constant) number, simply type that number in the parentheses of System.out.print() or System.out.println(). Here is an example:

public class Exercise {
public static void main(String[] args) {
System.out.print(248);
}
}

This would produce:

248

If the value is stored in a variable, you can write the name of that variable in the parentheses of System.out.print(). We will see various examples in the next sections.

Instead of a constant value in the parentheses of System.out.print() or System.out.println(), you may want to add other items, such as a sentence that is made of words and values that would come from variables. Fortunately, you have many options. If you want to exercise some control as to how a value is formatted, instead of using System.out.print() or System.out.println(), use System.out.printf(). This allows you to format how data should appear when displayed to the user. Data types are divided in categories: characters, integers, floating-point numbers, and strings. To display a particular type, you type the percent operator o %o , followed by the category of data. The basic formula to follow is:

System.out.printf("blah blah blah %Character blah blah", variable);

Any blah represents anything word or sentence you want to include in the display. At some point, you will write % followed by a character. We will see the examples in the appropriate section. The first section ends with a closing double-quote followed by a comma. After the comma, you must include the name of the variable whose value will be displayed in the Character placeholder.

Instead of one value, you can create as many placeholders as you want. The formula to follow is:

System.out.printf("blah %Char1 blah %Char1 blah %Char1 blah", var1, var2, var3);

Once again, you will replace each blah with whatever you want to add as part of your sentence. Each Char is a placeholder for a character as we will see in various examples. After the comma, you must include a variable name (or a value) for each Char placeholder, in the order they appear in the double-quotes, no more, no less.
Tags
Planet Minecraft Logo

Website

© 2010 - 2020
www.planetminecraft.com

Welcome