Need Help With Java: Reading a Text File to Pull Data From it.?

Deal Score0

My program is to create a mortgage calculator. The user is required to enter the loan principal then selects from 3 separate loans in a combo box. Once they are done, they click calculate.

In my actionPerformed method I have something along the lines of:
If (source == calculate) \ calculate is the button
if (selection == 0) \meaning this is loan type one from the combo box
{ \ calculate loan one’s method
else if (selection == 1) \ selection for loan 2 from the combobox
{ \ calc loan 2’s method
else if (selection == 2) \ selection for loan 3 from the combobox
{ \calc loan 3’s method

Here’s my dilemma. I need to be able to create a text file (done) and somehow have my program read the interest rates from that file to use for each loan’s calculations. But I keep confusing myself on how to do this because I am still not quite understanding it.

I have read lots of help documents on java, and everything talks about the different components of the method to read the file, but I have yet to find something that explains how to create the method and how to tell my program to “take line 0 and use that for loan 1’s interest rate. Take line 1 for 2’s, take line 2 for 3’s”

So, any help would be realllly appreciated.

P.S. Please don’t tell me to go to the Java help forums….been there done that.
Also, I am not necessarily looking for answers. Good references or resources on how to do it would be great too.
Thank you very very very much Jeff. I really appreciate it.
OKay, new question. After I add a method similar to what you guys are suggesting, what statement would I write in my actionPerformed method to call upon that method and get that particular line?
Okay, you guys are doing a great job with giving me a better understanding.

Jeff, are you saying to create 3 separate methods for each line that I want to retrieve? Then use that specific method when I want to call upon that line?
Because to give you an example, in my actionPerformed method, when the calculate button is clicked, I have 3 separate if statements for each loan type (0,1,2). Here is an example of what one looks like:

if (selection == 0)
Double data = (Double)calculateLoanOne();
String sourceInput = data.toString();

That’s why I was confused with how I would call upon that method in each of the if statements
And to better elaborate on the if statement, each statement is saying if the selection is Loan One, then calculate Loan One’s method. I have 3 separate methods for each loan.
*~Thanks for all the help guys. I resolved my problem. The answer was right in front of me the whole time~*

  1. Reply
    April 29, 2011 at 9:09 pm

    I’ve always liked the Scanner api for this. You could try something along the lines of

    File file = new File(pathOnLocalDisk);
    Scanner scanner = new Scanner(file);

    then you can try something like
    String lineOne, lineTwo, lineThreel
    lineOne = scanner.nextLine()

    lineTwo = scanner.nextLine()

    lineThree = scanner.nextLine()


    String [] strings = new String[];
    int count=0;
    strings[count] =;

    I have provided the links to the File and Scanner API below

    Edit: changed next() to nextLine(), if you are interested in using something other than a line break as your delimeter between the interest rates you can look into the useDelimiter(Pattern pattern) method on Scanner

    2nd Question:
    If you mean get that particular line of the file I suggest reading the file once at the beginning of the program as file I/O is fairly slow. Store all of the lines of the file in a global array or List of Strings in the class and when you want to access line x you could have a method somewhat along the lines of

    public List fileInputList;

    public String getInputLine(int lineNumber){
    if(fileInputList == null){
    if(lineNumber >= fileInputList.size())
    return “”;

    return fileInputList.get(lineNumber);

  2. Reply
    April 29, 2011 at 9:29 pm

    i am assuming each line of your text file has only one floating point number( interest rate usually are not whole numbers ).

    so just give the ( zero-indexed) line you want to the following function and get back the float you need.

    don’t forget to change the text file name to what you have and also to import the following :
    import java.util.Scanner;

    feel free to add as much as error checking code you want to this, if you need to.

    public float getInterestRateFromLine(int line)
    File file = new File(“data.txt”);
    String lineStr=””;
    try {
    Scanner sc = new Scanner(file);
    for ( int i = 0; i <= line; i++ ) { lineStr = sc.nextLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } return Float.parseFloat(lineStr); }

    Leave a reply

    Register New Account
    Reset Password