**真棒傢伙我得到這個工作糾正我的比較和我的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();
}}
米克賈格爾是22和單!? – NullUserException
@NullUserException我正要標記註釋,然後我看到了版主符號。 –
@LuiggiMendoza隨意標記來自mods的評論,就像你會標記任何其他用戶一樣。 – NullUserException