:::::JAVA PROGRAM ::::

Program to find greatest of three numbers.
import java.io.*;
class greatest
{
public static void main(String args[])throws IOException
{
int a,b,c;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter 3 numbers");
a=Integer.parseInt(in.readLine());
b=Integer.parseInt(in.readLine());
c=Integer.parseInt(in.readLine());
if (a>b && a>c)
System.out.println("greatest is"+" "+a);
else if(b>a && b>c)
System.out.println("greatest is"+" "+b);
else
System.out.println("greatest is"+" "+c);
}
}
Please can anyone explain step by step what is happening in the program and what does this line means` BufferedReader in=new BufferedReader(new InputStreamReader(System.in));` in above program and detail regarding that and what are tokenizer and were is it used?

Replies

  • Neeraj Sharma
    Neeraj Sharma
    import java.io.*;

    Imports the I/O (input/output) package into the program that lets the programmer use the I/O functions in his program.

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

    BufferedReader is used for buffering to the readers which make I/O quite fast. So we are instantiating BufferedReader and passing InputStreamReader as the argument for buffering. InputStreamReader reads input from a source. System.in provides the source here for InputStreamReader. System.in is used to read from the keyboard. So whatever we type via keyboard is taken by InputStreamReader which in turn is passed to BufferedReader.

    int a,b,c;

    Basic variables declaration of integer type

    a=Integer.parseInt(in.readLine());
    b=Integer.parseInt(in.readLine());
    c=Integer.parseInt(in.readLine());

    as readLine() reads the input in the form of Strings, Intege.parseInt() is helpful in converting that text into integers as we want to compare them as numbers and not texts

    if (a>b && a>c)
    System.out.println("greatest is"+" "+a);

    System.out.println() is used to print a line on the console. if value of a is greater than both b and c then a is greatest and hence it is printed.

    else if(b>a && b>c)
    System.out.println("greatest is"+" "+b);
    else
    System.out.println("greatest is"+" "+c);

    Same is the case here where b or c are printed according to the very simple logic of comparing the values.

    I am not too sure about Tokenizer, may be somebody else can help
  • monujatt
    monujatt
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); represents the input given by you through any input device which is represented by system.in

    Integer.parseInt is for converting to integer type

    Next is the simple logic to find the greatest number... 😀
  • sulochana anand
    sulochana anand
    as we know that taking user entry is not simple in java.so to take entry from user v import java.io(input/output)package.this package contains different methods of input output.as above program is to find gretest number among three.as in first lne ioexception is thrown because if any error will occurs during then it will be thrown.as u used integer data type.for eg-if user enters different data type instead of integer this program will throw exception immediately.BufferedReader in=new BufferedReader(new InputStreamReader(System.in));in this line BufferReader is a class which is used to read character from console.in is a object created using new BufferReader constructor.’System.in ‘is line buffered, by default.it means that no input is passed to program until we press enter i.there are three integers a,b,c;which will hold value passed by user.
    readLine() is a method which read and display what user passed. a=Integer.parseInt(in.readLine());in this line a is variable ‘Integer.parseInt’ is method which convert character into integer as BufferReader class is for character data we have to make system understand that what data type v are using.’in’ an object whis is used to read valuses for a,b and c valriables. if (a>b && a>c) is used to compare values of a,b,c .and to print System.out.print is used.
  • simplycoder
    simplycoder
    Previously whenever user inputs were taken, they were in form of strings.
    then programmers used to parse the string to convenient format say int,float,double...
    This was long time ago( before jdk 1.5)
    But today we use jdk version higher than or equal to 1.5 so you can use Scanner something like :
    Scanner read=new Scanner(System.in);
    and then
    int n=read.nextInt();
    google for more on this.
    also if there are only three variables, your algorithm works fine but then scale it up to 4,5,..100..10000. size will eventually fail your 'if' algorithm.

    Instead, take a variable max and assign max to the first number encounterd.
    then compare max with second if second number is greater than max, assign max to second number do the same thing for third and so on.

    In the end you would be left only with the maximum number which you can print.

You are reading an archived discussion.

Related Posts

what is in home communication,................ when i searched i got that in home communication is the communications between different electronics devices in home,. but i didn't get the idea. what...
my name is saif siddiqui and i will complete my btech in 2013 from a low grade engineering college, college is not going to arrange any campus so its my...
I am crazy about engineering, fun loving guy. Overall I am a genuine Mechie(no offense to any other branch).
can anyone tell me that which website i can download mechanical engineering books in pdf format.........
Dear All, I have Lighting Transformer for all Lighting Load(e.g Tube Light). If I reduce the O/p voltage by changing taps(from 230V to 200V), what will be the effect on...