Help with program to compute simple interest

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 14520 Views - Last Post: 14 September 2010 - 02:07 PM Rate Topic: -----

#1 Guest_Nick*


Reputation:

Help with program to compute simple interest

Posted 14 September 2010 - 10:25 AM

This is my second week in a college java programming class, and our project is to write a program to compute simple interest while using JOptionPane to show it in a seperate window. I cant seem to get it to work, or get the formula right in java code. The formula he gave us to use is p(1+r)^n where p is the principle, r is rate, and n is time in years. He wants us to use a string so when the pop up box pops up asking for "Enter the principle, rate and period." we just type each number with a space in between. The answer should then pop up in JOptionPane and say "Balance after (however many years you type in)=$(whatever the answer is)" we are also told to only allow two decimal places after the answer. This is the code i have written, any help to fix it or input would be beyond appreciated thanks ahead of time guys

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.lang.String;


 public class Interest 
 {
	 public static void main( String args[] )
	 {

		String prompt = "Enter the principle, rate and period.";
		String input = JOptionPane.showInputDialog(prompt);

		DecimalFormat precisionTwo = new DecimalFormat( "0.00" );		


		//compute result
		Scanner scanner = new Scanner(input);
		String principleString = scanner.next();
		BigInteger principle = new BigInteger(principleString);
		String rateString = scanner.next();
		int period = scanner.next();
		BigInteger result = principle * rate ^ period + 1;
		
		
		


		// display results
		String output = principle * rate ^ period + 1;
		JOptionPane.showMessageDialog(null, output);







	}
 }


Is This A Good Question/Topic? 0

Replies To: Help with program to compute simple interest

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: Help with program to compute simple interest

Posted 14 September 2010 - 10:30 AM

1. Exponents use Math.pow(num, power). The ^ symbol has other meaning in most high level programming languages.
2. Input for the JOptionPane will not use Scanner, it would look something akin to:

String response = JOptionPane.showInputDialog("Enter the principle, rate and period.");
//now we have to get all the values into their own variables, since the answer is delimited by a space
//you can use a StringTokenizer or String.split()
//depending on how fragile the program is allowed to be


Was This Post Helpful? 0
  • +
  • -

#3 Guest_Nick*


Reputation:

Re: Help with program to compute simple interest

Posted 14 September 2010 - 10:35 AM

im pretty sure im using the String.split, how would i get all the variables assigned to a specific letter, so i can go about assigning them in order they type and then from there they will be plugged into the formula? and i still cant figure out how to write the formula correct in code
Was This Post Helpful? 0

#4 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Help with program to compute simple interest

Posted 14 September 2010 - 10:36 AM

You already have the answer from the user in the input variable:
String input = JOptionPane.showInputDialog(prompt);


Now as KYA said you can use the split method of the String class. Using the blank space as delimiter In order to get the individual values from the string. Then parse this values to float types and complete the maths operations.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Nick*


Reputation:

Re: Help with program to compute simple interest

Posted 14 September 2010 - 10:42 AM

can you write a sample of code, doesn't have to be exact to what my specifications are, just so i can get an idea how to assign a letter to each variable and then inturn take those letters and plug them into the equation? If thats confusion i cant figure out, ive tried for hours to figure this out, but i cant figure out how to get it to read the number they typed in and then turn that "number" into a variable which will in turn be plugged into the equation
Was This Post Helpful? 0

#6 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Help with program to compute simple interest

Posted 14 September 2010 - 10:43 AM

I think it would be:
principle * Math.pow(1+(rate/100), years);


You are missing the years in the input.
Was This Post Helpful? 0
  • +
  • -

#7 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Help with program to compute simple interest

Posted 14 September 2010 - 10:48 AM

In order to get individual values from a String:
String s = "a b c d";
String[] values = s.split(" ");

String first = values[0];
String second = values[1];

//and so on...



Then convert:
float num = Float.parseFloat(stringValue);


Was This Post Helpful? 0
  • +
  • -

#8 Guest_Nick*


Reputation:

Re: Help with program to compute simple interest

Posted 14 September 2010 - 10:49 AM

So i edited my code and im still getting a boatload of errors, where am i going wrong with it?

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.lang.String;


 public class Interest 
 {
	 public static void main( String args[] )
	 {

		String prompt = "Enter the principle, rate and period.";
		String input = JOptionPane.showInputDialog(prompt);

		DecimalFormat precisionTwo = new DecimalFormat( "0.00" );		


		//compute result
		Scanner scanner = new Scanner(input);
		String principleString = scanner.next();
		BigInteger principle = new BigInteger(principleString);
		String rateString = scanner.next();
		int period = scanner.next();
		BigInteger result = principle * Math.pow(1+(rate/100), years);
		
		
		


		// display results
		String output = principle * Math.pow(1+(rate/100), years);
		JOptionPane.showMessageDialog(null, output);




	}
 }


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 14 September 2010 - 11:37 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0

