This question already has answers here:
Write to text file without overwriting in Java
(9 answers)
Closed 3 years ago.
I am new to java and I am working on a program that calculates a binary number to it's base 10 equivalent and saves each valid entry on a .txt file. Problem is that each entry is being overwritten that the only one that's saved is that last one entered. Can anyone point out what i'm doing wrong? and any tips on improving the syntax in general. Much appreciated.
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;
}
}
You are creating a new FileWriter and a new BufferedWriter each time inside the loop which is not necessary. You can move it outside the loop.
To make your existing code work, change
new FileWriter(fileWR)
to
new FileWriter(fileWR, true)
The second parameter passed is the append flag. From javadocs (emphasis mine)
boolean if true, then data will be written to the end of the file rather than the beginning.
It looks like you have fileWR.createNewFile(); both inside and outside the check.
//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())
Change this line:
BufferedWriter output = new BufferedWriter(new FileWriter(fileWR));
To:
BufferedWriter output = new BufferedWriter(new FileWriter(fileWR), true);
Because the constructor you used for FileWriter defaults to overwriting.
http://tutorials.jenkov.com/java-io/filewriter.html#overwriting-vs-appending-the-file
Related
like in the title , I'm stuck with this error for a while .I get the value from the file normally but when I try to convert it the error poops out. I read many topics ,but couldn't find any similar case to mine(with file) or any good tips. I tried adding an assert ,but it didn't help. The full description of error is :
Exception in thread "main" java.lang.NumberFormatException: null
at java.base/java.lang.Integer.parseInt(Integer.java:620)
at java.base/java.lang.Integer.parseInt(Integer.java:776)
at EnergyMeasure_needs_to_be_completed.main(EnergyMeasure_needs_to_be_completed.java:85)
Also I'm beginner (but I guess you already know that heh ;))
import java.util.Scanner;
import java.io.*;
public class EnergyMeasure_needs_to_be_completed {
public static void main(String[] args) throws IOException {
//int work_of_energy;
Scanner input = new Scanner(System.in);
System.out.println("\t\t\t\t Hi , this program will count how many kWh you're using");
//asks about number of devices
System.out.println("First of all, how many the same devices do you have in your house ?");
int devices = input.nextInt();
boolean bool = false;
do {
if (devices < 0) {
System.out.println("You can't have less than 0 devices in your home!\nMake input once again :");
devices = input.nextInt();
} else {
System.out.println("Okay, so you've got " + devices + " same devices.");
bool = true;
break;
}
}while(bool = true);
//asks about time of use
System.out.println("\nHow many hours you use them per day?");
int time_use = input.nextInt();
do {
if (time_use > 24 || time_use < 0) {
System.out.println("Wrong!\nMake input once again :");
time_use = input.nextInt();
}
else{
System.out.println("You use your devices for " + time_use + "h");
bool = true;
break;
}
}while(bool = true);
/*else if(!input.hasNextInt()){
System.out.println("Invalid input! \nEnter an integer : ");
time_use = input.nextInt();
} */
//downloads value of power from file
String power_dev; //path to the file
power_dev = null; //reference to one line at a time
try {
FileReader fileReader = //reads text files in the default encoding
new FileReader("power_monitors");
BufferedReader bufferedReader = //deal with a line at a time
new BufferedReader(fileReader);
while((power_dev = bufferedReader.readLine()) != null) {
System.out.println("\nThe power of your devices is " + power_dev + "W");
}
bufferedReader.close(); //close file
}
catch (FileNotFoundException e) { //if file doesn't exist catch the except
System.out.println("Unable to open file");
}
//assert power_dev != null;
int power_dec = Integer.parseInt(power_dev); //change the String ,to Integer
int power_of_devices = power_dec * devices; //summary devices power
//count the cost of work (W = P * t) [kWh]
int work_of_energy = (power_of_devices / 1000) * time_use;
System.out.println("The work of energy equals : " + work_of_energy);
}
}
If you print power_dev, what do you get? What format is it? Because the readLine() returns a textual line, so depending on the source you are reading from, you might get more than an int.
Why not use the read() method? It returns an int, so you wouldn't have to parse power_dev.
Again, hard to answer your question without seeing the file or having a reproductible code, but my best guess is that power_dev returns null or something that can't be parsed by Integer.parseInt() method.
I'm taking my very first java class. I need to ask for a zip code. I know how to ask for new input if they don't enter 5 digits, but how do I also ask for new input if they enter a non-integer?
Here is what I have:
import java.util.Scanner;
public class AndrewDemographics {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int zip; // 5 digit zip
System.out.print("Enter your 5 digit zip code: ");
zip = stdIn.nextInt();
while ((zip < 10000) || (zip > 99999)) {
// error message
System.out.println("Invalid Zip Code format.");
System.out.println("");
System.out.println("Enter your 5 digit zip code: ");
zip = stdIn.nextInt();
} //end if zip code is valid
}
}
To support zip codes starting with 0, you need to store the zip code in a String, and then it's easiest to validate it using a regex:
Scanner stdIn = new Scanner(System.in);
String zip;
do {
System.out.print("Enter your 5 digit zip code: ");
zip = stdIn.next();
} while (! zip.matches("[0-9]{5}"));
If you want to print error message, you can do it like this, which uses nextLine() so simply pressing enter will print error message too:
Scanner stdIn = new Scanner(System.in);
String zip;
for (;;) {
System.out.print("Enter your 5 digit zip code: ");
zip = stdIn.nextLine().trim();
if (zip.matches("[0-9]{5}"))
break;
System.out.println("Invalid Zip Code format.");
System.out.println();
}
As the comment suggests, you will need to take into account zip code starting with zero. I guess for that, you'll need to consider the input as a String:
check if the String is 5 characters long (to match the 5 digits)
String does not contain + sign as +1234 would work
check if the String is a valid integer
check if the Integer is positive as -1234 would be still valid
you now have something between 00000 and 99999
In practice
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
String userInput;
int zipCode = -1;
// flag to stop spamming the user
boolean isValid = false;
while (!isValid) {
// ask the user
System.out.print("Enter your 5 digit zip code: ");
userInput = stdIn.next();
// it should be 5 digits so 5 charaters long:
if (userInput.length() == 5 && !userInput.contains("+")) {
try {
zipCode = Integer.parseInt(userInput);
if (zipCode > 0) {
isValid = true;
}
}
catch (NumberFormatException e) {
// do nothing
}
}
System.out.println("Zip code is invalid!");
}
System.out.println("You have selected the zip code: " + zipCode);
}
There is an issue with zip codes with leading zeros in previous. There needs to be a check if both is a number and is 5 characters in length. A zero leading zip would be 4 digits in length if read in as a number type.
Top of my head:
String zip = null;
do {
zip = stdIn.next();
try {
Integer.parseInt(zip); // this will throw exception if not a number
} catch (NumberFormatException e) {
continue; // this will start the next loop iteration if not a number
}
} while (zip.length() != 5); // this will start the next iteration if not 5 characters
I took the input as a String using nextLine() rather than an int because it accounts for zip codes starting with 0, and a zip code, although written numerically, isn't really a numerical value. I felt that the easiest way to structure the if/else statements determining if the zip code was valid was to use return statements that would break out of the checks at the return, so I wrote a method that would check for the validity of the zip code:
public static boolean checkValidZip(String zip) {
if (zip.length() != 5) { //invalid if not 5 chars long
return false;
}
for (int i=0; i<zip.length(); i++) { //invalid if not all digits
if (!Character.isDigit(zip.charAt(i))) {
return false;
}
}
return true; //valid if 5 digits
}
The main method then, looks like this:
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
String zip = ""; //5 digit zip
boolean valid = false;
boolean allNums = true;
while (!valid) {
System.out.print("Enter your 5 digit zip code: ");
zip = stdIn.nextLine();
valid = checkValidZip(zip);
if (!valid) {
System.out.println("Invalid Zip Code format.");
System.out.println("");
}
}
//end if zip code valid
}
I am trying to read from a text file that have names and phone numbers that can also have other text files in it (including it self)
myBook.txt:
7
name1 123-456-7890
name2 098-765-4321
name3 135-792-4680
name4 246-801-3579
PHONEBOOK-FILE myBook2.txt
name5 147-025-8369
name6 150-263-7495
myBook2.txt:
1
Name7 000-222-3332
The first line is the number of items in the file, then it has PHONEBOOK-FILE to signify another file.
I cannot use arrays, I cannot change myBook.txt, I cannot use try / catch, and I have to use recursion
This is the code I have:
import java.util.*;
import java.io.*;
public class Phonebook
{
private boolean DEBUG = true;
private Scanner scan;
private Scanner input;
private File file;
private File holder;
private String query;
private boolean bottomOut;
private int nameCount;
private String fileNameHold;
// entry point for class
public void run()throws IOException
{
input = new Scanner(System.in);
//Gets file name and checks if it exists valid file
while(true)
{
System.out.print("Name of phone book to read in: ");
fileNameHold = input.next();
file = new File(fileNameHold);
if(file.exists())
break;
else
System.out.println("That file does not exist!");
}
System.out.println("Phonebook successfully read in!");
//Main control loop
while(true)
{
bottomOut = false;
System.out.print("Please enter person to search for: ");
query = input.next();
if(query.equals("."))
break;
file = new File(fileNameHold);
System.out.println(doWork(query, file, 0));
}
System.out.print("Thank you for using this program!");
}
//Does the searching and recursive stuff
private String doWork(String query, File fileName, int level)throws IOException
{
scan = new Scanner(fileName);
//Grabs item count fom begining of file
//if(!bottomOut)
nameCount = Integer.parseInt(scan.nextLine());
String line = "";
//Runs through entries
for(int i=0; i<nameCount; i++)
{
line = scan.nextLine();
debug("file: " +file);
debug("line: " + line);
debug("nameCount: " + nameCount);
if(line.toLowerCase().contains(query.toLowerCase()))
{
return line;
}
//Recursion is used to searth through linked files
else if(line.contains("PHONEBOOK-FILE"))
{
//System.out.println("Sanity Check");
holder = new File(line.replace("PHONEBOOK-FILE ", ""));
if(level < 2 || (level > 0 && bottomOut))
return doWork(query, holder, ++level);
else if(level >= 2 && !bottomOut)
bottomOut = true;
else
return "not found (REC)";
}
}
return "not found";
}
private void debug(String stuff)
{
if(DEBUG)
System.out.println("[[--DEBUG--]] " + stuff);
}
}
I assume the issue is in doWork but I could be wrong. What it is doing is it recurses through the file until it hits a specified bottom where if it hasn't found the name it should break out of the recursion and continue passed the PHONEBOOK-FILE line.
Currently if you search for a name passed that line if returns not found. It doesn't seem to be coming out of the recursion.
As you can probably tell I an not great with this.
Thanks for any help.
For each line in your file, you are going to compute a value. Either not found, or a line of your phonebook. If you get a line, you can break out of the loop. Either way, after the loop you return the value: either the line you got or not found;
What is trickier is how you compute a line which references another phonebook, the answer is that you just call your method with that phonebook. That's the recursion part.
import java.util.*;
import java.io.*;
public class Phonebook
{
private Scanner input;
private File file;
private String query;
// entry point for class
public void run()throws IOException
{
input = new Scanner(System.in);
//Gets file name and checks if it exists valid file
while(true)
{
System.out.print("Name of phone book to read in: ");
fileNameHold = input.next();
file = new File(fileNameHold);
if(file.exists())
break;
else
System.out.println("That file does not exist!");
}
System.out.println("Phonebook successfully read in!");
//Main control loop
while(true)
{
bottomOut = false;
System.out.print("Please enter person to search for: ");
query = input.next();
if(query.equals("."))
break;
file = new File(fileNameHold);
System.out.println(doWork(query, file));
}
System.out.print("Thank you for using this program!");
}
//Does the searching and recursive stuff
private String doWork(String query, File fileName)throws IOException
{
Scanner scan = new Scanner(fileName);
int nameCount;
File recurFile;
nameCount = Integer.parseInt(scan.nextLine());
String line = "";
String value = "Not found";
//Runs through entries
for(int i=0; i<nameCount; i++)
{
line = scan.nextLine();
// if the line is a file, then the value of that line
// is the result to your function applied to that new file
if(line.contains("PHONEBOOK-FILE")) {
recurFile = new File(line.replace("PHONEBOOK-FILE ", ""));
line = doWork(query, holder, ++level);
}
// the file will either return Not found or
// a line corresponding to your query
if(line.toLowerCase().contains(query.toLowerCase()))
{
// Your line is correct. The function doesn't care where it comes from
value = line;
break;
}
}
return value;
}
}
Scenario One: User is asked for 5 digit Input Number and 3 digit Code and then those are replaced in file name and inside the file.
Scenario Two: User is asked for 5 digit Input Number AND then ASKED if they want to input/change the 3 digit code. If yes then they can input a 3 digit code.
Current Code:
package blah blah
import all stuffs...
public class NumbChanger{
public static void main(String[] args) {
try {
Scanner user = new Scanner(System.in);
String inputCode= "";
System.out.print("Enter a xml file directory: "); // Enter xml file directory.
String directory = user.nextLine();
System.out.print("Enter the 5 digit starting Number: ");
int inputNumber = user.nextInt();
System.out.print("Do you want to change the code?");
boolean yesChange = user.hasNext();
if (!yesChange){
} else {
System.out.print("Enter the 3 character Code: ");
inputCode = user.next();
}
user.close();
Path folder = Paths.get(directory);
FilenameFilter xmlFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
String lowercaseName = name.toLowerCase();
if (lowercaseName.endsWith(".xml")) {
return true;
} else {
return false;
}
}
};
//this is the list of files
File[] allFiles = folder.toFile().listFiles(xmlFilter);
if (allFiles == null) {
throw new IOException("No files found");
}
String fileName;
for(File aFile : allFiles) {
if (aFile.isFile()) {
fileName = aFile.getName();
String oldNumber = fileName.substring(
((fileName.lastIndexOf(".")) - 12), (fileName.lastIndexOf(".")) - 4);
String oldCode = fileName.substring(
((fileName.lastIndexOf(".")) - 3), (fileName.lastIndexOf(".")));
if (!yesChange){
} else {
inputCode = fileName.substring(
((fileName.lastIndexOf(".")) - 3), (fileName.lastIndexOf(".")));
}
String newNumber = String.valueOf(inputNumber++);
String newFileName = fileName.replaceAll(
oldNumber, newNumber);
if (!yesChange){
} else {
newFileName = newFileName.replaceAll(oldCode, inputCode);
}
//renaming the file
Path newFilePath = Files.move(aFile.toPath(),
aFile.toPath().resolveSibling(newFileName));
//replacing the entry # within the XML
String content = new String(Files.readAllBytes(newFilePath),
StandardCharsets.UTF_8);
content = content.replaceAll(oldNumber, newNumber);
content = content.replaceAll(oldCode, inputCode);
Files.write(newFilePath, content.getBytes(StandardCharsets.UTF_8));
}
}
System.out.print(allFiles.length + " xml files were changed.");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
System.out.println(" Good Job!");
System.exit(0);
}
}
}
Reflection on above code.
Currently I make it work if they enter values for both. Where am I going wrong?
Further enhancements:
Check the length of code.
I understand I can do a simple
if (inputCode.length == 3){
}
else {
System.out.print ln ("Error")
}
But Im not to privy with booleans and while loops and if the user enters a different value I want them to prompt again versus having to run the program again.
thanks in advance! :)
Im not sure I understand your question, but wouldn't
System.out.print("Enter the 5 digit starting Number: ");
int inputNumber = user.nextInt();
while(String.valueOf(inputNumber).length() != 5) {
System.out.println("Please enter a 5 digit number.");
inputNumber = user.nextInt();
}
do the job?
If the number is not 5 digits long the user is asked to enter a new one.
You cant use .length() on an Integer, so you will have to convert it to a String first. Hence the line
String.valueOf(inputNumber).length()
I am trying to write a program that will check if the user-entered string is a binary number, and if it is, it will output the number of 1s in the number. I had this working fine with an integer value, but since an int can't hold more than 2 billion or whatever the max value is, I am trying to rewrite it to work with Strings.
As of right now, any number I enter will output "Number entered is not binary." and when I enter 0, I will get a StringIndexOutofBoundsException. I am a fairly novice programmer, so forgive any obvious errors I may have missed, I am just asking for a possible solution to my problem or a push in the right direction. Here is my code (after trying to make it work with Strings rather than integers):
import java.util.*;
public class BinaryHW {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter a binary number: ");
String bin = kb.nextLine();
//the method I used to check whether or not user entered a binary
//number requires changing the value of 'bin'.
//Therefore, 'origBin' set equal to 'bin' for later use.
String origBin = bin;
int count = 0;
boolean isBinary = true;
/* if bin = 0, then this loop will be skipped because
* 0 is a binary number and need not be checked.
*/
while (Integer.parseInt(bin) != 0) {
int lastDigit = bin.charAt(bin.length() - 1);
if (lastDigit > 1) {
System.out.println("Number entered is not binary.");
isBinary = false;
break;
} else {
bin = bin.substring(bin.length() - 2);
}
}
//Again, the method I used to count the 1s in the bin number
//requires changing the value of origBin, so origBin2 is introduced
String origBin2 = origBin;
for (int i = 0; i < origBin.length(); i++) {
if (origBin.charAt(origBin.length() - 1) == 1) {
count ++;
origBin2 = origBin.substring(origBin2.length() - 2);
} else {
origBin2 = origBin.substring(origBin2.length() - 2);
}
}
if (isBinary)
if (count == 1)
System.out.println("There is "
+ count + " 1 in the binary number entered.");
else
System.out.println("There are "
+ count + " 1s in the binary number entered.");
}
}
I think you are overcomplicating things... simply iterate through your binary string, and keep track of the number of 1's reached. If a number other than 0 or 1 is found, report that input is a non-binary number. Below is a snippet which accomplishes this:
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter a binary number: ");
String bin = kb.nextLine();
int oneCount = 0;
boolean validBinaryNum = true;
for (int i = 0; i < bin.length() && validBinaryNum; i++) {
char currentChar = bin.charAt(i);
if (currentChar == '1') oneCount++;
else if (currentChar != '0') {
validBinaryNum = false;
}
}
if (validBinaryNum && bin.length() != 0) {
System.out.println("Number of 1's: " + oneCount);
} else {
System.out.println("Number is not binary");
}
}