java - Can't pass string from one class to another -


hello trying write program takes third line comma separated text file passes through getdata method in weatherdatapoint class splits string separate strings on comma. want string variable date set value of dataline2[7]. print out date string calling getdate method driver class.

public class driver  

{

public static void main(string[] args)  {     weatherdatapoint weather = new weatherdatapoint();     string dataline1 = "";     string inputfile = "weatherdata.csv";     scanner readfile = null;     try     {         readfile = new scanner (new file(inputfile));     }     catch (filenotfoundexception ex)     {         system.out.println("error file not found");         system.exit(1);     }      while (readfile.hasnextline())     {          dataline1 = readfile.nextline();          weather.getdata(dataline1);        }      system.out.println(string.format("%s",weather.getdate())); } 

}

public class weatherdatapoint { private string date = ""; private string temperature;   public void getdata(string dataline1) {     string [] dataline2 = dataline1.split(",");     date = dataline2[7];     //long epoc = long.parselong(fields[date_pos]);     //date d = new date(epoc * 1000);  }   public string getdate() {     //return string.format("%s", (date));     return date; } 

when run program error

exception in thread "main" java.lang.arrayindexoutofboundsexception: 7 

edit here line text file

"1452386100","1","5","29.002591800284698","77.91272888363851","78.21244075516647","86.27","49.8",,"99","0",,"0",,"0","0","49.53002056234984","49.8","49.8",,,,,,,,,,,,,,,,,,,,,"100",,,,,,,,,,,, 

i made mistake when describing program want process of data in file skip first 3 lines sorry confusion. changing dataline[7] dataline[0] works.

try replace this:

 string [] dataline2 = dataline1.split(","); date = dataline2[7]; 

with:

  string [] dataline2 = dataline1.split(",");   date = dataline2[dataline2.length-1]; 

because in array dataline2 don't have element in 7th index.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -