Increment problem

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2111 Views - Last Post: 05 March 2012 - 05:12 PM Rate Topic: -----

#16 jdavi134   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Increment problem

Posted 04 March 2012 - 10:39 PM

View Postjon.kiparsky, on 04 March 2012 - 10:36 PM, said:

You've already declared most of those, so I'd expect if you actually put that line into your code you got a bunch of errors.

And you're probably also getting an error because you're declaring month as a double and formatting it as an int later on.

But yes, once you've fixed all the syntax the problem is in the logic. To solve it, find the place where you see the wrong number coming out and figure out how that number got wrong. It must have been set wrong - there are no little men in the machine changing your numbers. So go back to where you last set it and see what you did wrong. If it's not there, it's on a previous step, but work back until you find it.


I just couldn't explain it without giving the answer. It's late and i'm tired. Thanks for that.
Was This Post Helpful? 0
  • +
  • -

#17 Jagst3r15   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 21-December 10

Re: Increment problem

Posted 05 March 2012 - 02:01 PM

I added this but I cannot seem to get the declaration right, any help?

	  double  i, lastMonth;
	  int month;

This post has been edited by Jagst3r15: 05 March 2012 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#18 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Increment problem

Posted 05 March 2012 - 02:13 PM

Is lastMonth supposed to be an int? I know that i is a double, because it's your interest (that's a bad choice of name, generally when you see a variable i it's an int and it's being used as an index for a loop.
Was This Post Helpful? 0
  • +
  • -

#19 Jagst3r15   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 21-December 10

Re: Increment problem

Posted 05 March 2012 - 02:16 PM

Not sure, but I keep getting "error: variable i might not have been initialized" for the three variables. And, should I have something more like this for the loop (with a count thing instead of i?):

int count = 0;
      while (count < 5) {

Was This Post Helpful? 0
  • +
  • -

#20 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Increment problem

Posted 05 March 2012 - 02:20 PM

Local variables are not initialized for you. If you haven't set them to some value, the compiler will complain.
Was This Post Helpful? 0
  • +
  • -

#21 Jagst3r15   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 21-December 10

Re: Increment problem

Posted 05 March 2012 - 02:25 PM

So do I want to use a for loop instead?

Like:
for (rate = 5.0; rate <=8.0; rate=rate + 0.125)

Was This Post Helpful? 0
  • +
  • -

#22 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Increment problem

Posted 05 March 2012 - 02:29 PM

That looks fine to me. Try it and see if it works.
Was This Post Helpful? 0
  • +
  • -

#23 Jagst3r15   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 21-December 10

Re: Increment problem

Posted 05 March 2012 - 02:33 PM

Sweet!!!!!! No errors ;). I think there is still a problem with this code however, since I am only getting one line of output and it is wrong (8.125 4166.66 350000.0)

      monthlyPayment = (int)(monthlyPayment * 100) / 100.0 ;
      totalPayment = (int)(totalPayment * 100) / 100.0;
      System.out.println(rate +  "\t\t" + monthlyPayment + "\t\t" + totalPayment );


EDIT: Nvm, I think I need the month++ or something like that for it to increment, right?

This post has been edited by Jagst3r15: 05 March 2012 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#24 Jagst3r15   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 21-December 10

Re: Increment problem

Posted 05 March 2012 - 02:57 PM

View PostJagst3r15, on 05 March 2012 - 02:33 PM, said:

Sweet!!!!!! No errors ;). I think there is still a problem with this code however, since I am only getting one line of output and it is wrong (8.125 4166.66 350000.0)

      monthlyPayment = (int)(monthlyPayment * 100) / 100.0 ;
      totalPayment = (int)(totalPayment * 100) / 100.0;
      System.out.println(rate +  "\t\t" + monthlyPayment + "\t\t" + totalPayment );


EDIT: Nvm, I think I need the month++ or something like that for it to increment, right?


Just to clarify, this is my full code as it stands now:

