java - 不兼容的操作数类型 String 和 char 语法错误

标签 java eclipse joptionpane

**太棒了,我用这个来纠正我的比较和 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();
    }}

最佳答案

检查字符串的性别比较:

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

您在这里使用裸标记,并且与字符串“M”“F”进行比较。

另请查看该代码,您会发现其中存在语法错误 - 在许多 if 语句中 - 以及一个空 block 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
{ ; }

关于java - 不兼容的操作数类型 String 和 char 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12762870/

相关文章:

java lucene类未找到问题

java - 使用 java 分析 Neo4j 数据库命中情况

c++ - 尽管构建成功,但 Eclipse Neon 构建错误

java - 我在 Netbeans 中有一个使用 JOptionPane.showOptionDialog 的游戏,但我不知道如何执行 if 语句

java - java中selenium rc驱动程序的代理

java - 分割一个没有与java中的正则表达式匹配的相邻字符的字符串

java - 如何以正确的方式对 "Export"进行编码?

c - 如何避免在由 Eclipse 运行的 C 应用程序中出现不可复制的字符

java - 如何在调度更多事件之前等待 JOptionPane 关闭

java - 带有可点击链接的 JOptionPane