java - How do I isolate "tokens" into individual variables -


i've been trying teach myself java few months. i'm tired of books teach how "hello" , little else imo.

here question, want read "tab delimited" text file specific fields. of fields int, strings, , int[] arrays. (i find hard believe hard read simple file!)

how tokens variable first token in record(line) "int", second "string", 3 thru 5 separate "int" , next 21 in "int" array[]?

thanks in advance help.

package viewofgolfsystem;  import java.io.file; import java.io.filereader; import java.util.scanner;  import javax.swing.jfilechooser; import javax.swing.filechooser.filenameextensionfilter;  class textdatacheck {     static public void main(string args[]) throws exception {      jfilechooser chooser = new jfilechooser();     filenameextensionfilter filter = new filenameextensionfilter(                 "txt , csv files", "txt", "csv");     chooser.setfilefilter(filter);     int returnval = chooser.showopendialog(chooser);     if (returnval == jfilechooser.approve_option) {         chooser.addchoosablefilefilter(filter);         chooser.showopendialog(null);         file f = chooser.getselectedfile();          string filename = f.getabsolutepath();             // should return filename(i.e.path)          filereader flr = new filereader(filename);         scanner sinput = new scanner(flr).usedelimiter("\t");          while (sinput.hasnext()) {         system.out.println(sinput.next());             }         sinput.close();          } else {             system.out             .println("file chosen not approved option.  try again\n");         }     } } 

depending on route want go, , flexibility need write code yourself, , examples of such code can found on hundreds of sites, eg on blog. core of code propose this:

    bufferedreader breader = new bufferedreader(             new filereader(datafilename));      string line;      /**      * looping read block until lines in file read.      */     while ((line = breader.readline()) != null) {          /**          * splitting content of tabbed separated line          */         string datavalue[] = line.split("\t");         string value1 = datavalue[0];         string value2 = datavalue[1];         int value3 = integer.parseint(datavalue[2]);         double value4 = double.parsedouble(datavalue[3]);          /**          * printing value read file console          */         system.out.println(value1 + "\t" + value2 + "\t" + value3 + "\t"                 + value4);     }     breader.close(); 

should adapted purposes.

now, entirely different route use existing general purpose flatfile parser jsapar. beauty of solution entire conversion driven configuration file, if tomorrow want change file format easier adapt program. of course nothing free, take time learn such library, , adds size solution.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -