2013-04-26 163 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