Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: I need assistance with a rectangular array?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need assistance with a rectangular array?

    I don't understand how to add code that stores the formatted values as strings in the next row of the array and use the toStrings method of the integer class to store the years value? so far i've been able to create the initial program just need help with storeing the formatted values as strings in the next row of the array and use the toStrings method of the integer class to store the years value?

    this is the type of table i want to accomplish:
    monthly investment. Yearly interset rate. Years. future value.
    $100.00 ---------------------- 8.0----------10---- $18,2000
    $200.00 ---------------------- 7.0 --------- 7 -- ------ $6.2

    heres my code so far.
    PHP Code:
    import java.util.*;
    import java.text.*;
    public class 
    FutureValueApp
    {
        public static 
    void main(String[] args)
        {
            
    String[][] FutureValueArray = new String [10][4];
            
    int counter 0;
            
    // 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: "01000);
                
    double interestRate getDoubleWithinRange(sc,
                    
    "Enter yearly interest rate: "030);
                
    int years getIntWithinRange(sc,
                    
    "Enter number of years: "0100);

                
    // calculate the future value
                
    double monthlyInterestRate interestRate/12/100;
                
    int months years 12;
                
    double futureValue calculateFutureValue(
                    
    monthlyInvestmentmonthlyInterestRatemonths);

                
    // get the currency and percent formatters
                
    NumberFormat currency NumberFormat.getCurrencyInstance();
                
    NumberFormat percent NumberFormat.getPercentInstance();
                
    percent.setMinimumFractionDigits(1);

                
    // 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);
                
                
    FutureValueArray[counter][0] = Double.toString(monthlyInvestment);
                
    FutureValueArray[counter][1] = Double.toString(interestRate);
                
    FutureValueArray[counter][2] = Integer.toString(years);
                
    FutureValueArray[counter][3] = Double.toString(futureValue);
                
             
    System.out.print("Inv/Mo.\tRate\tYears\tFuture Value\n");   
            for(
    int i 0FutureValueArray.lengthi++)
                {
                    for (
    int j 0FutureValueArray[i].lengthj++)
                    
    System.out.println(FutureValueArray[i][j] + " \t");
                    
    System.out.println("\n");
                }
                
    // see if the user wants to continue
                
    System.out.print("Continue? (y/n): ");
                
    choice sc.next();
                
    counter++;
                
    System.out.println();
            }
            
            
        }

        public static 
    double getDouble(Scanner scString prompt)
        {
            
    boolean isValid false;
            
    double d 0;
            while (
    isValid == false)
            {
                
    System.out.print(prompt);
                if (
    sc.hasNextDouble())
                {
                    
    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 scString prompt,
        
    double mindouble max)
        {
            
    double d 0;
            
    boolean isValid false;
            while (
    isValid == false)
            {
                
    getDouble(scprompt);
                if (
    <= min)
                    
    System.out.println(
                        
    "Error! Number must be greater than " min ".");
                else if (
    >= max)
                    
    System.out.println(
                        
    "Error! Number must be less than " max ".");
                else
                    
    isValid true;
            }
            return 
    d;
        }

        public static 
    int getInt(Scanner scString prompt)
        {
            
    boolean isValidInt false;
            
    int i 0;
            while (
    isValidInt == false)
            {
                
    System.out.print(prompt);
                if (
    sc.hasNextInt())
                {
                    
    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 scString prompt,
        
    int minint max)
        {
            
    int i 0;
            
    boolean isValid false;
            while (
    isValid == false)
            {
                
    getInt(scprompt);
                if (
    <= min)
                    
    System.out.println(
                        
    "Error! Number must be greater than " min ".");
                else if (
    >= max)
                    
    System.out.println(
                        
    "Error! Number must be less than " max ".");
                else
                    
    isValid true;
            }
            return 
    i;
        }

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



  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,099
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: I need assistance with a rectangular array?

    Can you post what the program outputs now and explain what is wrong with it?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need assistance with a rectangular array?

    Quote Originally Posted by Norm View Post
    Can you post what the program outputs now and explain what is wrong with it?
    it outputs the following like 10 times

    null
    null
    null
    null

    null
    null
    null
    null

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,099
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: I need assistance with a rectangular array?

    I don't understand why this statement isn't executed:
          System.out.println("Welcome to the Future Value Calculator");
    It would print "Welcome to the Future Value Calculator".
    The post of the program's output does not show that line being printed. Are you sure you are executing the code posted in post#1?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need assistance with a rectangular array?

    Quote Originally Posted by Norm View Post
    I don't understand why this statement isn't executed:
          System.out.println("Welcome to the Future Value Calculator");
    It would print "Welcome to the Future Value Calculator".
    The post of the program's output does not show that line being printed. Are you sure you are executing the code posted in post#1?
    Yea prints it out for me, but I don't think thats causing the issue I think the issue is caused by my code:
    FutureValueArray[counter][0] = Double.toString(monthlyInvestment);
        FutureValueArray[counter][1] = Double.toString(interestRate);
        FutureValueArray[counter][2] = Integer.toString(years);
        FutureValueArray[counter][3] = Double.toString(futureValue);

    as am trying to After the code that calculates, formats, and displays the resutls for each calculation, add code that stores the formatted values as strings in the next row of the array ( I need to use the toString method of the integer class to store the years value). and the display the element of the array in the console.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,099
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: I need assistance with a rectangular array?

    Can you post the full output from the program? That will help us see where in the code the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need assistance with a rectangular array?

    Quote Originally Posted by Norm View Post
    Can you post the full output from the program? That will help us see where in the code the problem is.
    this is how my table should look like:
    ex:
    Inv/Mo. Rate Years Future Value
    $100.00 8.0% 10 $18.416.57
    $125.00 8.0% 10 $23,020.71

    this is the Output I get:

    Welcome to the Future Value Calculator

    DATA ENTRY
    Enter monthly investment: 320
    Enter yearly interest rate: 23
    Enter number of years: 2

    FORMATTED RESULTS
    Monthly investment: $320.00
    Yearly interest rate: 23.0%
    Number of years: 2
    Future value: $9,821.33

    Inv/Mo. Rate Years Future Value
    320.0
    23.0
    2
    9821.329095253966


    null
    null
    null
    null


    null
    null
    null
    null


    null
    null
    null
    null


    null
    null
    null
    null


    null
    null
    null
    null


    null
    null
    null
    null


    null
    null
    null
    null


    null
    null
    null
    null


    null
    null
    null
    null


    Continue? (y/n):

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,099
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: I need assistance with a rectangular array?

    320.0
    23.0
    2
    9821.329095253966
    This part of the array prints out ok. The rest of the array hasn't had any data stored in it yet. Use the value of counter to control how much to print.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need assistance with a rectangular array?

    Quote Originally Posted by Norm View Post
    This part of the array prints out ok. The rest of the array hasn't had any data stored in it yet. Use the value of counter to control how much to print.
    I have update the code.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,099
    Thanks
    52
    Thanked 2,284 Times in 2,255 Posts

    Default Re: I need assistance with a rectangular array?

    Does it work now?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need assistance with a rectangular array?

    ok so I got got the counter to countrol the array so i dont print nulls using for(int i = 0; i <= counter; i++) so now how do I structure the out put so its in a table and not a list like this:
    320.0
    23.0
    2
    9821.32909525396

    it would be cool to have it look like a table:
    Inv/Mo. Rate Years Future Value
    320.0 23.0 2 9821.32909525396

    --- Update ---

    Finally I got it, I was using System.out.println instead of System.out.print to structure the array into a table.
    Thanks Alot.

Similar Threads

  1. [SOLVED] for loop assistance
    By Kseidel in forum Loops & Control Statements
    Replies: 3
    Last Post: September 17th, 2012, 08:10 PM
  2. Need some assistance please
    By JavaPhish in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 09:00 AM
  3. rotating a rectangular image
    By ighor10 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 19th, 2010, 06:12 PM
  4. array program assistance needed
    By JavaNoob82 in forum Collections and Generics
    Replies: 4
    Last Post: December 14th, 2009, 04:49 AM
  5. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 12:12 AM

Tags for this Thread