Java Program not displaying correctly

Java Program not displaying correctly, amount is all distorted. Do not

Page 1 of 1

8 Replies - 1324 Views - Last Post: 09 September 2007 - 12:49 PM Rate Topic: -----

#1 dogma992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-August 07

Java Program not displaying correctly

Posted 09 September 2007 - 12:11 PM

//Classes
import javax.swing.*;
import java.text.*;

public class MortgageCalculatorcr3
{
	public static void main (String[] args)
	{
		//Variables
		double[] interest = new double[4];
		double[] principle = new double[4];
		double[] monthlyPayments = new double[4];
		double[] payment = new double[4];
		double[] interestAmount = new double[4];
		int		paymentsPerPage,
				lengthOfPause,
				maxPayments;

		//Variables for loan 1
	  interest[1]			= 0.0535;	// interest rate
	  principle[1]		= 200000;	// principle
	  monthlyPayments[1]	= 84;		// amount of monthly payments

		//Variables for loan 2
	  interest[2]			= 0.055;	   // interest rate
	  principle[2]		= 200000;	// principle
	  monthlyPayments[2]	= 180;   // amount of monthly payments


		//Variables for loan 3
	  interest[3]			= 0.0575;	// interest rate
	  principle[3]		= 200000;	// principle
	  monthlyPayments[3]	= 360;   // amount of monthly payments


	   DecimalFormat decimalPlaces = new DecimalFormat("0.00"); //how the output is displayed

	  //Formulas for each loan 1-3
	  payment[1] = principle[1] * ((interest[1] / 12.0) / (1 - Math.pow((1 + (interest[1] / 12.0)), -monthlyPayments[1])));
		payment[2] = principle[2] * ((interest[2] / 12.0) / (1 - Math.pow((1 + (interest[2] / 12.0)), -monthlyPayments[2])));
		payment[3] = principle[3] * ((interest[3] / 12.0) / (1 - Math.pow((1 + (interest[3] / 12.0)), -monthlyPayments[3])));

   
		String paymentOutput;
		int pageCount = 0;

		{
			//Calculate payments for loans 1-3
			interestAmount[1] = ((interest[1] / 12) * principle[1]);
			principle[1] = (principle[1] - payment[1]) + interestAmount[1];
			interestAmount[2] = ((interest[2] / 12) * principle[2]);
			principle[2] = (principle[2] - payment[2]) + interestAmount[2];
			interestAmount[3] = ((interest[3] / 12) * principle[3]);
			principle[3] = (principle[3] - payment[3]) + interestAmount[3];

			//new output for change request #2
			System.out.println("");
			System.out.println("");
			System.out.println("				Loan 1	  Loan 2	 Loan3");
			   System.out.println("--------------------------------------------");
			paymentOutput = "  Amount: $  ";
			{
				paymentOutput = paymentOutput + padMe(decimalPlaces.format(payment[1]), 9) + "  ";
			}

			{
				paymentOutput = paymentOutput + "		   ";
			}

			{
				paymentOutput = paymentOutput + padMe(decimalPlaces.format(payment[2]), 9) + "  ";
			}

			{
				paymentOutput = paymentOutput + "		   ";
			}

			{
				paymentOutput = paymentOutput + padMe(decimalPlaces.format(payment[3]), 9);
			}
			System.out.println(paymentOutput);
			System.out.println("Interest: $  " + padMe(NoneIfZero(decimalPlaces.format(interestAmount[1])), 9) + "  " + padMe(NoneIfZero(decimalPlaces.format(interestAmount[2])), 9) + "  " + padMe(NoneIfZero(decimalPlaces.format(interestAmount[3])), 9));
			System.out.println(" Balance: $  " + padMe(NoneIfZero(decimalPlaces.format(principle[1])), 9) + "  " + padMe(NoneIfZero(decimalPlaces.format(principle[2])), 9) + "  " + padMe(NoneIfZero(decimalPlaces.format(principle[3])), 9));
			System.out.println("--------------------------------------------");
		   

					   {


				pageCount = 0;
			}
		}
		System.out.println("");
		System.out.println("");
	}

