Logical program -1 -> read the number from standard input using scanner and another without scanner class
5 June 2025 at 11:11
Program-1:
package logicProgramme;
import java.util.Scanner;
public class Scanner_Class_ReadIntegerValue {
// read the number from standard input
public static void main(String[] args) {
int num;
// print the message to ask the user to enter number
System.out.println("Enter the integer: ");
// Scanner class import from utility package.
//create Scanner object.
Scanner s = new Scanner(System.in);
//create scanner object s ,to take input from keyboard (System.in)
//read the input form the user and store it in num.
num = s.nextInt();
System.out.println("Entered interger is: "+ num);
// closes the scanner to free up the resourse.
s.close();
}
}
Program-2
package logicProgramme;
public class ReadInetegerWithoutScanner {
// read the number from without scanner class
public static void main(String[] args) {
if (args.length >0) // check the user entered input or no.
{
int num =Integer.parseInt(args[0]);
// Integer.parseInt(args[0]); , Integer.parseInt -> it is convert string to integer, args[0] , index of array 0 the postion take the input
// parse mean read and convert , int means integer - string to int convert
// example:-i entered arg tab 123 and it String args output 45
// in eclipse right click file -> runs as -> run configuration -> argument tab give nput and apply and run.
// simple Integer ,java class in java.lang package parseInt
//Integer -> class , parseInt method,
System.out.println("Entered integer is: "+num);
}
else
{
System.out.println("No integer entered");
}
}
}
/*int to String convert
* String.valueof();
*
*
* /
*/
Example:3-
package logicProgramme;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class UsingBufferedReaderClass {
// how to use BufferReader to read a line of input from the user and print it to the console.
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input =reader.readLine();
System.out.println(input);
}
}
/*
* 1.Bufferedreader -> Bufferedreader in java class read the text from the charater(not byte ) input stream.
*
* system.in read the byte from the keyboard not characters.(system.in can not directely read string or line -only raw bytes)
*
*2.InputStreamReader -> it is a bridge b/w System.in (bytes) and java character
* it is convert to byte to character, so help full for read text from keyboard.
*
* 3.readline() it method of Bufferereader, it ead one full line of text and it is return a string
*/
/*BufferedReader it java class read the text chracter not bytes, system.in ,
* it read input from keyboard like byte not a character, Inputstreamreader is it help convert byte to character ,and read text in keyboard and , it bridge b/w (system.in) byte and character, and readLine() methdod of Bufferereader class,it reade the text line
*
*
*/