Hi all,
Me again. I am having issues with a looping program. My current issue right now is that it wont compile, it keeps saying i need another perentheses at line 20. This program is suppose to calculate the investment and show for every year. The year and amount of investment is input by the user. So if the user puts in 8 years, it would show for year 1, 2, 3 and so on. I know this program probably needs work, it was quickly put together so any hints or explinations on this would be great. Here is my code:

import javax.swing.JOptionPane;
public class Investment
{
    public static void main(String[] args)
    {
        String inputInvest;
        String yearsInvest;
        double rate = 0.8;
        double investment;
        int years;
        int yearsloop = 1;
        double balance;
    {
    inputInvest = JOptionPane.showInputDialog(null,
        "What is the amount of the investment?");
    yearsInvest = JOptionPane.showInputDialog(null,
        "How many years will this investment be?");
    investment = Double.parseDouble(inputInvest);
    years = Integer.parseInteger(yearsInvest);
    while(yearsloop !> years)
    {
        balance = investment * return;
        yearsloop = JOption.showConfirmDialog(null,
            "After " + yearsloop + "at a 8% return" +
            "your balance is " + balance + "Do you" +
            "want to see your balance after another year?");
        yearsloop = yearloop + 1;
    }
    System.exit(0);
    }
}
}

Thanks much,
Sam

Recommended Answers

All 14 Replies

it keeps saying i need another perentheses

What is the "it"? Do you get errors when compiling the program?

