簡體   English   中英

不兼容的操作數類型字符串和字符語法錯誤

[英]Incompatible operand types String and char syntax error

**太棒了,我可以用它來糾正我的比較和IF語句,但是我現在正在控制台上打印文件中的信息。 沒有更多的錯誤代碼,但是控制台只說java.io.PrintWriter .....有關如何使用outputFile和導入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

我在做錯什么,為什么我總是收到語法錯誤? 我正在使用簡單的日食,我通常可以通過使用他們的調試器來發現我做錯了什么,但是我有點被卡住了。 請幫忙! 謝謝

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語句中-還有一個空塊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
{ ; }
if (gender.equals(F))

F在您的代碼中沒有變量。您應該在此處使用“ F”進行比較。

另外,您將if用作:- if age > 30 ..這不是正確的方法。.您需要在方括號中加上:- if (age > 30)

您說if (gender.equals(F)) ,但是F是什么? 那是您在某處定義的變量嗎? 我認為您更可能是指字符串“ F”。 if (maritalStatus.equals(M))相同if (maritalStatus.equals(M)) ,缺少並括了括號)

代碼有幾個問題。 我將嘗試對到目前為止所見的內容進行快速恢復(顯示每種情況的一個示例,其余相關問題類似),如果有更多發現,我將更新答案:

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語句。 只有一個else每個if ,認為它是避免孤立else ,因為沒有與之關聯的條件。 結構為:

if(condition) {

} else {

}

或者,如果if和/或else僅包含一行(我經常使用方括號,這比較困難。它可以使語句更清晰地定界,並提高恕我直言的可讀性。):

if(condition)
    //code
else
    //code

或者,嵌套語句:

if(condition) {

} else if(condition2) {

} else if(condition3) {

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM