getting two consicutive value from file in java -


i working csv file, , want extract 2 values in specific positions each , every row.

the csv input file looks this:

a, b, c, d 12,32,45,76 23,45,77,56 32,34,49,28 73,92,26,68 73,36,77,26 

for example want 2 consecutive values 3rd position (column c) every row @ same time, (45, 77), (49, 26), (77, ???)…

after getting 2 values, want calculation on them , store them back.i working on []2x1 size matrix multiplication. reason need 2 consecutive value @ time.

package rotation.pkg45;import java.io.file;  import java.io.filenotfoundexception; import java.util.scanner; import java.util.logging.level; import java.util.logging.logger; import java.io.filewriter; import java.io.*; import java.text.decimalformat; import java.util.arraylist; import java.util.collections; import java.util.list; import java.util.listiterator;  public class rotation45 {     public static void main(string[] args) throws ioexception {         string filename = "bank-full2.csv";          file file = new file(filename);         bufferedwriter writer = null;          try {             writer = new bufferedwriter(new filewriter("bank2test1.csv"));                  double a1 = 0.866025;             double a2 = 0.5;             double a3 = -0.5;             double a4 = 0.866025;             double b1;             double b2;             double c1;             double c2;                      scanner inputstream = new scanner(file);             inputstream.next();              scanner inputstreamm = new scanner(file);             inputstreamm.next();                       while (inputstreamm.hasnext()) {                   string data = inputstreamm.next(); //read each line , store in data                 string[] values = data.split(","); //every line splited " ; " , store each attribute in string list                  double first = double.parsedouble(values[2]);   /*nosuchelementexception*/string data1 = inputstreamm.next(); //read comming nextline second value , store in data1                 string[] values1 = data1.split(",");                  //inputstream.next();                          double second = double.parsedouble(values1[2]);                  c1 = ((a2 * second) + (a1 * first));                 c2 = ((a3 * first) + (a4 * second));                 values1[2] = string.valueof(c2);                 values[2] = string.valueof(c1);                 stringbuilder sb = new stringbuilder();                 //string newdata = sb.tostring();                 (int = 0; < values.length; i++) {                     sb.append(values[i]);                     if (i < values.length - 1) {                         sb.append(",");                     }                 }                 sb.append("\n");                 (int = 0; < values1.length; i++) {                     sb.append(values1[i]);                     if (i < values.length - 1) {                         sb.append(",");                     }                 }                 //get new string                 //system.out.println(sb.tostring());                  writer.write(sb.tostring()+"\n");             }             writer.close();             inputstreamm.close();         } catch(filenotfoundexception ex) {             logger.getlogger(rotation45.class.getname()).log(level.severe, null, ex);     } } 

i getting error nosuchelement exception @ mention in code...

i solved issue using bufferedreader instead of scanner read datas. since code trying read whole line via scanner , split it, can in easier way using bufferedreaderand calling readline()method.

so changes brought code replacement of use of scanner use of bufferedreader. i have not checked computing seems me working fine.

here image of result get:

result of computations

also since while loop based on scanner changed too.the while loop using checks both lines not null (reading them @ same time while condition being assessed)

the last point regarding code on bufferedwriter have newline()method inserts portable equivalent of line skeep (the \n in code)

here code. execute , check working fine.

public class rotation45 {     public static void main(string[] args) throws ioexception {         string filename =     "bank-full2.csv";          file file = new file(filename);         bufferedwriter writer = null;         bufferedreader reader = null;          try {              reader = new bufferedreader(new filereader(file));             writer = new bufferedwriter(new      filewriter("bank2test1.csv"));                  double a1 = 0.866025;             double a2 = 0.5;             double a3 = -0.5;             double a4 = 0.866025;             double b1;             double b2;             double c1;             double c2;                      string line1= null;             string line2 = null;             // skeeps head line             reader.readline();              string currentline = null;              while (  ((line1 = reader.readline())!= null) && ((line2 =     reader.readline()) != null)       ) {                  string[] values = line1.split(",");                   double first = double.parsedouble(values[2]);                  string[] values1 = line2.split(",");                   double second = double.parsedouble(values1[2]);                  c1 = ((a2 * second) + (a1 * first));                 c2 = ((a3 * first) + (a4 * second));                 values1[2] = string.valueof(c2);                 values[2] = string.valueof(c1);                 stringbuilder sb = new stringbuilder();                  (int = 0; < values.length; i++) {                     sb.append(values[i]);                     if (i < values.length - 1) {                         sb.append(",");                     }                 }                 sb.append("\n");                 (int = 0; < values1.length; i++) {                     sb.append(values1[i]);                     if (i < values.length - 1) {                         sb.append(",");                     }                 }                  writer.write(sb.tostring());                 writer.newline();             }             writer.flush();             writer.close();             reader.close();          } catch(filenotfoundexception ex) {             logger.getlogger(rotation45.class.getname()).log(level.severe,      null, ex);         }     } } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -