Question

Hello, I'm having some issues with my code and would appreciate any help This is the...

Hello, I'm having some issues with my code and would appreciate any help

This is the code I'm working with:

https://pastebin.com/0QVP91yt

This is the error I'm getting.

I need to add the total interest of the loans after listing all of them, instead of listening the interest after each loan. I also need to move the "Done reading file" output after listing the loans instead of before.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.util.*;

import java.io.*;

// Defines driver class CalcInterestProfits

public class CalcInterestProfits {

// Declares an array of object of the class ModLoan

ModLoan mLoan[];

// Method to read file and stores the data in mLoan array of object

int readFile(String filename) {

// Local variable to store the data read from file

int loanNumber;

double loanAmount;

double annualInterestRate;

int loanTerm;

// Counter for number of records

int counter = 0;

// Scanner class object declared to read file contents

Scanner fileRead = null;

// try block begins

try {

// Opens the file for reading

fileRead = new Scanner(new File(filename));

// Loops till end of the file to count number of records

while (fileRead.hasNext()) {

// Reads a record

fileRead.nextLine();

// Increase the record counter by one

counter++;

} // End of while loop

// Close the file

fileRead.close();

// Allocates memory to array of size counter

mLoan = new ModLoan[counter];

// Re initializes counter to 0

counter = 0;

// Re - opens the file for reading

fileRead = new Scanner(new File(filename));

// Loops till end of the file

while (fileRead.hasNext()) {

// Reads the loan number from the file

loanNumber = fileRead.nextInt();

// Reads the loan amount from the file

loanAmount = fileRead.nextDouble();

// Reads the annual interest rate from the file

annualInterestRate = fileRead.nextDouble();

// Reads the loan term from the file

loanTerm = fileRead.nextInt();

// Creates the object using parameterized constructor and adds it to

// counter index position of mLoan array of object

mLoan[counter] = (new ModLoan(loanNumber, loanAmount, annualInterestRate, loanTerm));

// Increase the record counter by one

counter++;

} // End of while loop

// Close the file

fileRead.close();

} // End of try

// Catch block to handle FileNotFoundException

catch (FileNotFoundException e) {

e.printStackTrace();

System.err.println("Unable to open the file" + filename + "\n Program exiting.");

System.exit(1);

} // End of catch

return counter;

} // End of method

// main method definition

public static void main(String[] args) throws IOException {

// Creates an object of class CalcInterestProfits

CalcInterestProfits cip = new CalcInterestProfits();

// Stores the file name to write

final String LOG_FILENAME = "log.txt";

// Scanner class object created to accept data from user

Scanner keyboard = new Scanner(System.in);

// To store the file name to read from file

String filename;

// Initializes File object to null

File inFile = null;

// Initializes Scanner object for file to null

Scanner fileReader = null;

// PrintStream class object declared to write data to file

PrintStream ps = null;

// Accepts the file name from the user

System.out.println("Enter input data filename:");

filename = keyboard.next();

// Calls the method to read file contents and store it in array of objects

// Stores return value from the method as number of records

int len = cip.readFile(filename);

// Creates two double array to store monthly payment and total interest

double MonthlyPayment[] = new double[len];

double totalInterest[] = new double[len];

double totInterest = 0.0;

// Loops till number of records

for (int x = 0; x < len; x++) {

// Calls the method to display each record data

cip.mLoan[x].displayLoanTerms();

// Calls the method to calculate each record monthly payment

MonthlyPayment[x] = cip.mLoan[x].computeMonthlyPayment();

// Calls the method to calculate each record total interest

totalInterest[x] = cip.mLoan[x].computeTotalInterest(MonthlyPayment[x]);

totInterest += totalInterest[x];

} // End of for loop

// Displays number of records read from file

System.out.println("Done reading file " + filename + " -- " + len + " lines of data read");

// Displays total interest for each record

System.out.println("Total interest to be collected on all loans: $" + totInterest);

// try block begins

try {

// Opens the file for writing

ps = new PrintStream(new File(LOG_FILENAME));

// Loops till number of records

for (int x = 0; x < len; x++)

// Writes the total interest with message to file

ps.println(totalInterest[x] + " interest will be collected on loan number "

+ cip.mLoan[x].getLoanNumber());

} // End of try block

// Catch block to handle FileNotFoundException

catch (Exception e) {

e.printStackTrace();

System.err.println("Unable to open the file for writing");

} // End of catch

} // End of main method

} // End of class

nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/October/17102018$ javac CalcInterestProfits.java nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/october/17102018$ java CalcInterestProfits Enter input data filename: loanData2.txt Loan 111111 is for $111111.00 at 1.10% APR, to be paid over 120 month(s) Loan 222222 is for $222222.00 at 2.22% APR, to be paid over 240 month(s) Loan 333333 is for $300000.00 at 3.30% APR, to be paid over 360 month(s) Loan 444444 is for $340000.00 at 4.44% APR, to be paid over 180 month(s) Loan 555555 is for $350000.00 at 5.00% APR, to be paid over 300 month(s) Done reading file loanData2.txtー5 lines of data read Total interest to be collected on all loans: $622560.73

Add a comment
Know the answer?
Add Answer to:
Hello, I'm having some issues with my code and would appreciate any help This is the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm stuck on an assignment and would really appreciate some help with the parts that are...

    I'm stuck on an assignment and would really appreciate some help with the parts that are in bold below: Write a program that takes a value from a user and stores it in the registry. You can use any key name that you like but also store the current time as another value inside of your new key. Finally, get a directory listing of your current working directory and store that value. You may need to use REG_MULTI_SZ for that...

  • Hello; I would greatly appreciate your help with providing a solution to a problem that I'm...

    Hello; I would greatly appreciate your help with providing a solution to a problem that I'm having trouble with. Thank you! Write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. The numbers will be read in from the user via the cin statement. You do not know how many numbers the user may enter. When the user enters a 0 (zero), then the user is done entering...

  • Hello everyone! I am working on my assignment for C++ and I'm having a bit of...

    Hello everyone! I am working on my assignment for C++ and I'm having a bit of trouble on the value producing functions, I am creating a program wherein I have to input a value and I need to create a function called square value where in it will square the value I have input. I cannot seem to figure out this part. Here is the code that I have so far, and thank you for any help. I greatly appreciate...

  • Hello, I would greatly appreciate it if anyone could help me. I saved a text file...

    Hello, I would greatly appreciate it if anyone could help me. I saved a text file in the C drive of my computer. I need to use open the text file to do my homework. But when I used the Python 3.7 to open the file, I got error message: NO SUCH FILE OR DIRECTORY. What should I do to open the file using the Python. Many thanks for your help.

  • Hello, I'm having some trouble with this physics question, any help is appreciated thanks! A beam...

    Hello, I'm having some trouble with this physics question, any help is appreciated thanks! A beam of monochromatic light passes from air to water (n = 1.33) to glass (n = ?). The two interfaces are parallel to each other, and the angle of incidence in air is 26 degrees, and that in the glass is 18 degrees. What is the index of refraction of the glass? Keep 2digits after decimal point. Your answer should be formatted like: 0.00

  • I'm trying to do this in a C# Console App.(NET Framework).I've attached the Code that will...

    I'm trying to do this in a C# Console App.(NET Framework).I've attached the Code that will be required to complete. Any Guidance will be appreciated. C# Console File I/O Project Assignment Working with files and being able to process data in and out of a file is critical to the programmer’s role. Almost every organization will have to deal with files one way or another. For example, it is very common for accountants to use spreadsheets for calculations and sometimes...

  • I'm kind of new to programming, and I am having trouble figuring out why my program...

    I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...

  • Hello I need help with the following I'm having trouble getting to just the main code....

    Hello I need help with the following I'm having trouble getting to just the main code. Attached is the ButtonDebounce header file and the description of the code the language is C++; This lab will end with a functioning combination lock. The lock will have a combination that resides in EEPROM, and is programmable via the serial port. The user will enter a code, using the button and encoder on the board, and is shown on the lcd screen. Once...

  • JAVA Hello, I would like some help wiht this program, I'm not sure hard to code...

    JAVA Hello, I would like some help wiht this program, I'm not sure hard to code the deep copy and array for MenuSchedule Here is my code: Chez Moraine, a fake restaurant known for fine dining at reasonable prices, has hired you to develop a program to manage their menu during peak times: Friday, Saturday, and Sunday nights Below is the UML diagram you will implement. You may also use my specs on the next page as your guide to...

  • I'm having trouble to link the .js file to the html so changes for the html...

    I'm having trouble to link the .js file to the html so changes for the html to be made in the .js file <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>:JavaScript, HTML and jQuery</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- this is so I don't have to give you a separate CSS file --> <style>     .hidden {display: none;}     .menu { width: 100px;} </style> </head> <body> <div class="container"> <h1>Assignment 2: JavaScript, HTML and jQuery</h1> <p>To complete this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT