Java Input-output (I/O operations)
We can only print using System.out but can use different print varieties in it:
1. print
The cursor remains on the same line
System.out.print(" GFG " + x);
2. println
Cursor Moves to a new line
System.out.println(" GFG " + x);
3. printf
The cursor remains on the same line
System.out.printf(" GFG %d",x);
Formatted Print
System.out.printf("%7.5f", Math.PI);
Explanation of the above statement:
7 is the Field width and .5 is the precision fo the floating number printed . So, answer will be 3.14159
Taking Input in Java
1. Parsing Command-line arguments
We can take input using the Command line simp
Java
// Java Program to take Input // from command line arguments class GFG { public static void main(String args[]) { for ( int i = 0 ; i < args.length; i++) System.out.println(args[i]); } } |
javac GFG.java
java GFG This is just to check 2
Output:
This
is
just
to
Check
2
2. Buffer Reader Class
InputStreamReader() is a function that converts the input stream of bytes into a stream of characters so that it can be read as BufferedReader expects a stream of characters.
Below is the implementation of the above method:
Java
// Java Program for taking user // input using BufferedReader Class import java.io.*; class GFG { // Main Method public static void main(String[] args) throws IOException { // Creating BufferedReader Object // InputStreamReader converts bytes to // stream of character BufferedReader bfn = new BufferedReader( new InputStreamReader(System.in)); // String reading internally String str = bfn.readLine(); // Integer reading internally int it = Integer.parseInt(bfn.readLine()); // Printing String System.out.println( "Entered String : " + str); // Printing Integer System.out.println( "Entered Integer : " + it); } } |
Output:
ABC
11
Entered String : ABC
Entered Integer : 11
3. Scanner Class
Syntax:
Scanner scn = new Scanner(System.in);
Below is the implementation of the above method:
Java
// Java Program to take input using // Scanner Class in Java import java.io.*; import java.util.Scanner; class GFG { public static void main(String[] args) { // creating the instance of class Scanner Scanner obj = new Scanner(System.in); String name; int rollno; float marks; // taking string input System.out.println( "Enter your name" ); name = obj.nextLine(); // taking float input System.out.println( "Enter your marks" ); marks = obj.nextFloat(); // printing the output System.out.println( "Name :" + name); System.out.println( "Marks :" + marks); } } |
Output:
ABC
65
Name :ABC
Marks :65
Java Cheat Sheet
Java is a programming language and platform that has been widely used since its development by James Gosling in 1982. It follows the Object-oriented Programming concept and can run programs written in any programming language. Java is a high-level, object-oriented, secure, robust, platform-independent, multithreaded, and portable programming language All those words are collectively called Java Buzzwords. It is commonly used for programming web-based, window, enterprise, and mobile applications. This Java Cheat Sheet article has been written by experts in Java and based on the experience of students who have recently undergone Java interviews.
This Core Java Cheat Sheet has been designed by Java experts, based on the experience of students who have recently undergone Java interviews. Whether you are a beginner or an experienced Java developer, this Java Cheat Sheet is a valuable resource for quickly accessing essential syntax, concepts, and best practices related to Java Programming.
Contact Us