How to output in java

Java Basic Input and Output

Don’t worry if you don’t understand it. We will discuss class , public , and static in later chapters.

Let’s take an example to output a line.

Java programming is interesting. 

Here, we have used the println() method to display the string.

Difference between println(), print() and printf()

  • print() — It prints string inside the quotes.
  • println() — It prints string inside the quotes similar like print() method. Then the cursor moves to the beginning of the next line.
  • printf() — It provides string formatting (similar to printf in C/C++ programming).

Example: print() and println()

1. println 2. println 1. print 2. print 

In the above example, we have shown the working of the print() and println() methods. To learn about the printf() method, visit Java printf().

Example: Printing Variables and Literals

When you run the program, the output will be:

Here, you can see that we have not used the quotation marks. It is because to display integers, variables and so on, we don’t use quotation marks.

Example: Print Concatenated Strings

Here, we have used the + operator to concatenate (join) the two strings: «I am » and «awesome.» .

System.out.println("Number Number input">Java Input 

Java provides different ways to get input from the user. However, in this tutorial, you will learn to get input from user using the object of Scanner class.

In order to use the object of Scanner, we need to import java.util.Scanner package.

 import java.util.Scanner; 

To learn more about importing packages in Java, visit Java Import Packages.

Then, we need to create an object of the Scanner class. We can use the object to take input from the user.

 // create an object of Scanner Scanner input = new Scanner(System.in); // take input from the user int number = input.nextInt(); 

Example: Get Integer Input From the User

import java.util.Scanner; class Input < public static void main(String[] args) < Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input.nextInt(); System.out.println("You entered " + number); // closing the scanner object input.close(); >> 
Enter an integer: 23 You entered 23 

In the above example, we have created an object named input of the Scanner class. We then call the nextInt() method of the Scanner class to get an integer input from the user.

Similarly, we can use nextLong() , nextFloat() , nextDouble() , and next() methods to get long , float , double , and string input respectively from the user.

Note: We have used the close() method to close the object. It is recommended to close the scanner object once the input is taken.

Example: Get float, double and String Input

Table of Contents

Источник

Java IO : Input-output in Java with Examples

Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams support all the types of objects, data-types, characters, files etc to fully execute the I/O operations.

Before exploring various input and output streams lets look at 3 standard or default streams that Java has to provide which are also most common in use:

  1. System.in: This is the standard input stream that is used to read characters from the keyboard or any other standard input device.
  2. System.out: This is the standard output stream that is used to produce the result of a program on an output device like the computer screen. Here is a list of the various print functions that we use to output statements:
  3. print(): This method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here.
    Syntax:
System.out.println(parameter);
Printing simple integer: x = 100 Formatted with precision: PI = 3.14 Formatted to specific width: n = 5.2000 Formatted to right margin: n = 2324435.2500
Enter characters, and '0' to quit. G e e k s f o r G e e k s 0
  • Depending on the type of operations, streams can be divided into two primary classes:
    1. Input Stream: These streams are used to read data that must be taken as an input from a source array or file or any peripheral device. For eg., FileInputStream, BufferedInputStream, ByteArrayInputStream etc.
    2. Output Stream: These streams are used to write data as outputs into an array or file or any output peripheral device. For eg., FileOutputStream, BufferedOutputStream, ByteArrayOutputStream etc.
  • Depending on the types of file, Streams can be divided into two primary classes which can be further divided into other classes as can be seen through the diagram below followed by the explanations.
    1. ByteStream: This is used to process data byte by byte (8 bits). Though it has many classes, the FileInputStream and the FileOutputStream are the most popular ones. The FileInputStream is used to read from the source and FileOutputStream is used to write to the destination. Here is the list of various ByteStream Classes:
      Stream class Description
      BufferedInputStream It is used for Buffered Input Stream.
      DataInputStream It contains method for reading java standard datatypes.
      FileInputStream This is used to reads from a file
      InputStream This is an abstract class that describes stream input.
      PrintStream This contains the most used print() and println() method
      BufferedOutputStream This is used for Buffered Output Stream.
      DataOutputStream This contains method for writing java standard data types.
      FileOutputStream This is used to write to a file.
      OutputStream This is an abstract class that describe stream output.

      Example:

Источник

Java Output / Print

You learned from the previous chapter that you can use the println() method to output values or print text in Java:

Example

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

You can add as many println() methods as you want. Note that it will add a new line for each method:

Example

System.out.println("Hello World!"); System.out.println("I am learning Java."); System.out.println("It is awesome!"); 

Double Quotes

When you are working with text, it must be wrapped inside double quotations marks "" .

If you forget the double quotes, an error occurs:

Example

System.out.println("This sentence will work!");
System.out.println(This sentence will produce an error);

The Print() Method

There is also a print() method, which is similar to println() .

The only difference is that it does not insert a new line at the end of the output:

Example

System.out.print("Hello World! "); System.out.print("I will print on the same line."); 

Note that we add an extra space (after "Hello World!" in the example above), for better readability.

In this tutorial, we will only use println() as it makes it easier to read the output of code.

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Читайте также:  Найти два наибольших числа python
Оцените статью