Search code examples
androidbufferedreaderbufferedinputstream

Loop Statement For BufferedReader Is Not Working


I seem to be overlooking something quite obvious with my loop, but in its present state it is not posting the content of the eula text file to the alert dialog. Anyone see anything I am overlooking? There are 21 lines in the text file. Thanks!

Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.eula);
BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));

    String key1 = preference.getKey();
    //if the user click on the Legal Notices preference, display the license
    if (key1.equalsIgnoreCase("prefEULA")){
    String eulaContent = null;
    try {

        while ((reader.readLine()) != null)
            for(int i=0;i<21;i++){
        {
             eulaContent = reader.readLine();

    }

    }
    }   catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Builder eulaDialog = new AlertDialog.Builder(this);
    eulaDialog.setTitle("End-User Licence Agreement");
    if (eulaContent!=null){
    eulaDialog.setMessage(eulaContent);
    eulaDialog.show();                

    }            

    }

Solution

  • I really wanted to use the loop I had already written, and was able to once I got some advice that put me on the right track. I ended up going with the code below, which works awesomely! A few adjustments to the loop, which included adding a null String object for the buffered data to be appended to. Also, the ...+ "\n"; bit adds the new lines back into the buffered data.

     Resources res = getResources();  
     InputStream in_s = res.openRawResource(R.raw.eula);  
     BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));  
    
            String key = preference.getKey();  
            //if the user click on the Legal Notices preference, display the license  
            if (key.equalsIgnoreCase("prefEULA")){  
            String eulaContent = "";  
            try {  
    
                String line = null;  
                while ((line = reader.readLine()) != null) {  
    
                eulaContent += line + "\n";
            }  
    
    
            }   catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            Builder eulaDialog = new AlertDialog.Builder(this);  
            eulaDialog.setTitle("End User License Agreement");  
            if (eulaContent!=null){  
            eulaDialog.setMessage(eulaContent);  
            eulaDialog.show();                  
    
            }              
    
            }