Read File in Java, output the first comma delimited String -


i want extract first string in file using delimiter ",". why code generate number of lines greater one?

public static void main(string[] args) {     bufferedreader in = null;     try {         in = new bufferedreader(new filereader("irisafter.txt"));         string read = null;         while ((read = in.readline()) != null) {             read = in.readline();             string[] splited = read.split(",");             (int =0; i<splited.length;i++) {                 system.out.println(splited[0]);             }          }     } catch (ioexception e) {             system.out.println("there problem: " + e);             e.printstacktrace();     } {         try {             in.close();         } catch (exception e) { e.printstacktrace(); }     } } 

you printing inside loop. that's why printing multiple times (if that's you're asking).

string[] splited = read.split(",");     system.out.println(splited[0]); 

will do

edit: abishek mentioned, don't read = in.readline(); again inside while loop since doing skipping line.

while ((read = in.readline()) != null) {    string[] splited = read.split(",");    system.out.println(splited[0]); } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -