【问题标题】:Exception in thread "main" Method call Java线程“主”方法调用 Java 中的异常
【发布时间】:2026-02-12 04:50:01
【问题描述】:

我对 Java 很陌生,如果我的问题太简单了,我很抱歉,但由于我找不到 switch 语句从未执行的原因,所以我仍然被阻止。 代码如下:

static void findSpot(String[][] parking) {
    boolean control = false, repeat;


    System.out.println("Insert o Floor : Spot");
    Scanner s = new Scanner(System.in);
    String[] floorSpot = s.nextLine().split(":");

    int floor, spot;
    floor = Integer.parseInt(floorSpot[0]);
    spot = Integer.parseInt(floorSpot[1]);

    if (floor >= parking.length) {
        System.out.println("Invalid floor number!");
    }


    System.out.println("Insert registry: 00-00-AA  00-AA-00");
    String registry = s.nextLine();
    repeat = validateRegistry(registry);

    if (!repeat) {
        System.out.println("Invalide format.");
    }
}

【问题讨论】:

  • 请同时显示(完整的)堆栈跟踪。
  • 代码中没有 switch 语句....
  • @MichaelBerry 它确实解释了为什么它没有被执行:)
  • @Stultuske 绝对。我当然希望我的代码很容易调试 ;-)
  • @paul 您的代码仍然不完整。它没有显示它是在哪个方法中,那么我们如何知道它是如何调用的,或者使用哪些参数?

标签: java


【解决方案1】:

对不起,堆栈上的定义溢出,不允许我在这里发布整个代码,并简要解释错误:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.System.out;



public class ex17 {

    public static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {

       int x=args.length;// 
       String[][] parking=new String[x][];

        setPositions(parking,args);

        boolean exit = false;
  while (!exit) {
            char choice;

            out.println("\nE- Entrance vehicle");
            out.println("T- Terminate");
            out.print("Option ->  ");
           choice = in.next().toLowerCase().charAt(0);

            switch (choice) {
                case 'e':
                    findSpot(parking);
                    break;
                    case 't':
                    exit=quitProgram();
                    break;
                default:
                    out.println(Invalid\n".toUpperCase());
                    break;


            }
static void findSpot(String[][] parking) {
    boolean control = false, repeat;


    System.out.println("Insert o Floor : Spot");
    Scanner s = new Scanner(System.in);
    String[] floorSpot = s.nextLine().split(":");

    int floor, spot;
    floor = Integer.parseInt(floorSpot[0]);
    spot = Integer.parseInt(floorSpot[1]);

    if (floor >= parking.length) {
        System.out.println("Invalid floor number!");
    }


    System.out.println("Insert registry: 00-00-AA  00-AA-00");
    String registry = s.nextLine();
    repeat = validateRegistry(registry);

    if (!repeat) {
        System.out.println("Invalide format.");
    }
}

【讨论】: