You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetBeans中Java代码调用方法与打印语句报错问题求助

Fixing Your File Handling Errors in NetBeans

Let's walk through fixing those two errors in your Java code step by step. First, I'll fill in the missing parts of your code with the most common mistakes that match the issues you described, then break down the fixes.

Original (Error-Prone) Code

package practicefile;

/**
 *
 * @author jahkeyshagodwin
 */
import java.io.*;
import java.util.*;

public class Practicefile {
    // import java.util.*; // Unnecessary duplicate import (already declared above)

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        Scanner inp = new Scanner("test.txt"); // Error 1: Incorrect Scanner constructor use
        String content = inp.nextLine();
        inp.close();
        System.out.println(contet); // Error 2: Typo in variable name
    }
}

Identifying the Two Errors

  1. Method Call Error (Scanner Initialization)
    When you pass a string like "test.txt" directly to the Scanner constructor, it reads that string as literal input content—not the file named test.txt on your system. This is a common mix-up between reading a string vs. a file.

  2. System.out.println Error
    The variable name contet is misspelled; it should be content (you missed an 'n'). This causes a compile-time error because the compiler can't locate a variable with that misspelled name.

Corrected Code

Here's the fixed version that properly reads and prints file content, plus some improvements for reliability:

package practicefile;

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

/**
 *
 * @author jahkeyshagodwin
 */
public class Practicefile {

    /**
     * @param args the command line arguments
     * @throws FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {
        // Fix 1: Use a File object to target the actual file on your disk
        // Make sure "test.txt" is in your NetBeans project's root folder (same level as src)
        Scanner inp = new Scanner(new File("test.txt"));
        
        // Better practice: Read ALL lines of the file (not just the first one)
        while (inp.hasNextLine()) {
            String content = inp.nextLine();
            System.out.println(content); // Fix 2: Correct variable name, output inline
        }
        
        inp.close();
    }
}

Extra Robustness Tip

To avoid crashes if the file is missing or inaccessible, use a try-with-resources block to handle exceptions and auto-close the scanner (no need for manual close()):

public static void main(String[] args) {
    // Try-with-resources automatically closes the Scanner when done
    try (Scanner inp = new Scanner(new File("test.txt"))) {
        while (inp.hasNextLine()) {
            System.out.println(inp.nextLine());
        }
    } catch (FileNotFoundException e) {
        // Print a user-friendly error message instead of a messy stack trace
        System.err.println("Oops! Couldn't find the file: " + e.getMessage());
    }
}

Quick NetBeans Notes

  • Place your test.txt file in the root of your NetBeans project (not inside the src folder) unless you use a full path like new File("src/practicefile/test.txt").
  • NetBeans highlights compile errors in red—hover over the red underlines to get instant hints about what's wrong!

内容的提问来源于stack exchange,提问作者gabi145

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:40:01