2012-10-06 91 views
0

**真棒傢伙我得到這個工作糾正我的比較和我的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

我在做什麼錯了,爲什麼我一直收到語法錯誤?我使用簡單的eclipse,通常我可以通過使用它們的調試器來發現我做了什麼錯誤,但我有點卡住了。請幫忙! THX

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

米克賈格爾是22和單!? – NullUserException

+0

@NullUserException我正要標記註釋,然後我看到了版主符號。 –

+1

@LuiggiMendoza隨意標記來自mods的評論,就像你會標記任何其他用戶一樣。 – NullUserException

回答

3

審查性別的字符串比較:

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 
{ ; } 
0
if (gender.equals(F)) 

F是在你的代碼中沒有變量。您應該使用 「F」這裏來比較..

另外,你正在使用你的如果作爲: - if age > 30 ..這是不正確的方法.. 你需要放在括號內: - if (age > 30)

0

你說if (gender.equals(F)),但什麼是F?這是你在某處定義的變量嗎?我認爲你的意思是字符串「F」的可能性更大。與if (maritalStatus.equals(M))相同(順便說一句缺失和左括號)

0

代碼有幾個問題。我會嘗試做一個簡短的簡歷(顯示每個案例的一個例子,其餘相關問題都是類似的),目前爲止我已經看到了,如果我發現更多,我會更新答案:

gender.equals(F) 

什麼是F ?,它不是一個已定義的變量,也不是一個有效的值。如果你想比較一個字符串,你需要使用引號,如"F"

if age > 30 

大多數聲明需要()來劃定您要檢查的內容。 if聲明就是其中之一,要求它與if(age > 30)一樣。另外,請記住,如果if包含多行代碼,則需要使用括號。

eligibleBachelorsReport += ""firstName + ""lastName 

有沒有必要使用引號String變量之前將它們串聯。儘管如此,ig你想在字符串之間添加一個空格,請執行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) { 

} 
相關問題