Java try/catch 类构建参数的错误

发布于 2025-01-10 18:20:47 字数 963 浏览 0 评论 0原文

我对编码还很陌生,正在开发一个项目,其想法是查看 Excel 电子表格,然后创建一个类,为每行信息构建一个语句。我下面有以下代码。

public class Message {

// information from excel
String excelPath = "./data/RecruiterData.xlsx";
String sheetName = "Sheet1";
ExcelUtils excel = new ExcelUtils(excelPath, sheetName);
int excelRowCount = excel.getRowCount();


// build recruiter class
public static Recruiter[] buildRecruiter = {
    try {
    // this needs to be a for each cell filled in excel, create an instance of recruiter
    for (int i = 0; i < excelRowCount; i++) {
        new Recruiter(excel.getCellData(1, i), excel.getCellData(2, i), excel.getCellData(3, i), excel.getCellData(4, i));
    }
} catch (Exception e) {
    System.out.println("Something went wrong");

   }
}

但它却给了我一堆错误。第一个错误出现在 public static Recruiter[] 中,在该行的末尾,它显示:期望 } 期望。

接下来,我的 try catch 语句有错误,只是说意外的标记。

最后,它为新 Recruiter 行中的每个参数提供一个错误,指出未处理的异常:java.io.IOException。我在网上寻求帮助没有成功,所以我想我会询问一下。太感谢了!

I am still pretty new to coding and am working on a project where the idea is to look over an excel spreadsheet and put then create a class that builds a statement for each row's information. I have the following code below.

public class Message {

// information from excel
String excelPath = "./data/RecruiterData.xlsx";
String sheetName = "Sheet1";
ExcelUtils excel = new ExcelUtils(excelPath, sheetName);
int excelRowCount = excel.getRowCount();


// build recruiter class
public static Recruiter[] buildRecruiter = {
    try {
    // this needs to be a for each cell filled in excel, create an instance of recruiter
    for (int i = 0; i < excelRowCount; i++) {
        new Recruiter(excel.getCellData(1, i), excel.getCellData(2, i), excel.getCellData(3, i), excel.getCellData(4, i));
    }
} catch (Exception e) {
    System.out.println("Something went wrong");

   }
}

But yet it is giving me a bunch of errors. The first error is in the public static Recruiter[] where right at the end of that line, it says ; expect } expected.

Next, there are errors with my try catch statement just saying unexpected token.

Finally, it is giving each parameter in the new Recruiter line an error saying unhandled exception: java.io.IOException. I was not sucessful trying to find help online so I thought I would ask about it. Thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲鸠 2025-01-17 18:20:47

嗯,我认为你的错误是在方法声明中

   public class Message {

// information from excel
static String excelPath = "./data/RecruiterData.xlsx";
static String sheetName = "Sheet1";
static ExcelUtils excel = new ExcelUtils(excelPath, sheetName);
static int excelRowCount = excel.getRowCount();


// build recruiter class
public static Recruiter[] buildRecruiter () {
    //in java methods are declarated for that way [scope] [instance] data_type name ([params]) { [body; return data_type]}
    //next if you declare static your function all fields must be static too or change te method declaration to public or private 
    try {
    // this needs to be a for each cell filled in excel, create an instance of recruiter
    for (int i = 0; i < excelRowCount; i++) {
        new Recruiter(excel.getCellData(1, i), excel.getCellData(2, i), excel.getCellData(3, i), excel.getCellData(4, i));
    }
} catch (Exception e) {
    System.out.println("Something went wrong");

   }
}
}

mmmm i think your error is in the method declaration

   public class Message {

// information from excel
static String excelPath = "./data/RecruiterData.xlsx";
static String sheetName = "Sheet1";
static ExcelUtils excel = new ExcelUtils(excelPath, sheetName);
static int excelRowCount = excel.getRowCount();


// build recruiter class
public static Recruiter[] buildRecruiter () {
    //in java methods are declarated for that way [scope] [instance] data_type name ([params]) { [body; return data_type]}
    //next if you declare static your function all fields must be static too or change te method declaration to public or private 
    try {
    // this needs to be a for each cell filled in excel, create an instance of recruiter
    for (int i = 0; i < excelRowCount; i++) {
        new Recruiter(excel.getCellData(1, i), excel.getCellData(2, i), excel.getCellData(3, i), excel.getCellData(4, i));
    }
} catch (Exception e) {
    System.out.println("Something went wrong");

   }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文