【发布时间】:2012-10-06 18:59:42
【问题描述】:
**太棒了,我得到这个来纠正我的比较和我的 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();
}}
【问题讨论】:
-
米克·贾格尔 22 岁,单身!?
-
@NullUserException 我正要标记评论,然后我看到了版主符号。
-
@LuiggiMendoza 随意标记来自模组的 cmets,就像标记任何其他用户一样。
-
好的,我想我明白了,所以现在我已经更正了我使用 .equals()) 比较字符串的方式,并且我在 () 中的 IF 语句现在程序运行正常但是我似乎无法让我的控制台打印正确的信息。它会弹出,所以我没有收到任何错误,但控制台只是说“java.io.PrintWriter”,而不是在文件中重新发布报告。关于如何使用的任何指针 :import javax.swing.JOptionPane;
-
lol @ NullUserException--根据我的 java 教授他是...玛莎斯图尔特也是 23 岁,已婚。
标签: java eclipse joptionpane