+92 332 4229 857 99ProjectIdeas@Gmail.com

How To Take Input/Output In Java



How to take input/output in Java:

The following code shows you how to take input/output on console and also in SWING components.



Code

Output in Java console based:

System.out.print("Pakistan Won"); 
System.out.println("Pakistan Won");



Input in Java from console:  

import java.io.*;

Way One:


try {

InputStreamReader isr = new InputStreamReader(System.in);    
BufferedReader br = new BufferedReader(isr);
System.out.println("Input Is : " + br.readLine());
      
} catch (Exception e) {}
 
Way Second:

try {
      
FileReader fr = new FileReader(FileDescriptor.in);
BufferedReader br  = new BufferedReader(fr);
System.out.println("You Input This : " + br.readLine());
      
} catch (Exception e) {}

Input/output Through Swing Components:
 
import
javax.swing.*; //For swing components (Java Own components)
import java.awt.*; //For underlying Operating system components


/* this program takes the input (number) through GUI and display its square on GUI. */

// takes input through GUI
String input = JOptionPane.showInputDialog("Enter the number");//it returns string

int number = Integer.parseInt(input); //converting string to integer

int square = number * number;                         

//Display square of number on GUI
 

JOptionPane.showMessageDialog(null, "Square : " + square); 

0 comments: