-3

I want to read from specific line and so on. For instance I want to read from line 8 and until it gets to the end of the file. Could someone teach me the proper way on how to program it?

My current code:

import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;

public class Arff {
    public static void main(String[] args) throws FileNotFoundException {

        File TextFile = new File("weather.nominal.arff");
        Scanner reader = new Scanner(TextFile);

        while(reader.hasNextLine()) {
            String text = reader.nextLine();
            String[] SplitData = text.split(" ");

            if (SplitData[0].equals("@relation")) {
                System.out.println(SplitData[1]);
                System.out.println();
            }
            if (SplitData[0].equals("@attribute")) {
                System.out.print(SplitData[1] + " ");
            }
        }
    }
}

weather.nominal.arff

@relation weather.symbolic
@attribute outlook {sunny, overcast, rainy}
@attribute temperature {hot, mild, cool}
@attribute humidity {high, normal}
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}

@data
sunny,hot,high,FALSE,no
sunny,hot,high,TRUE,no
overcast,hot,high,FALSE,yes
rainy,mild,high,FALSE,yes
rainy,cool,normal,FALSE,yes
rainy,cool,normal,TRUE,no
overcast,cool,normal,TRUE,yes
sunny,mild,high,FALSE,no
sunny,cool,normal,FALSE,yes
rainy,mild,normal,FALSE,yes
sunny,mild,normal,TRUE,yes
overcast,mild,high,TRUE,yes
overcast,hot,normal,FALSE,yes
rainy,mild,high,TRUE,no

Desired output:

weather.symbolic

outlook temperature humidity windy play

sunny,hot,high,FALSE,no
sunny,hot,high,TRUE,no
overcast,hot,high,FALSE,yes
rainy,mild,high,FALSE,yes
rainy,cool,normal,FALSE,yes
rainy,cool,normal,TRUE,no
overcast,cool,normal,TRUE,yes
sunny,mild,high,FALSE,no
sunny,cool,normal,FALSE,yes
rainy,mild,normal,FALSE,yes
sunny,mild,normal,TRUE,yes
overcast,mild,high,TRUE,yes
overcast,hot,normal,FALSE,yes
rainy,mild,high,TRUE,no

Tom
  • 16,842
  • 17
  • 45
  • 54
soyan
  • 59
  • 5
  • 1
    Your problem doesn't seem to match your question. For starters, there aren't even 32 lines in your input file. – Erick G. Hagstrom Sep 28 '15 at 02:29
  • Your desired output is *not* skipping the first lines, it is transforming them and *including* them in the output. So your question does not match your expected input/output at all. – Erwin Bolwidt Sep 28 '15 at 03:02

1 Answers1

0

Regarding your question: if you know the lines you want to skip, just define a counter variable before the loop and count the lines you read in the loop.

int count = 0;
while {
//....
  if (count >=8){
  //...
  }
  count++;
}

regarding the example data, you could also use a boolean variable outside the loop and store wether @data was already read - this would be better if the line index is not known already or could change over time.

Christian R.
  • 1,528
  • 9
  • 16