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 8454179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:57:26+00:00 2026-06-10T11:57:26+00:00

So I want to read a text file but for some strange reason it

  • 0

So I want to read a text file but for some strange reason it can’t find the file. I have used these methods before, and I therefore have no idea why this isn’t working can someone please help me out?

EDIT:

sorry guys I left out a big piece of info which is that it can find the file when it is writing to it but not when it is reading from it. Thanks for all your guys’ help and sorry for wasting your time asking a question I didn’t need the answer to… Again sorry.

Some more info:

“Assets.txt” is in both the project’s root folder as well as in the src/assetregistry

“Assets.txt” will be there when the program is run

All I get is the JOption messege from the catch exception in the readFromFile() method

According to the properties of assetRegistry the working directory is

"C:\Users\Justin\Documents\NetBeansProjects\assetregistry\src\assetregistry"

Thank you to everyone who helped, especially Chris and Andrew Thompson. The program now work and the following is the updated version. Feel free to copy it if you want. It’s really a simple program.

Main class:

package assetregistry;
import java.io.IOException;
import javax.swing.JOptionPane;

public class Assetregistry {

public static void main(String[] args) throws IOException {
    new Assetregistry();
}

assetArray aa = new assetArray();

public Assetregistry() throws IOException {
    aa.readFromFile ();
    char choice = 'Z';
    while (choice != 'X') {
        choice = menu();
        options(choice);
    }
}   

public char menu() {
    char ch = JOptionPane.showInputDialog("\t" + "Welcome to asset registry. Please input your choice" + "\n" + "A: Enter new asset" + "\n" + "B: Calculate depreciation and display" + "\n" + "X: Exit").toUpperCase().charAt(0);
    return ch;
}

private void options(char c) throws IOException {

    switch (c) {
        case 'A':
            aa.enterAsset();
            break;
        case 'B':
            aa.calculate();
            break;
        case 'X':
            System.exit(0);
            break;
    }
}
}

Array/method class

package assetregistry;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;

public class assetArray {

asset[] aArr = new asset[100];
private double year;
private double month;
private int count = 0;
Assetregistry AR;
String home = System.getProperty("user.home");
File userHome = new File(home);
File file = new File(userHome,"Assets.txt");

public assetArray() throws IOException {
}

public void enterAsset() throws IOException {

    int choice = JOptionPane.YES_OPTION;
    while (choice == JOptionPane.YES_OPTION) {
        String name = JOptionPane.showInputDialog("Enter asset name");
        double costP = Double.parseDouble(JOptionPane.showInputDialog("Enter the cost price of the asset"));
        int lSpan = Integer.parseInt(JOptionPane.showInputDialog("Enter the amount of years you wish to keep the asset"));
        double mBought = Double.parseDouble(JOptionPane.showInputDialog("Enter the month the asset was bought (As a number)"));
        double yBought = Double.parseDouble(JOptionPane.showInputDialog("Enter the year the asset was bought"));
        double depr = (1.00 / lSpan * 100.00);
        aArr[count] = new asset(name, costP, lSpan, yBought, mBought, depr);
        PrintWriter pw = new PrintWriter(new FileWriter(file, true));
        pw.println(aArr[count].toString());
        pw.close();
        count++;
        choice = JOptionPane.showConfirmDialog(null, " Do you want to enter another asset?", " Enter another asset?", JOptionPane.YES_NO_OPTION);
    }
}

public void calculate() {
    String name;
    int lSpan;
    double ybought;
    double mbought;
    double pValue;
    double Rate;
    double deprExp;
    double numYears;
    double fValue;
    double accDepr;
    String[] mnth = new String[12];
    mnth[0] = "January";
    mnth[1] = "February";
    mnth[2] = "March";
    mnth[3] = "April";
    mnth[4] = "May";
    mnth[5] = "June";
    mnth[6] = "July";
    mnth[7] = "August";
    mnth[8] = "September";
    mnth[9] = "October";
    mnth[10] = "November";
    mnth[11] = "December";

    if (count > 0) {
        year = Integer.parseInt(JOptionPane.showInputDialog("Enter the year you wish to calculate depreciation for"));
        month = Integer.parseInt(JOptionPane.showInputDialog("Enter the month you wish to calculate depreciation for"));
        int m = (int) month;
        int y = (int) year;
        System.out.println("Asset regestry" + "\t" + mnth[m-1] + "  " + y);
        for (int i = 0; i < count; i++) {
            name = aArr[i].getName();
            lSpan = aArr[i].getLifeSpan();
            ybought = aArr[i].getyBought();
            mbought = aArr[i].getmBought();
            pValue = aArr[i].getCostP();
            Rate = aArr[i].getDeprR();
            int m2 = (int) mbought;
            int y2 = (int) ybought;
            deprExp = pValue - (pValue * ((1.00 - ((Rate))) * 1.00 / 100.00));
            numYears = (year + (month / 12.00)) - (ybought + mbought / 12.00);
            fValue = pValue * (1.00 - (((Rate) * (numYears)) / 100.00));
            if (fValue <= 0.00) {
                fValue = (int) 1;
            }
            accDepr = pValue - fValue;
            System.out.println("\n" + "Asset:  " + name);
            System.out.println("Life span:  " + lSpan + "yrs");
            System.out.println("Cost price:  " + "R" + pValue);
            System.out.println("Date acquired:  " + mnth[m2-1] + "  " + y2);
            System.out.println("Depreciatin rate (p.a.):  " + Rate + "%");
            System.out.println("Depreciation(p.a.):  R" + deprExp);
            System.out.println("Accumulated depreciation:  R" + accDepr);
            System.out.println("Current book value:  R" + fValue);
            System.out.println("_________________________________________");
        }
    } else {
        JOptionPane.showMessageDialog(null, "There are no assets in memory", "NO ASSETS!", JOptionPane.ERROR_MESSAGE);
    }

}

/**
 *
 */
public void readFromFile() {
    String line = "";
    try {
        BufferedReader fr = new BufferedReader(new FileReader(file));
        line = fr.readLine();
        while (line != null) {
            StringTokenizer stk = new StringTokenizer(line, "#");
            String name = stk.nextToken();
            double costP = Double.parseDouble(stk.nextToken());
            int lSpan = Integer.parseInt(stk.nextToken());
            double yBought = Double.parseDouble(stk.nextToken());
            double mBought = Double.parseDouble(stk.nextToken());
            double deprR = Double.parseDouble(stk.nextToken());
            aArr[count] = new asset(name, costP, lSpan, yBought, mBought, deprR);
            count++;
            line = fr.readLine();

        }
        fr.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "There is a file missing.", "FILE MISSING!", JOptionPane.ERROR_MESSAGE);
    }
}
}

