java - Reading file and store in vector -


i'm reading file:

name1 wordx wordy passw1
name2 wordx wordy passw2
name3 wordx wordy passw3
name (i) wordx wordy passw (i)
x
x word
x words
words
x
words

at moment can print line line:

line 1: name1 wordx wordy passw1
line 2: name2 wordx wordy passw2

i plan have access to: users [0] = name1
users [1] = name2
users [2] = name3
..

passws [0] = passw1
passws [1] = passw2
passws [2] = passw3
..

my code is:

public static void main(string args[]) throws filenotfoundexception, ioexception {      arraylist<string> list = new arraylist<string>();      scanner infile = null;     try {         infile = new scanner(new file("c:\\file.txt"));     } catch (filenotfoundexception e) {          e.printstacktrace();     }      while (infile.hasnextline()) {         list.add(infile.nextline()+",");     }      string liststring = "";     (string s : list) {         liststring += s + "\t";     }      string[] parts = liststring.split(",");     system.out.println("line1: "+ parts[0]); } 

how following output:

user name1 , password passw1
user name32 , password passw32

thanks in advance.

something do:

public static void main(string args[]) throws filenotfoundexception, ioexception {      arraylist<string> list = new arraylist<string>();      scanner infile = null;     try {         infile = new scanner(new file("c:\\file.txt"));     } catch (filenotfoundexception e) {          e.printstacktrace();     }      while (infile.hasnextline()) {         list.add(infile.nextline());     }      int line = 0;     string[] parts = list.get(line).split(" ");     string username = parts[0];     string pass = parts[3];      system.out.println("line" + (line + 1) + ": " + "user " + username +" , password " + pass); } 

edit: if want iterate through lines put last lines in loop:

for (int line = 0; line < list.size(); line++) {     string[] parts = list.get(line).split(" ");     string username = parts[0];     string pass = parts[3];     system.out.println("line" + (line + 1) + ": " + "user " + username +" , password " + pass); } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -