java - Try-Catch 中的静态方法未完全执行

标签 java exception static-methods

我将对象添加到对象数组列表的静态方法。

public static void addObject() {
    int id;
    String name;

    try{
        System.out.print("Id: ");
        id = Integer.parseInt(sc.next());

        System.out.print("Name: "); //when the program gets here, he just skips back to my main class...
        name = sc.nextLine();

        Object o = new Object(id, name);
        l.addObject(o); //adds the object to this list (l) of objects
    }
    catch(NumberFormatException nfe){
        System.out.println("Not a valid id!");
        addObject();
    }   
}

我的主要方法在 do-while-loop 中包含一个开关,用于添加、删除和编辑对象。

public static void main(String[] args){
    int choice; 
    do{ 
        try{
            choice = Integer.parseInt(sc.next());
            switch (choice){ 
                case 0: break; //ends the program
                case 1: addObject(); break; //starting static method to add an object with a name and an id

                //here are some more cases with similar static methods (left them out for simplicity)

                default: System.out.println("Not a valid choice!");break;
            }
        }
        catch(NumberFormatException nfe){
            System.out.println("Not a valid choice!");
            choice = -1; //to keep the loop running, and prevent another exception
        }

    }while (choice != 0);

System.out.println("Good bye!");

}

我的对象类

public class Object{

    private int id;
    private String name;

    public Object(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

我的ObjectList类

import java.util.*;

public class ObjectList {

    private List<Object> objects;

    public ObjectList() {
        objects = new ArrayList<Object>();
    }

    public void addObject(Object o){
        objects.add(d);
    }
}

当我尝试运行静态方法来添加对象时,它会很好地记录对象的 id,但是当我输入对象 id 时,它会返回到我的 main 方法,从头开始循环。 当我在开关中输入一个字符串(重新启动循环)时,它 react 得很好。 但我似乎无法正确添加对象。

这也是一个学校作业,他们给了我们所有这些代码(除了try-catch方法),并要求我们为静态方法和main方法编写一个try-catch。 我可能可以找到带有 if 子句的 main 方法的解决方法,但我想知道是否可以使用 try-catch 方法。

最佳答案

问题:

  • 使用扫描程序时,您必须了解它如何处理和不处理行尾标记,尤其是当您组合处理此标记的方法调用(例如 nextLine())和不处理该标记的方法调用(例如 nextInt())时。请注意,前者 nextLine() 会吞掉行尾 (EOL) 标记,而 nextInt() 和类似方法则不会。因此,如果您调用 nextInt() 并留下行尾标记缠结,则调用 nextLine() 将不会获取下一行,而是会吞掉悬空的 EOL 标记。一种解决方案是在调用 sc.nextInt() 之后立即调用 sc.nextLine() 以处理 EOL token 。或者在调用 sc.next() 的地方,将其更改为 sc.nextLine()
  • 不要使用递归(让 addObject() 方法调用自身),因为简单的 while 循环会更好、更干净、更安全。
  • 如果您确实有一个名为“Object”的类,请更改它,因为该名称与所有 Java 类的关键基类冲突。

例如,如果您有以下代码:

Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.print("Enter name: ");
String name = sc.nextLine();

您会发现名称始终为 "",这是因为 sc.nextLine() 吞掉了用户输入数字时留下的行尾 (EOL) 标记。解决这个问题的一种方法是:

Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
sc.nextLine();  // ***** to swallow the dangling EOL token
System.out.print("Enter name: ");
String name = sc.nextLine();

关于java - Try-Catch 中的静态方法未完全执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18345626/

相关文章:

java - 我用 Java 编写的 Java 客户端库出现 SSL 错误。遵循文档后请求仍然失败

Android + 威瑞信 SSL 版本 1

java - 如何处理现实项目中的运行时异常

php - 如何从 Eloquent 模型静态获取表名?

Java zip 程序生成损坏的 zip 文件

java - "T extends Junk"在 Java 的泛型类中意味着什么?

java - 如何使用套接字接收和存储文件

java - Sonar 违规 : "Method may fail to close stream on exception"

php从同一个类中的静态方法调用类方法但未实例化

c# - 如何用静态方法模拟?