Assets class

package assetregistry;

/**
 *
 * @author Justin
 */
public class asset {

private String name;
private double costP;
private int lifeSpan;
private double yBought;
private double mBought;
private double deprR;

public asset(){
}

public asset(String name, double costP, int lifeSpan, double yBought, double mBought, double deprR) {
this.name = name;
this.costP = costP;
this.lifeSpan = lifeSpan;
this.yBought = yBought;
this.mBought = mBought;
this.deprR = deprR;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getCostP() {
return costP;
}

public void setCostP(double costP) {
this.costP = costP;
}

public int getLifeSpan() {
return lifeSpan;
}

public void setLifeSpan(int lifeSpan) {
this.lifeSpan = lifeSpan;
}

public double getyBought() {
return yBought;
}

public void setyBought(double yBought) {
this.yBought = yBought;
}

public double getmBought() {
return mBought;
}

public void setmBought(double mBought) {
this.mBought = mBought;
}

public double getDeprR() {
return deprR;
}

public void setDeprR(double deprR) {
this.deprR = deprR;
}

@Override
public String toString ()
{
String stg = "";
stg += name + "#" + costP + "#" + lifeSpan + "#" + yBought + "#" + mBought + "#" + deprR + "#";
return stg;
}   
}
  • 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-10T11:57:28+00:00Added an answer on June 10, 2026 at 11:57 am
    String home = System.getProperty("user.home");
    System.out.println("User home directory is: " + home);
    File userHome = new File(home);
    // Put files in a known and reproducible path!
    File file = new File(userHome, "Assets.txt");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file. I want read that file. But In that if
I have a .txt file having some text. I want to read this file
i want to read from a text file in C#. But I want all
I want to read an existing PDF file, get not only the text, but
I want to save a matrix to a text file, so I can read
I have some simple text file. I am trying to read that file contents
I want to read a text file line by line, but I'm not interested
I have a huge line-separated text file and I want to make some calculations
If I have some text file like abc.txt the I want to the hex
I have been doing a project where I want to read a text file

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.