Java Basic Input and Output
Java Basics – Part 13
Java Basics – Part 13
Forward: In this part of the series, I show a simple way of inputting data, and a simple way of outputting data with Java and the console.
By: Chrysanthus Date Published: 2 Sep 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
The main Method
This is what you have seen of the main Method:
class mainClass
{
public static void main(String[] args)
{
//statements
}
}
Any code outside the main method that has to be executed, must be called directly or indirectly from the block of the main method. When the program starts, statements of the main method are executed from top to bottom. Before the execution reaches the end of the main method, calls must have been made to code segments (methods) outside the block of the main method.
To execute a program, you normally type something like the following at the DOS (Command) prompt:
filename.exe
and then you press Enter. This is a place where you can type input for the program. The program name is filename.exe, where filename is the name you give. After typing the program name, you can type a space and then type strings that will be input to the program. The strings are separated by spaces (not by commas). So each of the strings should not have a space, since spaces are used to separate strings. The strings should not be in quotes. For this to work, you need to put the following parameters in the parentheses of the main method:
String[] args
This is what I have been doing but the variable, args, has not been used so far in the main method in the series. This expression is a parameter. It begins with the object type, String[] followed by a variable name as you would expect for a parameter in the parentheses of any method. Now, String[] is an array object type of strings.
You have one parameter in the parentheses of the main method, which is what I have written above and I hope you have noticed it in the main method above.
Consider the following:
filename.exe one two three
This is the kind of thing you will type at the DOS prompt in order to try the code below. filename.exe represents the name you should give your executable file. You can give any name you want for your application. After this, for the example below, there is a space and then three strings. The strings are separated by spaces. Quotation makes are not used. Each string should not have a space. Each string is an input datum to the Java program. Read and try the program below using the following line at the DOS prompt.
Inputting one two three
The compiled Java program does not end with “.exe”, similar to programs of other languages. That is why, in the line, you have the executable file name just, Inputting, and then the strings, one, two, and three. This is the program:
class Inputting
{
public static void main(String[] args)
{
System.out.println(args[1]);
}
}
The output should be “two” without the quotes. Remember, array counting begins from zero. The parameter of the mains method is “String[] args”, which in this case received the array of strings (“one”, “two”, “three”). In the block of the main method you use, the array variable, args, to access the elements (values) of the array. The name of the array is, args.
In hope you can now input data into a Java program using the keyboard for the console.
Note: You do not necessarily have to name your Java program, Inputting. You can give it any name you want.
Outputting Data
You have already been outputting data using for the console. One way to do this is just to use the expression, System.out.println(). This expression prints output to one line. I will give you a more formal explanation on how this object is used later.
I did not explain the other items (public, static, void) for the main method. I will explain them later.
Well, let us stop here and continue in the next part of the series.
Chrys