Java Newb

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Travis Ray

    Java Newb

    Hi, I just started a java class and I have this assignment and I'm
    stumped... I realize the code is messy... but I just started... so go easy
    on me :)

    Java Problem
    I am working on this assignment. The user enters a loan amount, number of
    years to repay, and annual percentage rate. The project is supposed to
    output a table for each month's payment information. I wrote the attached
    code and it works well. However, there is a lot of extra code to try and
    get around a formatting problem. The output prints out in columns... when
    an amount is something like 32.50 it prints out as 32.5 and messes up my
    columns... very fustrating. As you will see in the code, I have attempted
    to take steps to prevent this. It works 95% of the time... however... it
    sometimes doesn't add the 0... and it sometimes addes a zero to the end of a
    number that should not have a zero at the end... for example... it
    occassionally turns something like 29.65 into 29.650. this screws up my
    columns further. Is there a way to force 2 decimal places in a java
    program? Or is there a way to align columns other than putting spacer
    strings inbetween data elements?



    import javax.swing.JOp tionPane;

    public class Loan {
    public static void main(String[] args) {
    double annualInterestR ate;
    int numOfYears = 0;
    double loanAmount = 0;
    double monthlyPayment = 0;
    double interest = 0;
    double principal = 0;
    double principalpay = 0;
    double payments = 0;
    String spacer1="";
    String spacer2="";
    String spacer3="";

    String loanAmountStrin g = JOptionPane.sho wInputDialog(nu ll,
    "Enter Loan Amount:","Loan Amount",JOption Pane.QUESTION_M ESSAGE);

    loanAmount=
    Double.parseDou ble(loanAmountS tring);

    String numOfYearsStrin g = JOptionPane.sho wInputDialog(nu ll,
    "Enter Number of Years:","Number of Years",
    JOptionPane.QUE STION_MESSAGE);

    numOfYears = Integer.parseIn t(numOfYearsStr ing);

    String annualInterestR ateString = JOptionPane.sho wInputDialog(nu ll,
    "Enter Annual Interest Rate:","Annual Interest Rate",
    JOptionPane.QUE STION_MESSAGE);

    annualInterestR ate =
    Double.parseDou ble(annualInter estRateString);

    principal=loanA mount;
    payments=numOfY ears*12;
    int mypayments=(int )payments;

    String[][] TableList;
    TableList=new String[mypayments+1][4];

    TableList[0][0] = "Payment#";
    TableList[0][1] = "Interest";
    TableList[0][2] = "Principal" ;
    TableList[0][3] = "Balance";

    for(int i=1;i < mypayments+1; i++)
    {
    TableList[i][0]=String.valueOf (i);
    }


    String output=TableLis t[0][0] + " " + TableList[0][1] + " " +
    TableList[0][2] + " " + TableList[0][3];

    for(int i=1; i < mypayments+1; i++)
    {
    interest = (principal*(ann ualInterestRate/100))/12;
    monthlyPayment = principal*((ann ualInterestRate/100)/12)/
    (1-(Math.pow(1/(1+((annualInte restRate/100)/12)), payments)));
    principalpay = monthlyPayment - interest;
    principal = principal - principalpay;
    monthlyPayment = (int) (monthlyPayment * 100) / 100.0;
    double myinterest = (int)(interest * 100) / 100.0;
    double myprincipal = (int)(principal * 100) / 100.0;
    double myprincipalpay = (int)(principal pay * 100) / 100.0;
    int testint=(int)(m yinterest*100);
    int testprin=(int)( myprincipal*100 );
    int testpay=(int)(m yprincipalpay*1 00);

    TableList[i][1]=String.valueOf (myinterest);
    TableList[i][2]=String.valueOf (myprincipalpay );
    TableList[i][3]=String.valueOf (myprincipal);

    if(testint % 10 == 0)
    TableList[i][1]=TableList[i][1]+'0';
    if(testprin % 10 == 0)
    TableList[i][3]=TableList[i][3]+'0';
    if(testpay % 10 == 0)
    TableList[i][2]=TableList[i][2]+'0';
    payments--;

    spacer1="...... ...";
    spacer2="...... ";
    spacer3="...... ";

    if(i > 9)
    spacer1="...... ..";
    if(i > 99)
    spacer1="...... .";
    if(interest>99. 99)
    spacer2="....." ;
    if(interest<10)
    spacer2="...... .";
    if(principalpay <10)
    spacer3="...... ";

    output = output + "\n\r" + TableList[i][0] +
    spacer1 + TableList[i][1] +
    spacer2 + TableList[i][2] +
    spacer3 + TableList[i][3];


    }


    System.out.prin tln(output);




    System.exit(0);
    }
    }


  • Robert Tyrie

    #2
    Re: Java Newb

    On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <travrichard@co mcast.net>
    wrote:
    [color=blue]
    > Or is there a way to align columns other than putting spacer
    > strings inbetween data elements?[/color]

    Take a look at the DecimalFormat class,




    Comment

    • Robert Tyrie

      #3
      Re: Java Newb

      On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <travrichard@co mcast.net>
      wrote:
      [color=blue]
      > Or is there a way to align columns other than putting spacer
      > strings inbetween data elements?[/color]

      Take a look at the DecimalFormat class,




      Comment

      • Travis Ray

        #4
        Re: Java Newb

        It worked after a long time of studying the NumberFormat and Locale classes.
        Thank You :)

        "Robert Tyrie" <robtyrie@yahoo .ca> wrote in message
        news:opr2berkd4 [email protected] eco.ca...[color=blue]
        > On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <travrichard@co mcast.net>
        > wrote:
        >[color=green]
        > > Or is there a way to align columns other than putting spacer
        > > strings inbetween data elements?[/color]
        >
        > Take a look at the DecimalFormat class,
        >
        > http://java.sun.com/j2se/1.4.2/docs/...malFormat.html
        >
        >[/color]


        Comment

        • Travis Ray

          #5
          Re: Java Newb

          It worked after a long time of studying the NumberFormat and Locale classes.
          Thank You :)

          "Robert Tyrie" <robtyrie@yahoo .ca> wrote in message
          news:opr2berkd4 [email protected] eco.ca...[color=blue]
          > On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <travrichard@co mcast.net>
          > wrote:
          >[color=green]
          > > Or is there a way to align columns other than putting spacer
          > > strings inbetween data elements?[/color]
          >
          > Take a look at the DecimalFormat class,
          >
          > http://java.sun.com/j2se/1.4.2/docs/...malFormat.html
          >
          >[/color]


          Comment

          • hiwa

            #6
            Re: Java Newb

            If your problem is text formatting for number strings, you should use
            java.text.Forma t and its derivatives -- especially DecimalFormat.

            Comment

            • hiwa

              #7
              Re: Java Newb

              If your problem is text formatting for number strings, you should use
              java.text.Forma t and its derivatives -- especially DecimalFormat.

              Comment

              • Denz

                #8
                Re: Java Newb


                "Robert Tyrie" <robtyrie@yahoo .ca> wrote in message
                news:opr2berkd4 [email protected] eco.ca...[color=blue]
                > On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <travrichard@co mcast.net>
                > wrote:
                >[color=green]
                > > Or is there a way to align columns other than putting spacer
                > > strings inbetween data elements?[/color]
                >
                > Take a look at the DecimalFormat class,
                >
                > http://java.sun.com/j2se/1.4.2/docs/...malFormat.html[/color]

                As an *exercise*, might also want to do the formatting manually?
                .... by using String .indexOf('.') and .length() methods to determine how
                many charaters after the decimal point, then add the append the appropriate
                number of zeros to give you two decimal places. For alligning columns,
                probably need to use .length() on individual strings to determine how many
                spaces to use in padding to get column alignment.


                Comment

                • Denz

                  #9
                  Re: Java Newb


                  "Robert Tyrie" <robtyrie@yahoo .ca> wrote in message
                  news:opr2berkd4 [email protected] eco.ca...[color=blue]
                  > On Sat, 24 Jan 2004 21:40:43 -0500, Travis Ray <travrichard@co mcast.net>
                  > wrote:
                  >[color=green]
                  > > Or is there a way to align columns other than putting spacer
                  > > strings inbetween data elements?[/color]
                  >
                  > Take a look at the DecimalFormat class,
                  >
                  > http://java.sun.com/j2se/1.4.2/docs/...malFormat.html[/color]

                  As an *exercise*, might also want to do the formatting manually?
                  .... by using String .indexOf('.') and .length() methods to determine how
                  many charaters after the decimal point, then add the append the appropriate
                  number of zeros to give you two decimal places. For alligning columns,
                  probably need to use .length() on individual strings to determine how many
                  spaces to use in padding to get column alignment.


                  Comment

                  Working...