arrays - java read input from file with multiple data types of varying lengths -


i'm trying read file containing names , doubles, easy if first name, last name doubles, of lines have middle initial , have 1 name.
like this:
hancock john 40.00 9.75
light karen l 40.00 9.60
bob 12.02 42.90
and on i'm putting names string array , numbers 2d array. how separate data types? edit: trying @ first

   import java.io.*;    import java.text.*;    import java.util.*;     public class jedwards11      {       public static void main(string[] args) throws exception        {  printwriter outfile = new printwriter("h:/java/program11.out");            string filename = "h:/java/program11.dat", line, fullname;          stringtokenizer st;          scanner infile = new scanner(new filereader(filename));          int ctr = 0, = 0, k = 0;          string [] names = new string [30];//print vertically          double [][] money = new double [7][30];//short across, long down          //hours worked, hourly rate, gross pay, net pay, fed, state, union              //while(infile.hasnext())             for(i=0; i< names.length; i++)             {               if(infile.hasnextdouble())             {money[1][i] = infile.nextdouble();              money[2][i] = infile.nextdouble();}              else             names[i] = infile.next();              }               /*while(infile.hasnext())             {line = infile.nextline();             st = new stringtokenizer(line);             names[i]=st.nextstring;             money[1][i] = st.nextdouble();             money[2][i] = st.nextdouble();*/              for(i=0;i<names.length;i++)               for(i=0; i<names.length; i++)             system.out.println("name = " + names[i] +" money1 = "+money[1][i]                                                                  +" money2= "+money[2][i]);              infile.close();             outfile.close();              }              } 

which did not work @ all. since i've searching google , re-reading java book. sorry formatting, it's not being cut/paste friendly , last time hit enter posted before ready :(

you may have followed approach. anyway give 1 easy trick solve scanner, add tokens buffer until digits encountered, assign name array.

again, if-else block erroneous since per iteration either if-part or else-part executed. try this, solve problem :

for (i = 0; < names.length; i++) {         stringbuffer sbr = new stringbuffer();          while (infile.hasnext("[a-za-z]+")) {             sbr.append(infile.next() + " ");         }         names[i] = sbr.tostring().trim();          if (infile.hasnextdouble()) {             money[1][i] = infile.nextdouble();             money[2][i] = infile.nextdouble();         }     } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -