**太棒了,我得到这个来纠正我的比较和我的 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();
}}