myacobucci
myacobucci

Reputation: 185

what is 'Not a statement'?

I'm trying to read a file and then print the file back out. Skipping the first line.

Here is my code.

import java.util.Scanner;
import java.io.File;
import java.io.*;
public class cas{
public static void main(String[] args) {
Scanner CL = new Scanner(new File("myBoard.csv"));
    CL.nextLine;
    while(CL.hasNext){
        String[] tempAdd = CL.nextLine.split(" ");
        for(int i = 0; i<tempAdd.length; i++)
            System.out.print(tempAdd[i] + " ");
        System.out.println();
    }

}
}

I'm getting this error

cas.java:7: not a statement
    CL.nextLine;

Isn't this statement supposed to move the pointer to the next line and do nothing with it?

Yes its a method call, why does the compiler not catch the other CL.nextLine ?

Upvotes: 0

Views: 134

Answers (6)

vipin
vipin

Reputation: 162

import java.util.Scanner;
import java.io.*;
public class puzzle {
public static void main(String[] args) {



    Scanner CL = null;

    try {
        CL = new Scanner(new File("F:\\large_10000.txt"));
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }
    CL.nextLine();
        while(CL.hasNextLine()){
            String[] tempAdd = CL.nextLine().split(" ");

            for(int i = 0; i<tempAdd.length; i++)
                System.out.print(tempAdd[i] + " ");
            System.out.println();
            break;
        }



}
}**strong text**

This code is working fine .just little mistakes.

Upvotes: 0

Anders Lind&#233;n
Anders Lind&#233;n

Reputation: 7323

You'll have to change -

while(CL.hasNext)

to -

while(CL.hasNext()){

and

CL.nextLine.split(" ")

to -

CL.nextLine().split(" ")

Your version should be interpreted as "syntax error".

Upvotes: 3

Achintya Jha
Achintya Jha

Reputation: 12843

See below I changed the code where needed. You missing "()" .

CL.nextLine();
    while(CL.hasNext()){
        String[] tempAdd = CL.nextLine().split(" ");
        for(int i = 0; i<tempAdd.length; i++)
            System.out.print(tempAdd[i] + " ");
        System.out.println();
    }

Upvotes: 0

CloudyMarble
CloudyMarble

Reputation: 37576

You need to use the brackets for methods:

scanner.nextLine();                              // nextLine() with brackets->()
while (scanner.hasNext()) {                      // hasNext() with brackets->()
  String[] tempAdd = CL.nextLine().split(" ");   // nextLine() with brackets->()
  for(int i = 0; i<tempAdd.length; i++)
    System.out.print(tempAdd[i] + " ");

  System.out.println();
}

Upvotes: 0

aminhjz
aminhjz

Reputation: 11

Java compiler is thinking nextLine is a public class property ( which i guess you are trying to call nextLine method which means you should use CL.nextLine() ) and since you cant have a property like that without asigning it to a variable or some thing this statement (CL.nextLine) is invalid .

Upvotes: 0

user1445967
user1445967

Reputation: 1570

Shouldn't nextLine be executed as:

CL.nextLine();

If you just write "CL.nextLine" you have stated the name of the method but this does not do anything, you have to execute the method with "()". You must do the same with

CL.hasNext();

Upvotes: 0

Related Questions