我完全坚持如何将输出写入输出文件。我在下面提供了一些信息,希望能让自己更加清晰。感谢任何可以帮助我的人。
输入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();
}
}
}
}
}
答案 0 :(得分:0)
问题出在您的try
区块中。
<强>不正确:强>
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();
问题:您几乎必须使用System.out.println
outputfile.println
&#39}
<强>正确:强>
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();
outputfile.println("The reversed digits are: ");
reverse (input, userEntry);
outputfile.println();
System.out.println("The even digits are: ");
even (input, userEntry);
System.out.println();
outputfile.println("The even digits are: ");
even (input, userEntry);
outputfile.println();
System.out.println("The odd digits are: ");
odd (input, userEntry);
System.out.println();
outputfile.println("The odd digits are: ");
odd (input, userEntry);
outputfile.println();
答案 1 :(得分:0)
我测试了以下代码,该代码用于将反向编号写入文件。您可以将相同的技术应用于其他方法。注意我使用PrintWriter.print
而不是PrintWriter.println
来获取同一行上的所有数组元素。
public class Order {
PrintWriter outputfile;
FileWriter writer;
public 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 {
writer = new FileWriter("outDataFile.txt", true);
outputfile = new PrintWriter(writer);
outputfile.println("Your original number was: ");
outputfile.println(userEntry);
outputfile.flush();
System.out.println("Your original number was: ");
System.out.println(userEntry);
System.out.println("The reversed digits are: ");
outputfile.println("The reversed digits are: ");
reverse (input, userEntry, writer);
System.out.println();
} catch (IOException e){
}
}
//Reverses the order of digits.
public void reverse(Scanner keyboard, String intString, FileWriter writer) {
outputfile = new PrintWriter(writer);
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]);
outputfile.print(digits[i]);
outputfile.flush();
}
}
}