loop into a variable

133 Views Asked by At

I'm doing a problem for my Java class, and I'm hoping you can point me in the right direction, or give me a hint.

The program main uses a method I wrote outside the main.

The program takes variables entered in JOptionPane, and puts them into the method.

The method takes those variables, and runs them through a 'while' loop, and returns the result to the main.

The problem I'm having is, getting the return value to go back into the main for output.

I want to have

return fin;

go back to the main, so it can be outputted.

Part of the problem, is that I have to use different data types, particularly, float and double, which I can't seem to be able to parse properly.

The code is as follows:

public static double Compound(int time,double rate,float init)
{
    double fin,capital;
    int counter;
    fin=capital;
    capital=0;
    counter=0;
    while (counter<time)
    {
        capital=init+(rate/100.0)*init;         
        counter++;          
    }
    return fin;



}

I know I screwed up somewhere, but I can't tell where.

EDIT EDIT EDIT

I managed to get the code mostly working. Now I just have to plug in the variables into the code above.

The code below is what I'm using to get into the code above.

The instructor wants the input all in one window.

Getting the input into the method is going to trip me up, I know it.

Anyway, here's the rest of the code:

import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Pr50
{
public static void main(String[] args)
{
    JTextField time=new JTextField();
    JTextField interest=new JTextField();
    JTextField initial=new JTextField();
    Object[] fields={
        "Time to Maturity", time,
        "Interest rate", interest,
        "Initial deposit", initial
        };
    JOptionPane.showInputDialog(null, fields);


    int x;
    double fin,rate;
    float init;
    fin=money.Compound(5,10,5500);
}
}

EDIT EDIT EDIT

I got the problem working, mostly, kinda, not-really.

I run the program through, with a JOptionPane output, and I get gobbeldygook.

import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Pr50
{
public static void main(String[] args)
{
    JTextField time=new JTextField();
    JTextField interest=new JTextField();
    JTextField initial=new JTextField();
    Object[] fields={
        "Time to Maturity", time,
        "Interest rate", interest,
        "Initial deposit", initial
        };
    JOptionPane.showInputDialog(null, fields);

    JOptionPane.showMessageDialog(null, "The value of the fund is: "+initial);


    /*int x;
    double fin,rate;
    float init;
    fin=money.Compound(5,10,5500);*/
}
}

I really want to know how to fix that.

EDIT EDIT EDIT

I need to get the variables from the input window, but I get an error message stating that 'JText cannot be converted into int.'

Here's the code, in all it's unholy glory:

Main:

import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Pr50
{
public static void main(String[] args)
{
    fin=money.Compound(time,interest,initial);
    JTextField time=new JTextField();
    JTextField interest=new JTextField();
    JTextField initial=new JTextField();
    Object[] fields={
        "Time to Maturity", time,
        "Interest rate", interest,
        "Initial deposit", initial
        };
    JOptionPane.showInputDialog(null, fields);

    JOptionPane.showMessageDialog(null, "The value of the fund is: "+initial);


    int x;
    double fin,rate;
    float init;
    fin=money.Compound(time,interest,initial);
}
}

Method being called up:

public class money
{
public static double Compound(int time,double rate,float init)
{
    return init*Math.pow(1+(rate/100), time);
}
}
2

There are 2 best solutions below

3
On

From your question, I guess you're trying to get the value returned by the method Compound.

Simply call the method Compound inside your main method after obtaining variables time, rate and init using JOptionPane. For example:

double compound = Compound(time, rate, init);
System.out.println("The answer is " + compound);
2
On

There's a lot of weird stuff going on in that code, but it looks to me that this it what you are trying to do:

public float compound(int time, double rate, float init)
{
    return init * Math.pow(1 + (rate / 100), time);
}