	public static String padMe(String padValue, int padSize)
	{
		String returnValue = padValue;
		for(int i = 0; i < (padSize - padValue.length()); i++)
		{
			returnValue = " " + returnValue;
		}
		return returnValue;
	}

	public static String NoneIfZero(String strValue)
	{
		double value = Double.parseDouble(strValue);
		String returnValue;
		if(value <= 0)
		{
			returnValue = " ";
		}
		else
		{
			returnValue = Double.toString(value);
		}
		return returnValue;
	}
}
EDIT: CODE TAGS

This post has been edited by PennyBoki: 09 September 2007 - 12:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Program not displaying correctly

#2 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Java Program not displaying correctly

Posted 09 September 2007 - 12:12 PM

What errors are you getting?
Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Java Program not displaying correctly

Posted 09 September 2007 - 12:14 PM

Hi dogma992, just as alpha said, what are the errors, post them and we'll see what we can do, and next time please use code tags.
Was This Post Helpful? 0
  • +
  • -

#4 dogma992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-August 07

Re: Java Program not displaying correctly

Posted 09 September 2007 - 12:25 PM

View PostPennyBoki, on 9 Sep, 2007 - 12:14 PM, said:

Hi dogma992, just as alpha said, what are the errors, post them and we'll see what we can do, and next time please use code tags.


I am not getting any errors its just not displaying correctly. I am trying to have all of the loans line up and I keep getting one of my amounts displayed all the way to the right. Sorry about forgetting the code tags
//Classes
import javax.swing.*;
import java.text.*;

public class MortgageCalculatorcr3
{
	public static void main (String[] args)
	{
		//Variables
		double[] interest = new double[4];
		double[] principle = new double[4];
		double[] monthlyPayments = new double[4];
		double[] payment = new double[4];
		double[] interestAmount = new double[4];
		int		paymentsPerPage,
				lengthOfPause,
				maxPayments;

		//Variables for loan 1
	  interest[1]			= 0.0535;	// interest rate
	  principle[1]		= 200000;	// principle
	  monthlyPayments[1]	= 84;		// amount of monthly payments

		//Variables for loan 2
	  interest[2]			= 0.055;	   // interest rate
	  principle[2]		= 200000;	// principle
	  monthlyPayments[2]	= 180;   // amount of monthly payments


		//Variables for loan 3
	  interest[3]			= 0.0575;	// interest rate
	  principle[3]		= 200000;	// principle
	  monthlyPayments[3]	= 360;   // amount of monthly payments


	   DecimalFormat decimalPlaces = new DecimalFormat("0.00"); //how the output is displayed

	  //Formulas for each loan 1-3
	  payment[1] = principle[1] * ((interest[1] / 12.0) / (1 - Math.pow((1 + (interest[1] / 12.0)), -monthlyPayments[1])));
		payment[2] = principle[2] * ((interest[2] / 12.0) / (1 - Math.pow((1 + (interest[2] / 12.0)), -monthlyPayments[2])));
		payment[3] = principle[3] * ((interest[3] / 12.0) / (1 - Math.pow((1 + (interest[3] / 12.0)), -monthlyPayments[3])));

   
		String paymentOutput;
		int pageCount = 0;

		{
			//Calculate payments for loans 1-3
			interestAmount[1] = ((interest[1] / 12) * principle[1]);
			principle[1] = (principle[1] - payment[1]) + interestAmount[1];
			interestAmount[2] = ((interest[2] / 12) * principle[2]);
			principle[2] = (principle[2] - payment[2]) + interestAmount[2];
			interestAmount[3] = ((interest[3] / 12) * principle[3]);
			principle[3] = (principle[3] - payment[3]) + interestAmount[3];

			//new output for change request #2
			System.out.println("");
			System.out.println("");
			System.out.println("				Loan 1	  Loan 2	 Loan3");
			   System.out.println("--------------------------------------------");
			paymentOutput = "  Amount: $  ";
			{
				paymentOutput = paymentOutput + padMe(decimalPlaces.format(payment[1]), 9) + "  ";
			}

			{
				paymentOutput = paymentOutput + "		   ";
			}

			{
				paymentOutput = paymentOutput + padMe(decimalPlaces.format(payment[2]), 9) + "  ";
			}

			{
				paymentOutput = paymentOutput + "		   ";
			}

			{
				paymentOutput = paymentOutput + padMe(decimalPlaces.format(payment[3]), 9);
			}
			System.out.println(paymentOutput);
			System.out.println("Interest: $  " + padMe(NoneIfZero(decimalPlaces.format(interestAmount[1])), 9) + "  " + padMe(NoneIfZero(decimalPlaces.format(interestAmount[2])), 9) + "  " + padMe(NoneIfZero(decimalPlaces.format(interestAmount[3])), 9));
			System.out.println(" Balance: $  " + padMe(NoneIfZero(decimalPlaces.format(principle[1])), 9) + "  " + padMe(NoneIfZero(decimalPlaces.format(principle[2])), 9) + "  " + padMe(NoneIfZero(decimalPlaces.format(principle[3])), 9));
			System.out.println("--------------------------------------------");
			 {
			}
		}
		System.out.println("");
		System.out.println("");
	}

