Hello kareem, By my humble opinion, it was a support of good programming style , e.g. declaring variables before usage. Unfortunately, java doesn't initiate variables automatically. So we made it equal to empty string ("") ; I would work if you type at line 14) String name =dataIn.readLine(); also but doing so, you make your code difficult to read by others. In this example, we created empty string variable and than replaced it by other variable using readLine.
Sang, sorry for answering instead of you. On Thursday, December 25, 2014 8:04:04 PM UTC+2, kareem ashiru wrote: > > Hi Sang, > > I am just at the elementary level in the Java tutorials( first course), I > have some question or a little more insight into the logic. In the sample > code for bufferedReader shown below: > > 1 import java.io.BufferedReader; > 2 import java.io.InputStreamReader; > 3 import java.io.IOException; > 45 > public class GetInputFromKeyboard { > 67 > public static void main( String[] args ){ > 8 BufferedReader dataIn = new BufferedReader( > 9 new InputStreamReader( System.in) ); > 10 > 11 String name = ""; > 12 System.out.print("Please Enter Your Name:"); > 13 try{ > 14 name = dataIn.readLine(); > 15 }catch( IOException e ){ > 16 System.out.println("Error!"); > 17 } > 18 System.out.println("Hello " + name +"!"); > 19 } > 20 } > > > we created a new variable to hold the new instance of BufferedReader class > :" dataIn" > I am not sure I understand why we need to create a new string variable > again : "name" and then equate "name" to "dataIn" > > My second question is, I think the readLine() method does the same thing > as code number 8 and 9, if not whats the difference. > > regards > -- You received this message because you are subscribed to the Google Groups "JPassion.com: Java Programming" group. To unsubscribe from this group and stop receiving emails from it, send an email to jpassion_java+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/jpassion_java. For more options, visit https://groups.google.com/d/optout.