Question:
A QUESTION RELATED TO JAVA??? PLSSS ITS IMPORTANT?
Abhishek
2009-12-06 03:24:31 UTC
A program--
import java.io.*;
class DisplayWeekDay1
{
public static void main(String args[]) throws IOException
{
int weekDay;
String number;
BufferedReader in= new BufferedReader(new InputStreamReader(System.in)) ;
System.out.print("Enter the day of week(1 to 7):");
number= in.readLine();
weekDay= Integer.parseInt(number);
switch(weekDay)
{
case 1: System.out.println("Sunday");
break;
case 2: System.out.println("Monday");
break;
case 3: System.out.println("tuesday");
break;
case 4: System.out.println("Wednesday");
break;
case 5: System.out.println("Thrursday");
break;
case 6: System.out.println("Friday");
break;
case 7: System.out.println("Saturday");
break;
default:
System.out.println("valid values are from 1 to 7");
}
}
}

QUESTIONS:
1. can you please explain me this line in simple text to text description
and also tell me what does it do for this program..

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

THANKSSSSSSSS
WILL BE HIGHLY OBLIGED :)
Five answers:
xgme x
2009-12-06 03:51:57 UTC
This line is for taking input from keyboard. I think you know before

Scanner scan = new Scanner(System.in);

That line is same with this line.

When you enter something on the commond line, Scanner object (in your case BufferedReader) can take the line. And you read the line by "in.readLine()" which returns you a String object.
SpeciousParagon
2009-12-06 03:37:32 UTC
The line creates a new Buffered Reader object that uses the system.in file as it's source. This means that it is reading input from the command line. The BufferedReader class is used as a wrapper class to the InputStreamReader to buffer the reading operations.

Basically, it is setting up a method to read values in from the command line, entered by the user. In this case, they get a number and print a message about what weekday it corresponds to.
Router ID
2009-12-06 03:43:33 UTC
Here is the correct code Abhishek!



import java.io.*;

class DisplayWeekDay1

{

public static void main(String args[]) throws IOException

{

int weekDay;

String number;

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(isr) ;

System.out.print("Enter the day of week(1 to 7):");

number= in.readLine();

weekDay= Integer.parseInt(number);

switch(weekDay)

{

case 1: System.out.println("Sunday");

break;

case 2: System.out.println("Monday");

break;

case 3: System.out.println("Tuesday");

break;

case 4: System.out.println("Wednesday");

break;

case 5: System.out.println("Thursday");

break;

case 6: System.out.println("Friday");

break;

case 7: System.out.println("Saturday");

break;

default:System.out.println("valid values are from 1 to 7!");

break;

}

}

}



for your question:

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





InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(isr) ;



Both are one and the same.

what you have done is you have passed the instance. or you can also pass the object.

The thing is that when you get an input from the console screen

you go for java.io package;

then to get the user input you use the class named InputStreamReader from the java.io package.

and System.in helps you to get the input from the keyboard.



InputStreamReader isr = new InputStreamReader(System.in);



and after you get the value you store the value in the buffer and use it. for that you have to use the value

BufferedReader in = new BufferedReader(isr) ;





check this link for more info: In this link check the class summary table.

http://java.sun.com/j2se/1.4.2/docs/api/java/io/package-summary.html
2009-12-06 03:36:55 UTC
Buffering is same as taking input using ReadLine but without buffering, each calling of ReadLine causes bytes to be read from the text and then to convert into char array, finally to return. This is time taking whereas the use of Buffer makes stream input.

Regards,

Zaidi
?
2009-12-06 04:26:03 UTC
Your mother!

Read "Thinking in Java Second Edition - Bruce Eckel"

it obviously


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...