2012-10-06 89 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) { 

} 
相关问题