【问题标题】:Trying to call class/method into main试图将类/方法调用到 main
【发布时间】:2019-04-09 10:06:13
【问题描述】:

我试图在我的主类中运行这个类的内容,但是我想不出一种方法来做到这一点。我尝试了从导入 package.class 和创建实例到其他选项的不同选项。这是否与此类的构造方式有关。如果这是不可能的,我只是不想继续旋转我的轮子。非常感谢这个问题的逻辑。

package stockapplication;


import java.util.Scanner;
import java.io.*;
import java.util.*;

public class Database {

public int empNum;
public String empFirst;
public String empLast;
public int empSal;

public Database(int c, String a, String b, int s) {
    empNum = c;
    empFirst = a;
    empLast = b;
    empSal = s;
}

public int getEmpNum() {
    return empNum;
}

public String getEmpFirst() {
    return empFirst;
}

public String getEmpLast() {
    return empLast;
}

public int getSalary() {
    return empSal;

}

public static void Stock() {

    Database[] array = new Database[3];
    Scanner sc = new Scanner(System.in);
    for (int i = 0; i < 3; i++) {
        System.out.printf("Please enter your employee number:");
        int c = sc.nextInt();
        System.out.printf("Please enter employee first name:");
        String a = sc.next();
        System.out.printf("Please enter employee last name:");
        String b = sc.next();
        System.out.printf("Please enter your salary:");
        int s = sc.nextInt();
        array[i] = new Database(c, a, b, s);

        try {
            File f = new File("Database1.txt");
            PrintWriter pw = new PrintWriter(new FileOutputStream(f, true));
            pw.append("\n" + c + "," + a + "," + b + "," + s + ",");
            pw.close();
        } catch (Exception e) {
        }
    }
}
}

【问题讨论】:

  • catch (Exception e) { } 绝不会默默吞下异常——也许那里有一些非常重要的信息?
  • 另外,您没有在此代码中显示main 方法
  • 需要调用构造函数来创建这个类的实例:Database db = new Database(0, "foo", "bar", 1);
  • @ScaryWombat 我的主要课程在 Netbeans 的不同选项卡中。我找到了这段代码并对其进行了调整以适应我的目的。本来里面有一个main方法,但是我把它拿出来了。我想将此代码作为更大项目的一部分带入我的主要方法中。我最终试图创建一个可以让用户管理的股票文本数据库。也许我需要另一种方式来构建代码。我还没有更改股票信息,只是想看看我是否可以在 main 方法中使用它。我不得不对异常 e 进行自我教育,发现它被认为是不好的做法。
  • 您面临哪些错误/问题?

标签: java class methods call


【解决方案1】:

添加另一个类,您将在其中添加逻辑并将数据库对象保留为模型。然后,从您的 Main 调用这个新类并运行它的方法。

public class ServiceDB {

    public ServiceDB() {
        //Empty
    }

    public void writeDB() {
        Database[] array = new Database[3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.printf("Please enter your employee number:");
            int c = sc.nextInt();
            System.out.printf("Please enter employee first name:");
            String a = sc.next();
            System.out.printf("Please enter employee last name:");
            String b = sc.next();
            System.out.printf("Please enter your salary:");
            int s = sc.nextInt();
            array[i] = new Database(c, a, b, s);
            try {
                File f = new File("Database1.txt");
                PrintWriter pw = new PrintWriter(new FileOutputStream(f, true));
                pw.append("\n" + c + "," + a + "," + b + "," + s + ",");
                pw.close();
            } catch (Exception e) {
                // Never a good idea to leave it empty, at least print the error.
                e.printStackTrace();
            }
        }
    }

}

主要

public static void main(String[] args) {
    ServiceDB serviceDB = new ServiceDB();
    serviceDB.writeDB();
}

当然,我跳过了导入。如果您需要我添加它们,请告诉我。

【讨论】:

    猜你喜欢
    • 2014-04-29
    • 2020-05-27
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多