if .. else语句中的语法错误?

时间:2022-11-11 21:01:52

I am getting a syntax error for this code on the "else" and the "c"s at the bottom and I'm not sure why.

我在“else”和底部的“c”处获得此代码的语法错误,我不知道为什么。

    public static void menuOptionSix(){
        ArrayList<Customer> list = generateFromFile("Week3ProgrammingDataUpdate.csv");
        Comparator<Customer> comparatorForSortingByEmployerGroupByDrugs = new Comparator<Customer>() {
            public int compare(Customer c1, Customer c2) {
                return c1.getEmployer().compareToIgnoreCase(c2.getEmployer());
            }
        };
        Collections.sort(list, comparatorForSortingByEmployerGroupByDrugs);
        for (Customer c : list){
            if (c.getEmployer().equalsIgnoreCase(c.getEmployer()))
                System.out.println(c.getFavoriteDrugs());
            } else {
                System.out.println(c.getEmployer() + " " + c.getFavoriteDrugs());
            }
    }

4 个解决方案

#1


2  

You are missing an opening brace on your if statement, so the closing brace before the else is not matched properly.

你在if语句中缺少一个左括号,所以else之前的右括号没有正确匹配。

//                                                     v---Insert here
if (c.getEmployer().equalsIgnoreCase(c.getEmployer())) {

#2


0  

You forgot to open if block with {

你忘了打开if if {

                                                      |here
if (c.getEmployer().equalsIgnoreCase(c.getEmployer())){
    System.out.println(c.getFavoriteDrugs());
}

#3


0  

This statement

if (c.getEmployer().equalsIgnoreCase(c.getEmployer()))

is missing a {.

缺少一个{。

And also, as Brent Robinson as pointed out, your for loop is missing the closing bracket.

而且,正如Brent Robinson所指出的那样,你的for循环错过了结束括号。

#4


0  

You are trying to use Short form of if statement, but using that with else , you Should to use {,} for if statement or use ternary operator instead of if else.

您正在尝试使用Short形式的if语句,但是使用else语句,您应该使用{,}来表示if语句或使用三元运算符而不是if else。

if (c.getEmployer().equalsIgnoreCase(c.getEmployer())){
            System.out.println(c.getFavoriteDrugs());
         }   //if closed
         else {
            System.out.println(c.getEmployer() + " " + c.getFavoriteDrugs());
        } // else closed

Here in this Question you can learn about how to use short form of if Statement properly (Ternary operator)

在本课题中,您可以了解如何正确使用if语句的简短形式(三元运算符)

#1


2  

You are missing an opening brace on your if statement, so the closing brace before the else is not matched properly.

你在if语句中缺少一个左括号,所以else之前的右括号没有正确匹配。

//                                                     v---Insert here
if (c.getEmployer().equalsIgnoreCase(c.getEmployer())) {

#2


0  

You forgot to open if block with {

你忘了打开if if {

                                                      |here
if (c.getEmployer().equalsIgnoreCase(c.getEmployer())){
    System.out.println(c.getFavoriteDrugs());
}

#3


0  

This statement

if (c.getEmployer().equalsIgnoreCase(c.getEmployer()))

is missing a {.

缺少一个{。

And also, as Brent Robinson as pointed out, your for loop is missing the closing bracket.

而且,正如Brent Robinson所指出的那样,你的for循环错过了结束括号。

#4


0  

You are trying to use Short form of if statement, but using that with else , you Should to use {,} for if statement or use ternary operator instead of if else.

您正在尝试使用Short形式的if语句,但是使用else语句,您应该使用{,}来表示if语句或使用三元运算符而不是if else。

if (c.getEmployer().equalsIgnoreCase(c.getEmployer())){
            System.out.println(c.getFavoriteDrugs());
         }   //if closed
         else {
            System.out.println(c.getEmployer() + " " + c.getFavoriteDrugs());
        } // else closed

Here in this Question you can learn about how to use short form of if Statement properly (Ternary operator)

在本课题中,您可以了解如何正确使用if语句的简短形式(三元运算符)