3 Replies - 1015 Views - Last Post: 12 August 2012 - 04:43 PM Rate Topic: -----

#1 ewhite   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-August 12

Coding Error in the below program. Related to Rectangular Array

Posted 12 August 2012 - 02:58 PM

I have made changes to the code after gathering lot of information from here and there. But its giving me errors.
My program should throw results like the following, but its not. Please tell me what code i am doing wrong and why?!

My program should show results like this:

Future Value Calculations

Inv/Mo. Rate Years Future Value
$100.00 8.0% 10 $18, 416.57
$125.00 8.0% 10 $23, 020.71
$150.00 8.0% 10 $27, 624.85

Here is my current Code:

import java.util.*;
import java.text.*;

public class FutureValueApp
{
    public static void main(String[] args)
    { 
     int rowCounter = 0;
     String[][] array = new String[10][4];
        // display a welcome message
        System.out.println("Welcome to the Future Value Calculator");
        System.out.println();

        // perform 1 or more calculations
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {

            // get the input from the user
            System.out.println("DATA ENTRY");
            double monthlyInvestment = getDoubleWithinRange(sc,
                "Enter monthly investment: ", 0, 1000);
            double interestRate = getDoubleWithinRange(sc,
                "Enter yearly interest rate: ", 0, 30);
            int years = getIntWithinRange(sc,
                "Enter number of years: ", 0, 100);
                Integer.toString(int i)
            // calculate the future value
            double monthlyInterestRate = interestRate/12/100;
            int months = years * 12;
            double futureValue = calculateFutureValue(
                monthlyInvestment, monthlyInterestRate, months);

            // get the currency and percent formatters
            NumberFormat currency = NumberFormat.getCurrencyInstance();
            NumberFormat percent = NumberFormat.getPercentInstance();
            percent.setMinimumFractionDigits(1);
        for(int i = 0; i < array.length; i++) {
         for(int j = 0; j < array[i].length; j++) {
        // Displays the elements in the array
    }
}
            // format the result as a single string
            String results =
                  "Monthly investment:\t"
                      + currency.format(monthlyInvestment) + "\n"
                + "Yearly interest rate:\t"
                      + percent.format(interestRate/100) + "\n"
                + "Number of years:\t"
                      +  years + "\n"
                + "Future value:\t\t"
                      + currency.format(futureValue) + "\n";

            // print the results
            System.out.println();
            System.out.println("FORMATTED RESULTS");
            System.out.println(results);

            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
        }
    }

    public static double getDouble(Scanner sc, String prompt)
    {
        boolean isValid = false;
        double d = 0;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextDouble())
            {
                d = sc.nextDouble();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid decimal value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return d;
    }

    public static double getDoubleWithinRange(Scanner sc, String prompt,
    double min, double max)
    {
        double d = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            d = getDouble(sc, prompt);
            if (d <= min)
                System.out.println(
                    "Error! Number must be greater than " + min + ".");
            else if (d >= max)
                System.out.println(
                    "Error! Number must be less than " + max + ".");
            else
                isValid = true;
        }
        return d;
    }

    public static int getInt(Scanner sc, String prompt)
    {
        boolean isValidInt = false;
        int i = 0;
        while (isValidInt == false)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValidInt = true;
            }
            else
            {
                System.out.println("Error! Invalid integer value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return i;
    }

    public static int getIntWithinRange(Scanner sc, String prompt,
    int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            i = getInt(sc, prompt);
            if (i <= min)
                System.out.println(
                    "Error! Number must be greater than " + min + ".");
            else if (i >= max)
                System.out.println(
                    "Error! Number must be less than " + max + ".");
            else
                isValid = true;
        }
        return i;
    }

    public static double calculateFutureValue(double monthlyInvestment,
    double monthlyInterestRate, int months)
    {
        double futureValue = 0;
        for (int i = 1; i <= months; i++)
        {
            futureValue =
                (futureValue + monthlyInvestment) *
                (1 + monthlyInterestRate);
        }
        return futureValue;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Coding Error in the below program. Related to Rectangular Array

#2 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,618
  • Joined: 25-December 08

Re: Coding Error in the below program. Related to Rectangular Array

Posted 12 August 2012 - 04:37 PM

Line 28 you have some serious syntax issues:
Integer.toString(int i)



If you want to use Integer.toString( int i ); you must do two things. First you need to pass a valid int object. Second, you should save it to a new String object.
int val = 5;
String str_val = "";
str_val = Integer.toString( val );



What did you want to convert to a string? I removed the line, and it looks like your program is working

This post has been edited by SwiftStriker00: 12 August 2012 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 Moosey   User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 18
  • Joined: 12-August 12

Re: Coding Error in the below program. Related to Rectangular Array

Posted 12 August 2012 - 04:40 PM

Take a look at line number 23 you have Integer.toString(int i)
You don't have to write int inside the parentheses and beside the fact you don't have the variable i anywhere except in your for loops so it can't change it to a string. Omit line 23 and it will work just fine.
Was This Post Helpful? 0
  • +
  • -

#4 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Coding Error in the below program. Related to Rectangular Array

Posted 12 August 2012 - 04:43 PM

And next time, post the error message you are getting. It will help people here easily help you and point out your mistakes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1