NetBeans中Java代码调用方法与打印语句报错问题求助
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
Method Call Error (Scanner Initialization)
When you pass a string like"test.txt"directly to theScannerconstructor, it reads that string as literal input content—not the file namedtest.txton your system. This is a common mix-up between reading a string vs. a file.System.out.println Error
The variable namecontetis misspelled; it should becontent(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.txtfile in the root of your NetBeans project (not inside thesrcfolder) unless you use a full path likenew 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

