Java程序遇ArrayIndexOutOfBoundsException异常求助(命令行参数问题)
Hey Kris, let's break down what's causing that error and fix it step by step!
The Root Cause
Your ArrayIndexOutOfBoundsException: 1 is happening because you're using args[1] in your code—but args refers to command-line arguments passed when starting your program, not the parameter the user types into the console.
If you run your program without adding command-line arguments (like just clicking "Run" in NetBeans), the args array is empty or has fewer elements than you're trying to access. Even worse, you meant to use the second part of the user's input (the part after the comma) instead of command-line args anyway!
Step-by-Step Fixes
1. Replace args[1] with the user's input parameter
You split the user's input into tokens using split(",")—the second token (tokens[1]) is the command you want to check. Don't forget to trim whitespace from it (since users might type boat, power on instead of boat,power on):
// Replace args[1] with tokens[1].trim() if(tokens[1].trim().equalsIgnoreCase("power on")) { System.out.println(name + " power up the boat"); } else if(tokens[1].trim().equalsIgnoreCase("power off")) { System.out.println(name + " turn off the boat"); } else { System.out.println(name + " command does not exist"); }
2. Remove duplicate Scanner initialization
You're creating a new Scanner(System.in) every time inside the while loop, which can cause input reading glitches. Keep only one Scanner instance for console input:
// Move this line outside the while loop, after reading the file keyboard = new Scanner(System.in); while (keyboard.hasNextLine()) { String input = keyboard.nextLine(); // ... rest of your code ... // Remove this line from inside the loop: keyboard = new Scanner(System.in); System.out.println("Enter boat name (name) or quit: "); }
3. (Optional) Make command checks case-insensitive
Using equalsIgnoreCase() instead of equals() makes your program more user-friendly—users can type "Power On", "POWER ON", etc., and it will still work.
Full Corrected Code
import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.util.Scanner; public class main { /** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { String filename = "Boats.txt"; ArrayList<String> list = new ArrayList<String>(); FileReader f = new FileReader(filename); Scanner keyboard = new Scanner(f); while(keyboard.hasNextLine()){ list.add(keyboard.nextLine()); } System.out.println("File Contents: " + list); System.out.println("Enter boat name (name) or quit: "); keyboard = new Scanner(System.in); // Only initialize once for console input while (keyboard.hasNextLine()) { String input = keyboard.nextLine(); if (input.equalsIgnoreCase("quit")) { break; } String[] tokens = input.split(","); if (tokens.length != 2) { System.out.println("Invalid format, try again"); System.out.println("Enter boat name (name) or quit: "); continue; } String name = tokens[0].trim(); String command = tokens[1].trim(); // Extract the user's command System.out.printf("%-10s \r\n", name); if(list.contains(name)){ System.out.println(name + " found in file"); if(command.equalsIgnoreCase("power on")) { System.out.println(name + " power up the boat"); } else if(command.equalsIgnoreCase("power off")) { System.out.println(name + " turn off the boat"); } else { System.out.println(name + " command does not exist"); } }else{ System.out.println(name + " not found in file"); } System.out.println("Enter boat name (name) or quit: "); } keyboard.close(); } }
Why This Works
- We're now using the actual command the user typed into the console (from
tokens[1]) instead of confusing it with command-line arguments. - Only one Scanner handles console input, so you won't run into unexpected input issues.
- The code is more forgiving of user input (whitespace and case differences).
内容的提问来源于stack exchange,提问作者Kris

