2013-04-26 164 views
0

我正在尝试读取文件,然后将文件打印出来。跳过第一行。什么是'不是声明'?

这是我的代码。

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(); 
    } 

} 
} 

我得到这个错误

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

是不是这个声明应该将指针移动到下一行,什么也不做呢?

是它的一个方法调用,为什么编译器不能捕获其他CL.nextLine?

+1

'CL.nextLine()'方法调用。 – 2013-04-26 04:24:12

回答

0

不应nextLine为执行:

CL.nextLine(); 

如果你只写“CL.nextLine”你说的方法的名称,但这并不做任何事情,你有执行方法“()”。你必须做同样的

CL.hasNext(); 
3

你必须改变 -

while(CL.hasNext) 

到 -

while(CL.hasNext()){ 

CL.nextLine.split(" ") 

到 -

CL.nextLine().split(" ") 

您的版本应该被解释为“语法错误”。

0

请参阅下面我在需要的地方更改了代码。你错过了 ”()” 。

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(); 
    } 
0
CL.nextLine; 

这不是一个方法调用。你应该把它像以下:

CL.nextLine(); 
0

Java编译器正在考虑nextLine是一个公共类属性(我猜你是试图调用nextLine方法,这意味着你应该使用CL.nextLine()),并因为你不能有一个这样的属性,而不会将它赋值给一个变量,或者这个语句(CL.nextLine)是有效的。

0

您需要使用括号方法:

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(); 
} 
0
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. 
+0

如果'Scanner'构造函数抛出,那么Nice'NullPointerException'。编码故意使用类似NPE(或更糟糕)那样的'null'。只要声明'main'方法就可以了。 – 2013-04-26 09:19:03