reclassify a list of strings JAVA -
i need java code
i have list saved in text file following :
a b c d c h b e b f b g g l e e j f k k m 1 2 1 3 2 4
after reading file test.txt, lines added list ( named ch in code) composed of 2 parts, fist part source extracted variable src in code second part target extract trg in code
for instance, in first line : b source (src) et target (trg) b
now, want create new list (mylist in code) when there line target equal source of previous line. instance, let's stay fist line , see 5th line :
a b b e
the new list mylist must contain line : b e
so in general ch list must reclassified following :
a d c h b g l b e b e j b f k m 1 2 4 1 3
i obtain following exception :
line:a b exception in thread "main" java.lang.nullpointerexception @ pathcreator.test.main(test.java:48) picked _java_options: -xmx512m
this code not working basicly because building new list 3 elements mylist.add(src[a] +" "+trg[a]+" "+trg[b]); while it's more.
any help?
here code:
import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.util.linkedlist; public class test { public static void main(string[] args) throws filenotfoundexception { filereader fis; fis = new filereader("c:\\test.txt"); bufferedreader in = new bufferedreader(fis); string i=null; string line =null; string[] src = null; string[] trg = null; linkedlist<string> ch = new linkedlist<string>(); linkedlist<string> mylist = new linkedlist<string>(); try { while((i = in.readline()) != null) { line = i; ch.add(line); system.out.println(" line:" + line); src = new string[ch.size()]; trg = new string[ch.size()]; for(int j = 0; j<ch.size()-1;j++){ string pc = ch.get(j).tostring(); string[] pe = pc.split(" "); src[j] = pe[0]; trg[j] = pe[1]; } for(int = 0; a<src.length; a++){ for(int b = a; b<trg.length; b++){ if(src[b].equals(trg[a]) && a!= b){ mylist.add(src[a] +" "+trg[a]+" "+trg[b]); system.out.println(src[a] +" "+trg[a]+" "+trg[b]); } } } } } catch (ioexception e){ system.out.println(e.getmessage()); system.exit(1); } } }
if understand problem:
the input list of edges of graph,
output list of roof-to-leaf paths of trees in graph.
i recommend fill graph structure input, search , use algorithm print roof-to-leaf paths (you can find 1 on google/stackoverflow).
your exception because of j<ch.size()-1
, leaves last element of src
, trg
null. try j<ch.size()
.
finally, please don't use unintelligible abbreviations variable names, avoid trg
, ch
, pe
, ...
Comments
Post a Comment