-1

I need to read the following txt.file with Scanner in java. The problem with this file is that there is a lot of information I don't need. The only things I need are the parameter(s) and the values of those parameters ( as an example, I need String n and int 5, String p and double 0.5, String lambda and double 0.3 ... ) the problem seems to be in the empty lines. I added the code I made, but if I run this, the second line of a distribution is never read.

What am I doing wrong?

txt-file as input:

distributie lengte klasse 1 naam verdeling  parameter(s)    value of parameter
                        n       5
                        p       0.5
distributie lengte klasse 2 naam verdeling  parameter(s)    value of parameter
                        lambda      0.3

distributie incidentie klasse 1 naam verdeling  parameter(s)    value of parameter
                        d       1

distributie incidentie klasse 2 naam verdeling  parameter(s)    value of parameter
                        n       8
                        p       0.1     
distributie servertijd klasse 1 naam verdeling  parameter(s)    value of parameter
                        d       1

distributie servertijd klasse 2 naam verdeling  parameter(s)    value of parameter
                        p       0.3

aantal pakketten te verwerken                   2000000

code

for(int a = 0; a< 6; ++a){

 inputStream.nextLine();
 System.out.print("\n"+inputStream.next());
 System.out.print("\n"+inputStream.next());
 String line = "";
 if (!(line = inputStream.nextLine()).isEmpty()) 
    {
     System.out.print("\n"+inputStream.next());
     System.out.print("\n"+inputStream.next());
    }
 else
 {

 }
 inputStream.nextLine();
 }}
Haldean Brown
  • 11,023
  • 4
  • 38
  • 55
StudentX
  • 405
  • 1
  • 4
  • 7
  • Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – thegauravmahawar Nov 18 '15 at 16:39
  • Just a hint: you don't need `else { }`. If you're not doing anything in `else` part of the `if`, you can avoid putting it. – a.ndrea Nov 18 '15 at 16:41

2 Answers2

0

Here are some improvements:

  • For the loop header I'd use a++ to increment a -- just preference.
  • .nextLine() returns a String so maybe reset the inputStream variable after executing .nextLine().
  • Print inputStream.nextLine() rather than inputStream.next().
  • In the if conditional I wouldn't initialise the variable line.
  • The if else condition doesn't need to be there to do what you want, i.e. to read the six values.

My recommendation is to read up about REGEX and if the REGEX of the line matches n,p,lamda,d and another value, then print.

for(int a = 0; a< 6; a++){
    inputStream.nextLine();
    //possibly reset inputStream here with something like inputStream = new Scanner(...);
    System.out.print("\n"+inputStream.nextLine());
    System.out.print("\n"+inputStream.nextLine());
}
trincot
  • 211,288
  • 25
  • 175
  • 211
0

Short answer: Don't use Scanner.

You said you "would like to read the following txt.file with the scanner method", but you didn't say why, and we don't always get what we like. In this case, using a Scanner is far from the best choice.

Looking at the file, the format seems to be data in blocks of 3 lines, with first line starting with the word distributie. At the end of the file there's a summary line starting with aantal.

Lines 2 and 3 of each block is either a keyword + decimal number, or a blank line. Since you are only after those keyword + number pairs, I'd suggest reading the file line-by-line, and matching keyword + number lines using a regular expression:

try (BufferedReader in = new BufferedReader(new FileReader(file))) {
    Pattern p = Pattern.compile("\\s+(\\w+)\\s+([0-9.]+)\\s*");
    for (String line; (line = in.readLine()) != null; ) {
        Matcher m = p.matcher(line);
        if (m.matches()) {
            String keyword = m.group(1);
            double number = Double.parseDouble(m.group(2));
            System.out.println(keyword + " = " + number);
        }
    }
}
Andreas
  • 138,167
  • 8
  • 112
  • 195
  • thank you for your help, I never worked with a regular expression but i will give it a try ... If i use this, only one keyword and number is found. do I have to create another loop around it to make it go through the whole txt.file ? – StudentX Nov 18 '15 at 18:02
  • Sorry, last character of pattern should be `*`, not `+`. – Andreas Nov 18 '15 at 21:42