What is the { on line 13 for?

public static void main(String[] args)
    {
        String inputInvest;
        String yearsInvest;
        double rate = 0.8;
        double investment;
        int years;
        int yearsloop = 1;
        double balance;
    {// this is your problem

The following errors should be corrected.

The code line 19 should be:
years = Integer.parseInt(yearsInvest);

The code line 20 should be:
while(yearsloop < years)

The code line 22 should be:
balance = investment * Math.pow(1.0 +rate/100, yearsloop);

The code line 23 should be:
JOptionPane.showConfirmDialog(null,

The code line 27 should be:
yearsloop = yearsloop + 1;

The output of the values in the JOptionPane.showConfirmDialog windonw shoud be formated.

I got it working mostly, but im wondering, can you use the and operator (&&) with a while statement? I want to have on mine:
while(yearsloop is <= years) && (selection == JOptionPane.YES_OPTION)
refer to line 20 on the above code. The selection variable will be an integer and have its own JOptionPane. I do not have this in my current code up top. My main question though is if i can have multiple conditions set with my while statement. I have the things already corrected that was given to me. Thanks everyone for the input.

Thanks,
Sam

Certainly you can have multiple conditions. In your example above though, you should have an outer set of parenthesis around the entire expression. The parens around each clause are optional.ie

while(yearsloop <= years && selection == JOptionPane.YES_OPTION)

Thanks, but now i have run into a new problem. I have the extra condition in there now but my problem is that if when the dialog pops up and asks if you want to see next years amount, if you answer no, it doesnt exit, it shows the next amount any way, im not sure if this is due to the position of my system.exit(0) line. Here is what i have:

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Investment
{
    public static void main(String[] args)
    {
       int selection;
        String inputInvest;
        String yearsInvest;
        DecimalFormat money = new DecimalFormat("$0.00");
       double rate = 0.8;
        double investment;
        int years;
        int yearsloop = 1;
        double balance;
    inputInvest = JOptionPane.showInputDialog(null,
        "What is the amount of the investment?");
    yearsInvest = JOptionPane.showInputDialog(null,
        "How many years will this investment be?");
    investment = Double.parseDouble(inputInvest);
    years = Integer.parseInt(yearsInvest);
    selection = JOptionPane.showConfirmDialog(null,
    "Would you like to see the first year?");
    while(selection == JOptionPane.YES_OPTION && yearsloop <= years) 
    {
    {
        balance = investment * Math.pow(1.0 + rate/100, yearsloop);
        JOptionPane.showMessageDialog(null,
            "After " + yearsloop + " years at an 8% return" +
            " your balance is " + (money.format(balance)));
        }
        selection = JOptionPane.showConfirmDialog(null,
        "Do your want to see another year?");
        yearsloop = yearsloop + 1;
    }
        System.exit(0);
}
}

Also, im assuming due to my multiple conditions and they way it is coded, the last confirm box comes up asking if you want to see the next year after the last time, like if the user inputs 8, it will show the eigth time but still the dialog box, but no matter what the choice, it ends. Is there a way to end the loop without it asking about the next year again after that eigth time? Im trying my best on this one, been on my couch doing this for the past 3 hours.

Thanks a lot,
Sam

I'd say you just need to check that your year count is less than the total before you prompt for another year.

Hitting "No" worked just fine for me. The program exited as expected.

ok finally got this part of things, it works great, i just have one more thing on it to do, have to add some input validation but i plan on giving that a go first. Thanks a lot Ezzaral for your help, even just the ideas on what i can do helps me to get the ball rolling. Here is what i have:

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Investment
{
    public static void main(String[] args)
    {
       int selection;
        String inputInvest;
        String yearsInvest;
        DecimalFormat money = new DecimalFormat("$0,000");
       double rate = 0.8;
        double investment;
        int years;
        int yearsloop = 1;
        double balance;
    inputInvest = JOptionPane.showInputDialog(null,
        "What is the amount of the investment?");
    yearsInvest = JOptionPane.showInputDialog(null,
        "How many years will this investment be?");
    investment = Double.parseDouble(inputInvest);
    years = Integer.parseInt(yearsInvest);
    selection = JOptionPane.showConfirmDialog(null,
    "Would you like to see the first year?");
    while(selection == JOptionPane.YES_OPTION && yearsloop <= years) 
    {
    {
        balance = investment * Math.pow(1.0 + rate/100, yearsloop);
        JOptionPane.showMessageDialog(null,
            "After " + yearsloop + " year(s) at an 8% return" +
            " your balance is " + (money.format(balance)));
    }
        if(years != yearsloop)
        selection = JOptionPane.showConfirmDialog(null,
        "Do your want to see another year?");
        yearsloop = yearsloop + 1;
    }
        System.exit(0);
}
}

Thanks much,
Sam

Hi all, I am having issues with my input validation. I cant get it to compile, the compiler says i need another ")" on line 22. Im not sure why there should be one there though. I know i am not fully done with validating everything, but i figured since i cant get one i might as well not start on the rest haha. Im basically trying to make it so if the investment amount put in is 0, an error message comes up. I think what i have should work but the compiler doesnt like this.

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Investment2
{
    public static void main(String[] args)
    {
       int selection;
        String inputInvest;
        String yearsInvest;
        String rateInvest;
        DecimalFormat money = new DecimalFormat("$0,000.00");
       double rate;
        double investment;
        int years;
        int yearsloop = 1;
        double balance;
    inputInvest = JOptionPane.showInputDialog(null,
        "What is the amount of the investment?");
        investment = Double.parseDouble(inputInvest);
    while(investment == 0)
    {
    inputInvest = JOptionPane.showInputDialog(null
    "Investment must be more than $0");
    investment = Double.parseDouble(inputInvest);
    }
    yearsInvest = JOptionPane.showInputDialog(null,
        "How many years will this investment be?");
    rateInvest = JOptionPane.showInputDialog(null,
        "What is the interest rate?");
    years = Integer.parseInt(yearsInvest);
    rate = Double.parseDouble(rateInvest);
    selection = JOptionPane.showConfirmDialog(null,
    "Would you like to see the first year?");
    while(selection == JOptionPane.YES_OPTION && yearsloop <= years) 
    {
    {
        balance = investment * Math.pow(1.0 + rate/100, yearsloop);
        JOptionPane.showMessageDialog(null,
            "After " + yearsloop + " year(s) at an 8% return" +
            " your balance is " + (money.format(balance)));
    }
        if(years != yearsloop)
        selection = JOptionPane.showConfirmDialog(null,
        "Do your want to see another year?");
        yearsloop = yearsloop + 1;
    }
        System.exit(0);
}
}

Thanks,
Sam

Line 22 is missing something ;). Just compare it with the other 3 showInputDialog lines and I'm sure you will see it.

(1) A comma is missing at the end of line 22
(2) input validation also includes other invalid cases, such as NumberFormatException, negative values for investment or number of years.

yea, i caught the comma at the end of line 22, stupid sytax error. Im working on the rest of the validation now, it only has to be simple validation such as if its a 0, have an error. Thank you both.

Sam

I got it done thanks to everyones tips. Here is what i got:

import javax.swing.JOptionPane;
 import java.text.DecimalFormat;
 public class Investment2
        {
             public static void main(String[] args)
          {
              int selection;
                 String inputInvest;
              String yearsInvest;
              String rateInvest;
              DecimalFormat money = new DecimalFormat("$0,000.00");
              double rate;
              double investment;
              int years;
              int yearsloop = 1;
              double balance;
              inputInvest = JOptionPane.showInputDialog(null,
                     "What is the amount of the investment?");
                  investment = Double.parseDouble(inputInvest);
                  while(investment == 0)
              {
                  inputInvest = JOptionPane.showInputDialog(null,
                  "The investment must be more than $0");
                   investment = Double.parseDouble(inputInvest);
                }
               yearsInvest = JOptionPane.showInputDialog(null,
                  "How many years will this investment be?");
                    years = Integer.parseInt(yearsInvest);
                while(years == 0)
                {
                yearsInvest = JOptionPane.showInputDialog(null,
                    "The amount of years must be more than 0");
                    years = Integer.parseInt(yearsInvest);
                }
              rateInvest = JOptionPane.showInputDialog(null,
                     "What is the interest rate?");
                    rate = Double.parseDouble(rateInvest);
                while(rate == 0)
                {
                rateInvest = JOptionPane.showInputDialog(null,
                    "The rate must be more than 0%");
                    rate = Double.parseDouble(rateInvest);
                }
              years = Integer.parseInt(yearsInvest);
              rate = Double.parseDouble(rateInvest);
              selection = JOptionPane.showConfirmDialog(null,
                  "Would you like to see the first year?");
              while(selection == JOptionPane.YES_OPTION && yearsloop <= years)
                {
                {
                    balance = investment * Math.pow(1.0 + rate/100, yearsloop);
                   JOptionPane.showMessageDialog(null,
                        "After " + yearsloop + " year(s) at an 8% return" +
                        " your balance is " + (money.format(balance)));
                }
                    if(years != yearsloop)
                    selection = JOptionPane.showConfirmDialog(null,
                        "Do your want to see another year?");
                    yearsloop = yearsloop + 1;
                }
                System.exit(0);
            }
        }

Also an unrelated question, if i were to copy my code back out of here in the future because i screwed it up, it there a quick way to take the numbers back out because the numbers (1,2,3...so on) always come with. I screwed up my code before and had to copy it back in, it was a pain.

Thanks much,
Sam

lines 44 and 45 are redundant.

it there a quick way to take the numbers back out

click the "Toggle Plain Text" above your code before copy it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.