the variable only have it value inside while loop?
Posted
by
user1834467
on Stack Overflow
See other posts from Stack Overflow
or by user1834467
Published on 2012-11-19T04:50:52Z
Indexed on
2012/11/19
5:00 UTC
Read the original article
Hit count: 122
static void parse(String fileName) throws IOException{
FileReader fileReader=new FileReader((fileName));
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuilder=new StringBuilder();
String string;
StringBuilder myString = null;
while((string = bufferedReader.readLine()) != null) {
myString =stringBuilder.append(string);
String h=myString.toString();
**System.out.println(h);**
}
}
static void parse(String fileName) throws IOException{
FileReader fileReader=new FileReader((fileName));
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuilder=new StringBuilder();
String string;
StringBuilder myString = null;
while((string = bufferedReader.readLine()) != null) {
myString =stringBuilder.append(string);
String h=myString.toString();
}
**System.out.println(h);**
}
when I try the second part of the code, it print out nothing. how can I get the whole h String outside of the while loop? Is it I have to declare the variable h as instance variable instead of local variable?
© Stack Overflow or respective owner