import java.util.Scanner;

public class ComputeLoan {
  public static void main(String[] args) {
	  
	  Scanner input = new Scanner(System.in);
	
	  double  rate;
	  int month;
	  int lastMonth;

	  System.out.print("Enter loan amount: ");
	  double loanAmount = input.nextDouble();

	  System.out.print("Enter number of years: ");
	  int numOfYears = input.nextInt();

	  double annualInterestRate = 5;

	  double monthlyInterestRate = annualInterestRate/12;

	  double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - (Math.pow(1 / (1 + monthlyInterestRate), numOfYears * 12)));

	  double totalPayment = monthlyPayment * 12 *numOfYears;
	  
	  System.out.println();

	  System.out.println("Interest Rate \t Monthly Payment \t Total Payment");

	  for (rate = 5.0; rate <=8.0; rate=rate + 0.125);

      monthlyPayment = (int)(monthlyPayment * 100) / 100.0 ;
      totalPayment = (int)(totalPayment * 100) / 100.0;
      System.out.println(rate +  "\t\t" + monthlyPayment + "\t\t" + totalPayment );
	  
    }
  }

Was This Post Helpful? 0
  • +
  • -

#25 jdavi134   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Increment problem

Posted 05 March 2012 - 03:36 PM

This is a lot harder than it looks to do on a phone.

Anyways, I don't think that your monthly interest rate value is being calculated correctly. If your interest rate is 5 percent, then you would need to set it equal to it's decimal equivalent, which is .05.
Was This Post Helpful? 0
  • +
  • -

#26 Jagst3r15   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 21-December 10

Re: Increment problem

Posted 05 March 2012 - 03:39 PM

Thanks, that fixed that issue :). Still confused about the dang loop/increment thing...can it be done with a for loop or do i need the count++/i++ thingamabob.
Was This Post Helpful? 0
  • +
  • -

#27 jdavi134   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Increment problem

Posted 05 March 2012 - 04:11 PM

View PostJagst3r15, on 05 March 2012 - 03:39 PM, said:

Thanks, that fixed that issue :). Still confused about the dang loop/increment thing...can it be done with a for loop or do i need the count++/i++ thingamabob.


Well from what I can see, your for loop is doing nothing. You do not have anything within the for loop. You need to include the part where you print out all of your info with the use of curly braces.

Jack
Was This Post Helpful? 0
  • +
  • -

#28 Jagst3r15   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 21-December 10

Re: Increment problem

Posted 05 March 2012 - 05:12 PM

Wow I got it, had to move stuff around to make the loop work :) :

// James Geiger
// COMP 171
// Lab 3.11

import java.util.Scanner;

public class ComputeLoan {
  public static void main(String[] args) {
	  
	  Scanner input = new Scanner(System.in);
	
	  double  rate;
	  int month;
	  int lastMonth;

	  System.out.print("Enter loan amount: ");
	  double loanAmount = input.nextDouble();

	  System.out.print("Enter number of years: ");
	  int numOfYears = input.nextInt();

	  double annualInterestRate = .05;

	// Table headings
	System.out.println("Interest Rate \t Monthly Payment \t Total Payment");

	// Start loop
	for (rate = 5.0; rate <=8.0; rate=rate + 0.125) {
		
	  double monthlyInterestRate = rate/1200;

	// Calculation
	  double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - (Math.pow(1 / (1 + monthlyInterestRate), numOfYears * 12)));

	  double totalPayment = monthlyPayment * 12 *numOfYears;
	  
	  System.out.println();

	        monthlyPayment = (int)(monthlyPayment * 100) / 100.0 ;
		 totalPayment = (int)(totalPayment * 100) / 100.0;
	  			System.out.println(rate +  "\t\t" +
					monthlyPayment + "\t\t\t" + totalPayment);		
	  }
    }
  }

This post has been edited by Jagst3r15: 05 March 2012 - 05:12 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2