#9 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: Help with program to compute simple interest

Posted 14 September 2010 - 10:49 AM

Assuming your program does not have to account for user error, it could be as simple as:

//order principle, rate, period

String input = JOptionPane.showInputDialog("Enter the principle, rate and period separated by a space.");
//ideal input: 3000.34 0.05 4
//you have to deterimne if you want the percent APR or the decimal equivalent 
//from the user and convert/handle accordingly 
String[] contents = input.split(" " ); //we should now have 3 entries
double principle = Double.parseDouble(contents[0]);
//likewise for the next two at indexes 1 and 2
//then use these variables to calculate and then display


Was This Post Helpful? 0
  • +
  • -

#10 Guest_Nick*


Reputation:

Re: Help with program to compute simple interest

Posted 14 September 2010 - 10:58 AM

Sorry for all the questions, im still lost, ive only had 4 classes as we only have 2 a week and were off for 1 of them, but here is my code, what do i have to edit to get it to do what i want? im still confused how to assign numbers to variables which will inturn be plugged into the equation

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.lang.String;
import java.util.Scanner;


 public class Interest 
 {
	 public static void main( String args[] )
	 {

		String prompt = "Enter the principle, rate and period.";
		String input = JOptionPane.showInputDialog(prompt);

		DecimalFormat precisionTwo = new DecimalFormat( "0.00" );		


		//compute result
		String[] contents = input.split(" ");


		// display results
		String output = principle * Math.pow(1+(rate/100), years);
		JOptionPane.showMessageDialog(null, output);




	}
 }

This post has been edited by JackOfAllTrades: 14 September 2010 - 11:38 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0

#11 Guest_Nick*


Reputation:

Re: Help with program to compute simple interest

Posted 14 September 2010 - 11:12 AM

I am now down to 4 errors, here are my errors, and the my code will be after, how would i go about fixing the errors? thanks

Errors:

C:\Users\Nick\Desktop\java>javac Interest.java
Interest.java:26: cannot find symbol
symbol : variable principle
location: class Interest
String output = principle * Math.pow(1+(rate/100), years);
^
Interest.java:26: cannot find symbol
symbol : variable rate
location: class Interest
String output = principle * Math.pow(1+(rate/100), years);
^
Interest.java:26: cannot find symbol
symbol : variable years
location: class Interest
String output = principle * Math.pow(1+(rate/100), years);
^
Interest.java:26: incompatible types
found : int
required: java.lang.String
String output = principle * Math.pow(1+(rate/100), years);
^
4 errors




Code:

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.lang.String;
import java.util.Scanner;


 public class Interest 
 {
	 public static void main( String args[] )
	 {

		String prompt = "Enter the principle, rate and period.";
		String input = JOptionPane.showInputDialog(prompt);

		DecimalFormat precisionTwo = new DecimalFormat( "0.00" );		


		//compute result
		Scanner scanner = new Scanner(input);
		String principleString = scanner.next();
		String rateString = scanner.next();
		String yearsString = scanner.next();


		// display results
		String output = principle * Math.pow(1+(rate/100), years);
		JOptionPane.showMessageDialog(null, output);




	}
 }

This post has been edited by JackOfAllTrades: 14 September 2010 - 11:38 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0

#12 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: Help with program to compute simple interest

Posted 14 September 2010 - 11:12 AM

Look at what I wrote.

You need to declare three variable, principle, rate, and time. Name them appropriately. Then parse each index of the resulting String array sequentially based on how you told the user to format said input.

i.e. P R Y or R P Y, etc...

principal rate time makes the most sense in my opinion
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Nick*


Reputation:

Re: Help with program to compute simple interest

Posted 14 September 2010 - 11:14 AM

the carrot in the first error is to be under the "p" in principle in the equation, in the second error its to be under "r" in rate in the equation, in the third its to be under "y" in years in the equation and in the last one its to be under "*" in the equation
Was This Post Helpful? 0

#14 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3207
  • View blog
  • Posts: 19,239
  • Joined: 14-September 07

Re: Help with program to compute simple interest

Posted 14 September 2010 - 11:17 AM

You do not need Scanners.

The dialog box pops up you enter the three items separated by a space.
Split that string into a string array using space as your delimiter.
Parse each index (there will be three) and assign the value to the respective variable.
Then, and only then, calculate it.
Display.
Was This Post Helpful? 0
  • +
  • -

#15 marshn07   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 14-September 10

Re: Help with program to compute simple interest

Posted 14 September 2010 - 11:35 AM

is there a tutorial that shows how to pull items from a string and assign them to variables? i just cant seem to understand it from reading it, i learn alot easier by seeing it physically or trying it myself
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2