	public static String padMe(String padValue, int padSize)
	{
		String returnValue = padValue;
		for(int i = 0; i < (padSize - padValue.length()); i++)
		{
			returnValue = " " + returnValue;
		}
		return returnValue;
	}

	public static String NoneIfZero(String strValue)
	{
		double value = Double.parseDouble(strValue);
		String returnValue;
		if(value <= 0)
		{
			returnValue = " ";
		}
		else
		{
			returnValue = Double.toString(value);
		}
		return returnValue;
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Java Program not displaying correctly

Posted 09 September 2007 - 12:29 PM

If you mean line up the output in the console, use the \t character (tab) and it should do the trick. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 dogma992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-August 07

Re: Java Program not displaying correctly

Posted 09 September 2007 - 12:34 PM

View Postalpha02, on 9 Sep, 2007 - 12:29 PM, said:

If you mean line up the output in the console, use the \t character (tab) and it should do the trick. Hope this helps.

Where would I place this?
Was This Post Helpful? 0
  • +
  • -

#7 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Java Program not displaying correctly

Posted 09 September 2007 - 12:37 PM

View Postdogma992, on 9 Sep, 2007 - 03:34 PM, said:

View Postalpha02, on 9 Sep, 2007 - 12:29 PM, said:

If you mean line up the output in the console, use the \t character (tab) and it should do the trick. Hope this helps.

Where would I place this?


Example:

System.out.println ("djsdfgklsbd \t\t\t\t X");
System.out.println ("djsdfklsbd \t\t\t\t X");
System.out.println ("djsdfggklsbd \t\t\t\t X");


In the above example, even if the strings's lengths are different, the X's will be lined up.
Was This Post Helpful? 0
  • +
  • -

#8 dogma992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-August 07

Re: Java Program not displaying correctly

Posted 09 September 2007 - 12:48 PM

View Postalpha02, on 9 Sep, 2007 - 12:37 PM, said:

View Postdogma992, on 9 Sep, 2007 - 03:34 PM, said:

View Postalpha02, on 9 Sep, 2007 - 12:29 PM, said:

If you mean line up the output in the console, use the \t character (tab) and it should do the trick. Hope this helps.

Where would I place this?


Example:

System.out.println ("djsdfgklsbd \t\t\t\t X");
System.out.println ("djsdfklsbd \t\t\t\t X");
System.out.println ("djsdfggklsbd \t\t\t\t X");


In the above example, even if the strings's lengths are different, the X's will be lined up.

Thanks for the help:-)
Was This Post Helpful? 0
  • +
  • -

#9 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Java Program not displaying correctly

Posted 09 September 2007 - 12:49 PM

u r welcome
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1