Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:19:14+00:00 2026-06-02T06:19:14+00:00

I am making a program for someone that read/writes to a file. I created

  • 0

I am making a program for someone that read/writes to a file. I created it, and tested it, but it crashes when I tell it the name.
Code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws Exception {

    Scanner scanner = new Scanner(System.in);

    print("Enter a name for the bell: ");
    String bellname = scanner.nextLine();

    FileInputStream fs = new FileInputStream("normbells.txt");
    DataInputStream in = new DataInputStream(fs);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    FileWriter fr = new FileWriter("normbells.txt");
    BufferedWriter bw = new BufferedWriter(fr);
    String line;

    while((line = br.readLine()) != null) {
        int index = line.indexOf(":");

        if(index == -1) {}else{
            String name = line.substring(0, index);

            if(bellname.equals(name)) {
                print("This bell name is already taken!");
                line = null;
                return;
            }

            print("Enter a time for the bell (24-hour format, please): ");

            String time = scanner.nextLine();

            String toWrite = name + ":" + time;

            boolean hasFoundNull = false;
            String currentString;

            while(hasFoundNull == false) {
                currentString = br.readLine();

                if(currentString == null) {
                    hasFoundNull = true;
                    bw.write(toWrite);
                }else{}
            }
        }
    }
}

public static void print(String args) {
    System.out.println(args);
}
}

Here is the output:
Enter a name for the bell:
Durp

Here is the file content:
Actually, the file is empty. It wiped it for some reason. Here is what it originally had:
Durp:21:00

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:19:15+00:00Added an answer on June 2, 2026 at 6:19 am

    FileWriter also has the constructor FileWriter(String, boolean), where the boolean flag means “append”.
    If you do not specify it, it will be false and the file cleared before writing to it.

    So, replace

    fr = new FileWriter("normbells.txt");
    

    with

    fr = new FileWriter("normbells.txt", true);
    

    and maybe it will work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im making a program for class that manages a Hotel. I have a function
I'm making a program that fits the wizard concept ideally; the user is walked
I'm making a program that retrieves decently large amounts of data through a python
I noticed while making a program that a lot of my int type variables
I'm making a program that communicate with certain patient monitor using C sockets. I'm
Now I'm making a little program in Java which must read a really big
I'm making a program that is using Windows Shell Integration, and the registry changes
I'm making a back-end function for another area of a program that will add
Update: someone answered and suggested that I write a plugin, but that is a
I am making a program that stores data in a 2D array. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.