java - Skipping Every N th line while reading from file -


I am trying to read a file containing the data in the form,

  0 160 76 160 0 320 11 320 0 960 470 960 960 0 12 6 12 0 24 7 24 0 1500 302 1500 3000 0 70 33 70 0 140 60 140 0 784 74 784 3920  

I have to leave every fourth row (like 4, 8, 12, .. ATC), which contains single values. I tried this way,

  scanner scan = new scanner (new file inputstream ("demofile.txt")); Int Linenum = 0; While (scan.hasNextLine ()) {if (linenum! = 0 & amp; linenum% 4 == 0) {linenum ++; to continue; } Else {String str = scan.nextLine (); String-locator st = new string locator (str, ""); // Remove four tokens from a single line String a = st.nextToken (); String b = st.nextToken (); // This line throws java.util.NoSuchElementException // and so on .. // Other stuff ... linenum ++; }}  

Although this is not working, I'm detecting the error:

  Exception in thread "main" java.util.NoSuchElementException Java on URL on read_emo.main (readdemo.java48) StrongTokenNear.NXToken (stringtochnizer.java: 332) // line 48 second code `<< code>  

This indicates that I am reading before the end of the file, what is wrong with the above logic? Please help. Thank you.

Start linux 1 and your program will work. In addition, the increase of linenum has been implemented poorly. Try something like this:

  linenum = 1; While (scan.hasNextLine ()) {string line = scan. NXtine (); If ((linenum% 4)> gt; {string string adjuster string signifier = new string signifier (line, ""); ... blah} ++ linenum; }  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -