【问题标题】:Incompatible operand types String and char syntax error不兼容的操作数类型 String 和 char 语法错误
【发布时间】:2012-10-06 18:59:42
【问题描述】:

**太棒了,我得到这个来纠正我的比较和我的 IF 语句,但我现在正在控制台上打印文件上的信息。没有更多错误代码,但是控制台只说 java.io.PrintWriter .....关于如何使用 outputFile 和 import javax.swing.JOptionPane 的任何指针?更正下面的代码

import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

public class StudentFile {
     public static void main(String[] args) throws Exception {
        // Declare and initialize variables
        String studentSummary = "Student Summary Report";
        String eligibleBachelorsReport = "Eligible Bachelors Report";

        // input record
        String firstName;
        String lastName;
        String gender;
        int age;
        String maritalStatus;

        // counters
        int marriedMen = 0;
        int singleMen = 0;
        int marriedWomen = 0;
        int singleWomen = 0;

        // create studentInput to read from StudentFile.txt
        File inputFile = new File("StudentFile.txt");
        Scanner input = new Scanner(inputFile);

        while (input.hasNext()) {
            // read name, gender, age, maritalStatus
            firstName = input.next();
            lastName = input.next();
            gender = input.next();
            age = input.nextInt();
            maritalStatus = input.next();


            if (gender.equals("F"))
            {
                if(maritalStatus.equals("M"))
                {
                marriedWomen = marriedWomen ++;
                }

                else {
                   singleWomen = singleWomen ++;
                 }
            }
            else if (maritalStatus.equals("M")){
                marriedMen = marriedMen++;
            }else{
                singleMen = singleMen++;
            }

                if( age > 30) {
                    eligibleBachelorsReport += " "+firstName + " "+lastName;
     }
            System.out.println(firstName + " " + lastName + " " + gender + " " + age + " "
                    + maritalStatus);
        }

        // write studentSummary, eligibleBachelorsReport to StudentReport.txt
        PrintWriter outputFile = new PrintWriter("StudentReport.txt");
        outputFile.println(studentSummary);
        outputFile.println(eligibleBachelorsReport);

        // write studentSummary, eligibleBachelorsReport to the console
        System.out.println(studentSummary);
        System.out.println(eligibleBachelorsReport);
        JOptionPane.showMessageDialog(null, outputFile);

        input.close();
        outputFile.close();
     }}

我正在尝试让这段代码正常工作,但我似乎无法弄清楚我做错了什么。我知道我正在尝试比较我已导入/附加到该程序的此文本文件中的字符串数据,但它似乎无法通过。

我的代码如下,我导入的txt文件标记为StudentFile.txt,三行信息按以下顺序排列:姓名、性别、年龄、婚姻状况 例如:米克·贾格尔 M 22 S

我做错了什么,为什么我不断收到语法错误?我正在使用简单的eclipse,我通常可以通过使用他们的调试器找到我做错了什么,但我有点卡住了。请帮忙!谢谢

import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

public class StudentFile {
    public static void main(String[] args) throws Exception {
        // Declare and initialize variables
        String studentSummary = "Student Summary Report";
        String eligibleBachelorsReport = "Eligible Bachelors Report";

        // input record
        String firstName;
        String lastName;
        String gender;
        int age;
        String maritalStatus;

        // counters
        int marriedMen = 0;
        int singleMen = 0;
        int marriedWomen = 0;
        int singleWomen = 0;

        // create studentInput to read from StudentFile.txt
        File inputFile = new File("StudentFile.txt");
        Scanner input = new Scanner(inputFile);

        while (input.hasNext()) {
            // read name, gender, age, maritalStatus
            firstName = input.next();
            lastName = input.next();
            gender = input.next();
            age = input.nextInt();
            maritalStatus = input.next();


            if (gender.equals(F)){
            }if maritalStatus.equals(M)){
                marriedWomen = marriedWomen ++;
                } else {
                singleWomen = singleWomen ++;
            }else{
                }if maritalStatus.equals(M)){
                marriedMen = marriedMen ++
            }else{
                singleMen = singleMen ++
                if age > 30 {
                    eligibleBachelorsReport += ""firstName + ""lastName
    }
            System.out.println(firstName + " " + lastName + " " + gender + " " + age + " "
                    + maritalStatus);
        }

        // write studentSummary, eligibleBachelorsReport to StudentReport.txt
        PrintWriter outputFile = new PrintWriter("StudentReport.txt");
        outputFile.println(studentSummary);
        outputFile.println(eligibleBachelorsReport);

        // write studentSummary, eligibleBachelorsReport to the console
        System.out.println(studentSummary);
        System.out.println(eligibleBachelorsReport);

        input.close();
        outputFile.close();
    }}

【问题讨论】:

  • 米克·贾格尔 22 岁,单身!?
  • @NullUserException 我正要标记评论,然后我看到了版主符号。
  • @LuiggiMendoza 随意标记来自模组的 cmets,就像标记任何其他用户一样。
  • 好的,我想我明白了,所以现在我已经更正了我使用 .equals()) 比较字符串的方式,并且我在 () 中的 IF 语句现在程序运行正常但是我似乎无法让我的控制台打印正确的信息。它会弹出,所以我没有收到任何错误,但控制台只是说“java.io.PrintWriter”,而不是在文件中重新发布报告。关于如何使用的任何指针 :import javax.swing.JOptionPane;
  • lol @ NullUserException--根据我的 java 教授他是...玛莎斯图尔特也是 23 岁,已婚。

标签: java eclipse joptionpane


【解决方案1】:

查看您的字符串比较的性别:

gender.equals(F)){
        }if maritalStatus.equals(M)){

您在此处使用裸令牌,不是与字符串 "M""F" 进行比较。

同时查看该代码,你有一个语法错误 - 并且在你的许多 if 语句中 - 如果 gender.equals("F") 是一个空块,并且你将多个 else 语句串在一起,而不是使用中级else if

这是不正确的:

if (test)
{

}
else
{ ; }
else // wrong, can only have one final else
{ ; }

对于三个分支,你需要一个介入的if

if (test)
{

}
else if (someOtherTest)
{

}
else // ok, one final else
{ ; }

【讨论】:

    【解决方案2】:
    if (gender.equals(F))
    

    F 在您的代码中没有变量。您应该在此处使用“F”进行比较..

    此外,您将 if 用作:-if age > 30.. 这不是正确的方法.. 您需要将括号括起来:-if (age > 30)

    【讨论】:

      【解决方案3】:

      你说if (gender.equals(F)),但F是什么?这是您在某处定义的变量吗?我认为您的意思更可能是字符串“F”。 if (maritalStatus.equals(M)) 也一样(顺便说一句,缺少左括号)

      【讨论】:

        【解决方案4】:

        代码有几个问题。我将尝试对到目前为止所看到的内容进行快速恢复(显示每个案例的单个示例,其余相关问题类似),如果我发现更多,我将更新答案:

        gender.equals(F)
        

        什么是 F?,它不是一个定义的变量,也不是一个有效的值。如果要与字符串进行比较,则需要使用引号,例如 "F"

        if age > 30
        

        大多数语句需要() 来界定您将检查的内容。 if 语句就是其中之一,要求它类似于 if(age > 30)。另外,请记住,如果您的 if 包含多行代码,则需要使用方括号。

        eligibleBachelorsReport += ""firstName + ""lastName
        

        没有必要在 String 变量之前使用引号来连接它们。不过,如果您想在字符串之间添加一个空格,请执行eligibleBachelorsReport += " " + firstName + " " + lastName + " - "。最后的- 只是一个建议。请记住,如果您要将很多东西连接在一起。另外,如果需要插入换行符,可以使用\n

        eligibleBachelorsReport += ""firstName + ""lastName
        marriedMen = marriedMen ++
        

        除其他外,这些行的末尾没有分号。

        singleMen = singleMen ++
        

        本身不是问题,但没有必要。只需执行singleMen++,它与singleMen = singleMen + 1singleMen += 1 相同。

        最后,请检查您的 if 声明。每个if 只有一个else,将其视为避免孤儿else,因为没有与它们相关的条件。结构是:

        if(condition) {
        
        } else {
        
        }
        

        或者,如果if 和/或else 只包含一行(我总是使用括号。它允许更清晰地分隔语句并提高可读性恕我直言。):

        if(condition)
            //code
        else
            //code
        

        或者,嵌套语句:

        if(condition) {
        
        } else if(condition2) {
        
        } else if(condition3) {
        
        }
        

        【讨论】:

          猜你喜欢
          • 2013-01-10
          • 2011-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多