我想將角色的名稱和性別導出到文本文件。另外,我需要在Mac和PC電腦上導出。我嘗試了各種方法,但都沒有爲我工作,或者我可能錯誤地放置了它們。將數據從Java導出到文本文件
我改變了我的代碼和我嘗試:
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.util.Scanner;
public class Character {
public static void main (String[] args) {
String characterGender;
String characterName;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Ricardo's character creator!");
System.out.println("Is your character a boy or a girl?");
characterGender = scan.nextLine();
if (characterGender.equals("girl")) {
System.out.println("Awesome! Please enter her name: ");
characterName = scan.nextLine();
} else {
System.out.println("Awesome! Please enter his name: ");
characterName = scan.nextLine();
}
System.out.print("Alright! " + characterName + " has been created!");
PrintWriter out = new PrintWriter("Character.txt");
out.println(characterName);
out.println(characterGender);
out.close();
}
}
我得到這樣的錯誤:
Character.java:34: error: unreported exception FileNotFoundException; must be caught or declared to be thrown.
這仍然會遇到OP的問題,這是沒有處理異常。它只是一個IOException,而不是FileNotFoundException。 – Grice 2014-10-20 20:00:18