joy
joy

Reputation: 741

the variable only have it value inside while loop?

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?

Upvotes: 0

Views: 2163

Answers (3)

Shamis Shukoor
Shamis Shukoor

Reputation: 2515

You have initialised h inside the while loop You cannot access it outside the loop

while((string = bufferedReader.readLine()) != null) {                 
        myString =stringBuilder.append(string);
        String h=myString.toString();
       }
       System.out.println(h);  

You can use it this way

String h;
 while((string = bufferedReader.readLine()) != null) {                 
           stringBuilder.append(string);
           }
       h=stringBuilder.toString();
       System.out.println(h);  

Upvotes: 1

Jeremy Morehouse
Jeremy Morehouse

Reputation: 801

You're declaring your h variable inside your while loop. Therefore, every iteration of the while loop will create a new h variable scopped to that iteration.

You won't be able to access the h variable outside the while loop unless it is declared outside the while loop.

Keeping in mind, that the way you have it written though, the h variable is being overwritten every iteration as well, so you're end result would be whatever was put in h last.

In the long run, this code could be cleaned up a lot as well, and you wouldn't even need h

static void parse(String fileName) throws IOException{
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    StringBuilder sb = new StringBuilder();
    while((String line = br.readLine()) != null) {                 
        sb.append(line);
    }

    System.out.println(sb.toString());  
}

You'll have to check my syntax, but that should be cleaner.

Upvotes: 0

A B
A B

Reputation: 1936

no need to create instance variable. just put h outside the loop:-

static void parse(String fileName) throws IOException{
FileReader fileReader=new FileReader((fileName));
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuilder=new StringBuilder();
String string,h=null;
StringBuilder myString = null;
while((string = bufferedReader.readLine()) != null) {                
    myString =stringBuilder.append(string);
    h=myString.toString();
    }
   System.out.println(h);
}

Upvotes: 2

Related Questions