What is the system in the scanner class

Java input and Java output

In this post, we'll show you how Java input and the Java edition works exactly.

You want to understand in no time how you can use a Java program program to do something enter and again output can leave? Then ours is Video just right for you!

Java input and Java output - what does that mean?

The Java input and Java edition is also often used with I / O abbreviated. It stands I. For Input and O For output and is essential for the users of the program. Since, strictly speaking, these are so-called Currents (English streams) one often speaks of the input stream and output stream.

The input and output in Java is intended to convert the corresponding input into a language that the computer understands in order to process the data. If there is a Java edition is required, the output must first be converted back into a language that we can understand.

Converting from language to language is so laborious that it can be converted into Java preprogrammed Packages that save us this work. We can use simple instructions to request input from the user, which is then automatically converted.

Java scanner

Now let's take a closer look at how to even get one input by the user with a Java program can demand. As you already know, there is for it Packages, which you now have to include in your program. This is the only way we can use their instructions.

In order for the user to add something to the Enter Java program you therefore need a so-called Scanner. With this Java scanner can the input then processed by the program. So that you can Java scanner you must first import it as follows:

It is important to ensure that this instruction is always placed above the class. Otherwise the compiler will report an error.

With this one scanner in Java it is simply a Java class, which has certain methods in store for you. In particular the method , which expects an input stream from the keyboard. So that you with this scanner you have to work in the main method create an object of this scanner. You can do this with the following instruction:

We'll look at a more detailed example later. First of all, we would like to give you the three Input and Output currents in Java to introduce.

System.in, System.out, and System.err

There are three ways in Java Input and Output electricity to process: the input stream,, the output stream for normal results and , the output stream for error messages. While and are connected to the screen, is connected to the keyboard and waits for input.

Java input and output with string

Let's just take a look Java program at that as output our input outputs:

The first line of the program binds the class scanner from the package and is necessary to create an object of the scanner class. The decisive factor in this program is the string named , which saves the user's input so that it can be output again later.

A new scanner must be declared and generated so that the input can be picked up at all. The scanner is called simply in this example .

After we have a scanner we can call the method in the expression of the assignment statement for the input string. This will automatically stop the program and it will go to the Input serviced by the user. By using the Enter key, the user confirms his entry and the program saves the information in the variable.

At the end we output the characters that we have entered and are now stored in the variable on the screen.

Java input and output with integer

The Java class scanner also has other methods. For example, it also contains a method with which you can enter integers. Because until now your input has always been saved as a string. For example, to request an integer value as input, the method is specified by the Scanner class. Now you can also request an integer value from the user and save it as an integer. The above example then looks modified as follows:

However, the user can only enter an integer number without a comma. Otherwise the program issues an error message and is terminated.

Avoid error messages

There are several ways in which you can prevent the user from doing this Error messages trigger. Let's take a look at our last example with integer input. Here you could put the assignment statement that saves the value in the variable using into a if statement pack. The input is only assigned if it really is an integer value. The if-condition would be. This is also a method from the Scanner class that checks whether the input is an integer value or not. If this is not the case, we will notify the user via.

We would like to point out that the scanner Class even more Methods owns, but as you can see enough for the simple Java input and Java edition already two methods out. Now you can do programs with easier input and output in Java write.