2013-03-11 159 views
0

我已經使用Java在NetBeans IDE中編寫了一個簡單的程序。在今天上午對主要方法進行了一些更改之後,當我運行該程序時,控制檯不會打印任何內容。我只是想讓它達到startMenus(sc)。編輯:現在我已經把幾的System.out.println(),它沒有達到「Blah2」這是我的第一個循環後,右......NetBeans不會打印到控制檯JAVA

public class Calculator { 

public static int[] NUMBERS; //global value for the array 

public static void main(String[] args) throws FileNotFoundException {  
    File file = new File("data.txt"); 
    Scanner sc = new Scanner(file); 

    System.out.println("Blah1"); 

    int counter = 0; 
    while (sc.hasNextInt()) { 
     counter = counter++; 
    } 

    System.out.println("Blah2"); 

    int lenth = counter; 

    NUMBERS = new int[lenth]; 

    System.out.println("Blah3"); 

    sc.close(); 

    File file2 = new File("data.txt"); 
    Scanner sc2 = new Scanner(file2); 

    System.out.println("Blah4"); 

    int i = 0; 

    while (sc2.hasNextInt()) { 
     NUMBERS[i] = sc2.nextInt(); 
     ++i; 
    } 

    System.out.println("Blah5"); 

    sc2.close(); 


    System.out.println("Welcome to Calculation Program!\n"); 
    startMenus(sc); 

} 
} 
+0

是否有例外?注意到主要方法拋出一個異常,但是誰或什麼正在處理該異常? – Mechkov 2013-03-11 19:46:38

+0

你確定這不是因爲它只是沒有完成循環之前,你認爲它沒有做到這一點? – 11684 2013-03-11 19:46:50

+1

在程序啓動時嘗試System.out,以確保:) – 2013-03-11 19:47:39

回答

0

你確定你不拋出任何在到達System.out.println之前會殺死您的應用程序的其他異常?根據您的描述來判斷,您可能想要調試或將其他println語句放在鏈條上,因爲它可能因某種原因而死亡。

+0

我只拋出FileNotFoundException。 – lancer 2013-03-11 19:55:55

0
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Scanner; 

public class Calculator {  

    public static int[] NUMBERS; //global value for the array 

    public static void main(String[] args) throws FileNotFoundException, IOException { 
    File file = new File("data.txt"); 
    file.createNewFile(); 
    Scanner sc = new Scanner(file); 

    int counter = 0; 
    while (sc.hasNextInt()) { 
     counter = counter++; 
    } 

    int lenth = counter; 

    NUMBERS = new int[lenth]; 

    sc.close(); 

    File file2 = new File("data.txt"); 
    file2.createNewFile(); 
    Scanner sc2 = new Scanner(file2); 

    int i = 0; 

    while (sc2.hasNextInt()) { 
     NUMBERS[i] = sc2.nextInt(); 
     ++i; 
    } 

    sc2.close(); 


    System.out.println("Welcome to Calculation Program!\n"); 
    startMenus(sc); 

} 

    private static void startMenus(Scanner sc) { 
     System.out.println("Run your code here!!!"); 
    } 
} 

幾件事情:

  1. 您需要導入其他類,是不是你的核心項目的一部分。例外,文件和掃描儀都屬於這一類。
  2. 您需要運行createNewFile()方法來實際創建文件。您的原始代碼正在拋出一個FileNotFound異常,因爲該文件從未被創建。
  3. 在調用它之前,您需要定義startMenus方法。

我已經包含一些更正的代碼。希望這可以幫助!

+0

我在程序中有startMenus方法,我只是沒有在這裏發佈。我也創建了這些文件,並將它們放到與項目相同的文件夾中。讓我試一下IOException,看看是否能解決它 – lancer 2013-03-11 20:00:47

0

System.out調用可能還沒有達到,因爲你的循環執行時間太長,所以你願意等待。從循環內部記錄一些東西以獲得更多的反饋,程序可能很好。

+0

我相信第一個循環是無限的。所以我需要以某種方式消耗每個整數,因爲它從文件中讀取... – lancer 2013-03-12 02:25:30

+0

您的第一個循環確實不會消耗整數,因此掃描程序將始終有一個下一個整型(除非它根本沒有),因爲您從不繼續。通過向循環體添加'sc.nextInt();來修復它(不捕獲那個int(如果你不需要它))。 – 11684 2013-03-12 08:35:02