我完全停留在如何將輸出寫入輸出文件。我在下面提供了一些信息,希望能讓我更清楚。感謝任何能夠幫助我的人。如何將數組寫入文件?
當1234是輸入(我需要這被寫入到txt文件)輸出:
Your original number was:
1234
The reversed digits are:
4321
The even digits are:
2 4
The odd digits are:
1 3
什麼目前正在寫入TXT文件:
Your original number was:
1234
這裏是我的代碼現在:
import java.util.Scanner;
import java.io.*;
public class order {
//Input validation. Allows any integer greater than 0. Any number starting
with 0 is invalid.
private static void validate(Scanner input) {
String userEntry;
System.out.println("Please enter a positive integer greater than 0.");
userEntry = input.nextLine();
while (!userEntry.matches("^[1-9][0-9]*$")) {
System.out.println("Invalid number. Try again");
userEntry = input.nextLine();
}
try {
FileWriter writer = new FileWriter("outDataFile.txt", true);
PrintWriter outputfile = new PrintWriter(writer);
outputfile.println("Your original number was: ");
outputfile.println(userEntry);
System.out.println("Your original number was: ");
System.out.println(userEntry);
System.out.println("The reversed digits are: ");
reverse (input, userEntry);
System.out.println();
System.out.println("The even digits are: ");
even (input, userEntry);
System.out.println();
System.out.println("The odd digits are: ");
odd (input, userEntry);
System.out.println();
} catch (IOException e){
}
}
//Reverses the order of digits.
public static void reverse(Scanner keyboard, String intString) {
int strsize = intString.length();
int[] digits = new int[strsize];
for (int i = 0; i < strsize; ++i)
{
String temp = Character.toString(intString.charAt(i));
digits[i] = Integer.parseInt(temp);
}
for (int i = strsize - 1; i >= 0; --i)
{
System.out.print(digits[i]);
}
}
//Extracts even numbers.
public static void even (Scanner keyboard2, String intString2) {
int strsize2 = intString2.length();
int[] digits2 = new int[strsize2];
for (int i = 0 ; i < strsize2 ; ++i) {
String temp = Character.toString(intString2.charAt(i));
digits2[i] = Integer.parseInt(temp);
if(digits2[i] % 2 == 0) {
System.out.print(digits2[i] + " ");
}
}
}
//Extracts odd numbers.
public static void odd (Scanner keyboard3, String intString3) {
int strsize3 = intString3.length();
int[] digits3 = new int[strsize3];
for (int i = 0 ; i < strsize3 ; ++i) {
String temp = Character.toString(intString3.charAt(i));
digits3[i] = Integer.parseInt(temp);
if(digits3[i] % 2 != 0) {
System.out.print(digits3[i] + " ");
}
}
}
//Allows continuous input.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean validate = true;
while (validate) {
validate(keyboard);
boolean invalid = true;
while (invalid) {
System.out.println("Would you like to try another calculation? [y/n]");
Scanner input = new Scanner(System.in);
char answer;
String option = input.next();
if ("y".equalsIgnoreCase(option)) {
invalid = false;
}
else if ("n".equalsIgnoreCase(option)) {
invalid = false;
validate = false;
System.out.println("Goodbye.");
input.close();
}
else {
System.err.println("The only valid answers are y/n.");
System.out.println();
}
}
}
}
}
您從不使用您的'PrintWriter'來打印除原始數字之外的任何東西。因此,這就是您在輸出文件中看到的所有內容。 –
歡迎來到Stack Overflow!我會想象這95%的代碼與你的問題無關。請創建一個[**最小**,完整且可驗證的示例](http://stackoverflow.com/help/mcve),以說明您的問題。 –
來自'System.out'的輸出是你期望的嗎?如果是這樣,爲什麼不把你的'PrintWriter'傳遞給其他方法,並以與第一行相同的方式寫入? – petehallw