xml - Two problems with this java code, one fatal one not as fatal what are they? -
here code. program made interview, told code had 1 fatal flaw, , 1 smaller problem. not find either. wrong program? i've tested , seems work fine. know fileexists() method kind of bad , need more checks... other not sure else wrong...
import java.io.bufferedwriter; import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.util.arraylist; import java.util.scanner; import java.util.regex.matcher; import java.util.regex.pattern; //i chose use library since offers solution performing operations on csv files, better 1 can provide scratch. import au.com.bytecode.opencsv.csvreader; public class csv2xml { //main program loop public static void main(string[] args) { string csvfilename; string xmlfilename; arraylist<arraylist<string>> data; //raw unformatted data boolean exists; string xml; //formated xml data do{ system.out.println("enter file path: "); csvfilename = getuserinput(); //gets filename user exists = fileexists(csvfilename); if(exists){ //checks see if file exists data = readfile(csvfilename); //reads in content csv file xml = toxml(data); xmlfilename = csvfilename.split("\\.")[0]+".xml"; //gets part of filename before first . writetofile(xml,xmlfilename); } else system.out.println("invalid file name, use full file path."); }while(!exists); } //reads line of input user //returns: string public static string getuserinput(){ scanner scanner = new scanner(system.in); string input = scanner.nextline(); return input; } //checks see if file exists //arguments: string (name of file) //returns boolean, true if file exists, false if not exist. public static boolean fileexists(string filename){ file file = new file(filename); if (file.exists()){ system.out.println("file exists"); return true; } return false; } //reads content of csv file , returns data in 2d array list //arguments: string (file name) //returns: arraylist<arraylist<string>> (a 2d arraylist containing data csv file in table format) public static arraylist<arraylist<string>> readfile(string filename){ arraylist<arraylist<string>> data = new arraylist<arraylist<string>>(); arraylist<string> currentrow; csvreader reader = null; try { reader = new csvreader(new filereader(filename)); } catch (filenotfoundexception e) { e.printstacktrace(); } string [] nextline = null; // nextline[] array of values in current line try { while ((nextline = reader.readnext()) != null) { currentrow = new arraylist<string>(); for(string s: nextline){ //puts data row in arraylist currentrow.add(s); } data.add(currentrow); //adds row arraylist } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return data; } //inserts xml tags between data segments,rows , columns //arguments: arraylist<arraylist<string>> (csv data) //returns: string (data xml tags) public static string toxml(arraylist<arraylist<string>> data){ string xml = "<?xml version= \"1.0\"?>"; for(int = 0; < data.size(); i++){ xml += "\n<row id=\""+i+"\">\n"; for(int z = 0; z < data.get(i).size(); z++){ xml += "<column id=\""+z+"\">\n"; xml += "\t<data>"; xml += data.get(i).get(z); xml += "</data>"; xml += "\n</column>\n"; } xml += "</row>"; } return xml; } //writes data in filename specified //arguments: string (data write), string (filename write data to) public static void writetofile(string data,string filename){ try { string fn = filename; file file = new file(filename); // if file doesnt exists, create if (file.exists()) { system.out.println("file exists, overwrite?(y/n):"); if(getuserinput().tolowercase().equals("n")){ do{ system.out.println("enter new file name:"); filename = getuserinput()+".xml"; file = new file(filename); }while(!isvalidname(filename)); } } else{ file.createnewfile(); } filewriter fw = new filewriter(file.getabsolutefile()); bufferedwriter bw = new bufferedwriter(fw); bw.write(data); bw.close(); system.out.println("done"); } catch (ioexception e) { e.printstacktrace(); } } //check files name make sure valid (found in msdn documentation) //arguments: string (filename) //returns: boolean (true if filename valid false if invalid) public static boolean isvalidname(string text) { pattern pattern = pattern.compile( "# match valid windows filename (unspecified file system). \n" + "^ # anchor start of string. \n" + "(?! # assert filename not: con, prn, \n" + " (?: # aux, nul, com1, com2, com3, com4, \n" + " con|prn|aux|nul| # com5, com6, com7, com8, com9, \n" + " com[1-9]|lpt[1-9] # lpt1, lpt2, lpt3, lpt4, lpt5, \n" + " ) # lpt6, lpt7, lpt8, , lpt9... \n" + " (?:\\.[^.]*)? # followed optional extension \n" + " $ # , end of string \n" + ") # end negative lookahead assertion. \n" + "[^<>:\"/\\\\|?*\\x00-\\x1f]* # 0 or more valid filename chars.\n" + "[^<>:\"/\\\\|?*\\x00-\\x1f\\ .] # last char not space or dot. \n" + "$ # anchor end of string. ", pattern.case_insensitive | pattern.unicode_case | pattern.comments); matcher matcher = pattern.matcher(text); boolean ismatch = matcher.matches(); return ismatch; } }
here guess :
csvreader reader = null; try { reader = new csvreader(new filereader(filename)); } catch (filenotfoundexception e) { e.printstacktrace(); } string [] nextline = null; // nextline[] array of values in current line try { while ((nextline = reader.readnext()) != null) { currentrow = new arraylist<string>(); for(string s: nextline){ //puts data row in arraylist currentrow.add(s); } data.add(currentrow); //adds row arraylist } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }
so happens if not initialize csvreader ? next block throw nullpointer exception. not catching one.
also reading manually line line when can use library function.
Comments
Post a Comment