简体   繁体   中英

NetBeans won't print to console JAVA

I have written a simple program in the NetBeans IDE using Java. After making a few changes to the main method this morning, the console does not print anything when I run the program. I simply want it to reach startMenus(sc). EDIT: I have now put in a few System.out.println() and it does not reach "Blah2" which is right after my first loop...

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

}
}

Are you sure you aren't throwing any other exceptions that are killing your app before it reaches System.out.println? Judging by your description you may want to either debug or put some other println statements further up the chain as it could be dying due to something.

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!!!");
    }
}

Couple of things:

  1. You need to import additional classes that are not part of your core project. Exceptions, File, and Scanner all fall into this category.
  2. You need to run the createNewFile() method to actually create your file. Your original code was throwing a FileNotFound exception because the file was never being created.
  3. You need to have the startMenus method defined before calling it.

I've included some corrected code. Hope this helps!

The System.out calls probably wheren't reached yet, because one of your loops took too long to execute, longer then you were willing to wait. Log something from inside a loop to get some more feedback, the program is probably fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM