0

**太棒了,我得到这个来纠正我的比较和我的 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,并按以下顺序包含三行信息:姓名、性别、年龄、婚姻状况 例如:Mick Jagger 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();
    }}
4

4 回答 4

3

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

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

您在这里使用裸标记,而不是与字符串比较"M", 和"F".

还要查看该代码,那里有一个语法错误 - 并且在你的许多if语句中 - 和一个空块 if gender.equals("F"),并且你将多个else语句串在一起,而不是使用 intermediate else if

这是不正确的:

if (test)
{

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

对于三个分支,您需要一个干预if

if (test)
{

}
else if (someOtherTest)
{

}
else // ok, one final else
{ ; }
于 2012-10-06T19:02:06.687 回答
0
if (gender.equals(F))

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

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

于 2012-10-06T19:03:04.760 回答
0

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

于 2012-10-06T19:03:42.920 回答
0

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

gender.equals(F)

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

if age > 30

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

eligibleBachelorsReport += ""firstName + ""lastName

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

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

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

singleMen = singleMen ++

本身不是问题,但没有必要。就做singleMen++which is same than singleMen = singleMen + 1or also singleMen += 1

最后一点,检查你的if陈述。elseper只有一个if,将其视为避免孤儿else,因为没有与它们相关的条件。结构是:

if(condition) {

} else {

}

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

if(condition)
    //code
else
    //code

或者,嵌套语句:

if(condition) {

} else if(condition2) {

} else if(condition3) {

}
于 2012-10-06T19:15:30.140 回答