printing out a 2 text files in java -


here code printing out 2 text files out put nor right keeps printing first line on , on example text in file : 1 2 3 4 1 2 3

when call function output : 1 1 1 1 1 1 1

    public static void printuser() {     bufferedreader br = null;     bufferedreader br1 = null;     try {         br = new bufferedreader(new filereader("info.txt"));         br1 = new bufferedreader(new filereader("info ai.txt"));     } catch (filenotfoundexception e) {         // todo auto-generated catch block         e.printstacktrace();     }      string line = null;      string line1 = null;       try {         while((line = br.readline())!= null) {              while((line1 = br1.readline())!= null){            system.out.println(line+"  ===  "+line1);             }          }     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     } } 

it's because of double while loop, each round of first while, second 1 restarts. unless want recursively read entire br1 each line of br, change inner while statement if statement:

public static void printuser() { bufferedreader br = null; bufferedreader br1 = null; try {     br = new bufferedreader(new filereader("info.txt"));     br1 = new bufferedreader(new filereader("info ai.txt")); } catch (filenotfoundexception e) {     // todo auto-generated catch block     e.printstacktrace(); }  string line = null;  string line1 = null;   try {     while((line = br.readline())!= null) {       line1 = br1.readline()         if(line1 != null){        system.out.println(line+"  ===  "+line1);         }      }  } catch (ioexception e) {     // todo auto-generated catch block     e.printstacktrace();  } } 

hope helps


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -