-1

I am scanning a text file in Java and reading it line by line using Buffered Reader. I have some text in its 60 to 80 th position. Depending upon the texts in this position I need to decide whether to skip the line or read some data from the same line. In this case if I find "END OF HEADER" I need to skip that line. I used bufferedreader.skip(line.lenght()) that is 80 here, to skip that line and move to next line to read some text but again it gives string out of range exception.

streamObs = new FileInputStream(obsFile);
inStreamObs = new InputStreamReader(streamObs);
buffStreamObs = new BufferedReader(inStreamObs);
BufferedReader in = new BufferedReader(new FileReader(obsFile));
String line="";
while((line = in.readLine()) != null)
{
  String typeField=line.substring(Math.min(line.length(),60),line.length());
  //System.out.println(typeField);
  typeField=typeField.trim();
  if (typeField.equals("RINEX VERSION / TYPE")) {
    System.out.println(" Current version:"+line.substring(5,9));
  }
  if (typeField.endsWith("TIME OF FIRST OBS")){
    System.out.println("Time of First Observation:"+ line.substring(2,44));
  }
  if (typeField.equals("END OF HEADER"))
  {
    in.skip(80);

  }
  System.out.println(line.substring(Math.min(line.length(),30),32));

}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
Bhoj Raj
  • 19
  • 4
  • Post your exception. – barq Feb 03 '15 at 13:43
  • Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 32 at java.lang.String.substring(String.java:1950) at jRinex.Rinex.parseRinexOFile(Rinex.java:168) at jRinex.Rinex.jButton1ActionPerformed(Rinex.java:135) at jRinex.Rinex.access$100(Rinex.java:24) at jRinex.Rinex$2.actionPerformed(Rinex.java:70) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) – Bhoj Raj Feb 03 '15 at 13:47
  • Why don't you just use a `continue` in case you find `END OF HEADER`? – Arnaud Denoyelle Feb 03 '15 at 13:47

2 Answers2

1

In order to skip some loop iteration you need to use continue;

roeygol
  • 4,908
  • 9
  • 51
  • 88
  • `continue` is not the only option; `if..else` would work just as well in this case – gknicker Feb 03 '15 at 16:34
  • but when you decide not to continue with the current while code and continue to the next iteration, you need to use `continue`, just try to consider it – roeygol Feb 03 '15 at 16:37
0

dude, just do nothing.
in.readLine() triggers an iterator which sets its index to the next line after you called the method. it returns the line the iterator was at and add 1 to the iterators index.

if (! typeField.equals("END OF HEADER"))
 {
 if (typeField.equals("RINEX VERSION / TYPE")) {
  System.out.println(" Current version:"+line.substring(5,9));
 }
 if (typeField.endsWith("TIME OF FIRST OBS")){
  System.out.println("Time of First Observation:"+ line.substring(2,44));
 }
  System.out.println(line.substring(Math.min(line.length(),30),32));
}

btw, dont use fix values for something like this. define private final fields or use getters and setters to get and/or change the values. 30, 32, 2, 44, 5 and 9 shouldn't be hardcoded numbers

Basti
  • 1,117
  • 12
  • 32
  • But my text files have some definite characters at definite position, so I have been using those hard coded values. At some lines I need to read substrings between 2,44 and at some other condition I need to read between 5,9 for example. – Bhoj Raj Feb 03 '15 at 14:15
  • Well the mechanic behind it is totaly fine. but what you are doing here is initializing every time this method runs through creating new integers per instance of this object, but always exactly the same. create private static final int currentVersionBegin = 5, currentVersionEnd = 9, tiOfFiObsBegin = 2, tiOfFiObsEnd = 44, defaultBegin = 30, defaultEnd = 32; this way those values are created only once and do not have to be created for every method call for every instance :) understood whant i mean? – Basti Feb 03 '15 at 14:19