我是Java的新手,我正在开发一个程序,该程序计算一个二进制数(以10为基数)并将每个有效条目保存在.txt文件中。问题在于,每个条目都被覆盖,因为保存的唯一条目是最后输入的条目。谁能指出我做错了什么?以及总体上改善语法的任何技巧。非常感谢。
import java.util.Scanner;
import java.io.*;
public class BinaryNum {
public static void main(String[] args) throws IOException //for file writing
{
Scanner keyboard = new Scanner(System.in);
String userEntry;// initial input by user for binary numbers
int ValidUserEntryNum;
int Decimal;
boolean decision = false; //bool to let user choose to continue
boolean bool; //variable to check if the valid string is a binary number
//loops for when the user names a choice
while(!decision)
{
//loops for when the user enters a binary number
do {
System.out.println("Please Enter a Binary Number: ");
userEntry = keyboard.nextLine();
//check to see if input is a string of numbers
userEntry = checkEntry (userEntry);
// convert string to int
ValidUserEntryNum = Integer.parseInt(userEntry);
//bool variable to see if the number is Binary
bool = CheckIsBinary (ValidUserEntryNum);
//check to see if the number is binary
if (!bool)
{
System.out.println("** Invalid.Input Must be a Binary number **");
}
} while(bool == false); //parameter for the loop (whether the number entered was binary)
//convert binary number to decimal number
Decimal = convert(ValidUserEntryNum);
//display on console
System.out.println("You Entered: " + ValidUserEntryNum);
System.out.println("It's base 10 equivilant is: " + Decimal);
System.out.println();
//creates the file name
File fileWR = new File("outDataFile.txt");
//creates the file object
fileWR.createNewFile();
BufferedWriter output = new BufferedWriter(new FileWriter(fileWR));
//to check if there is an existing file
if (fileWR.exists())
{
//writes in the file
output.write("You Entered: " + ValidUserEntryNum +"\r\n");
output.write("It's base 10 equivilant is " + Decimal +"\r\n");
output.close();
}
else //creates a new file if one doesnt already exist.
{
fileWR.createNewFile();
}
//option if the user wants to continue
System.out.println("Do you wish to continue?(yes or no):");
String st = keyboard.nextLine();
if (st.contentEquals("no"))
{
decision = true;
}
}
}
//to check if the user entered only a string of numbers (done)
public static String checkEntry (String userAnswer)
{
int UserLength = userAnswer.length();
int counter = 0;//to iterate through the string
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
while (UserLength == 0)
{
System.out.println("That is a blank");
System.out.println("Try again");
userAnswer = null;
userAnswer = keyboard.nextLine();
UserLength = userAnswer.length();
}
while (counter < UserLength)
{
if (!Character.isDigit(userAnswer.charAt(counter)))
{
System.out.println("That is not a binary number");
System.out.println("Try again");
userAnswer = null;
userAnswer = keyboard.nextLine();
UserLength = userAnswer.length();
counter = 0;
}
else
{
counter++;
}
while (UserLength == 0)
{
System.out.println("That is a blank, again");
System.out.println("Try again");
userAnswer = null;
userAnswer = keyboard.nextLine();
UserLength = userAnswer.length();
}
}
return userAnswer;
}
//method to check if the entered number is binary. (done)
public static boolean CheckIsBinary (int TrueBinary)
{
int temp;
while (TrueBinary > 0)
{
temp = (TrueBinary % 10);
if (temp != 1 && temp != 0)
{
return false;
}
TrueBinary = (TrueBinary/10);
}
return true;
}
//converts user binary to decimal
public static int convert(int ValidUserEntryNum)
{
//creating variables to convert binary to decimals
int temp = 0;
int Decimal = 0;
int power = 0;
//Convert the binary number to a decimal number
while (ValidUserEntryNum != 0)
{
temp = (ValidUserEntryNum % 10);
Decimal += temp * Math.pow(2, power++);
ValidUserEntryNum = (ValidUserEntryNum/10);
} return Decimal;
}
}
答案 0 :(得分:1)
每次在循环内创建一个新的FileWriter
和一个新的BufferedWriter
都是不必要的。您可以将其移出循环。
要使现有代码正常工作,请更改
new FileWriter(fileWR)
到
new FileWriter(fileWR, true)
传递的第二个参数是append
标志。来自javadocs(强调我的)
布尔值
true
,则数据将被写入文件的结尾而不是开头。
答案 1 :(得分:0)
似乎您具有fileWR.createNewFile();内部和外部检查。
//creates the file name
File fileWR = new File("outDataFile.txt");
//creates the file object
fileWR.createNewFile(); <--
BufferedWriter output = new BufferedWriter(new FileWriter(fileWR));
//to check if there is an existing file
if (fileWR.exists())
答案 2 :(得分:0)
更改此行:
BufferedWriter output = new BufferedWriter(new FileWriter(fileWR));
收件人:
BufferedWriter output = new BufferedWriter(new FileWriter(fileWR), true);
因为用于 FileWriter 的构造函数默认为覆盖。 http://tutorials.jenkov.com/java-io/filewriter.html#overwriting-vs-appending-the-file