Results 1 to 14 of 14
- 10-04-2012, 06:23 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
How do I set iBalance to the args value? :S
When I run it, all it says is "Input a number"...:(
any help appreciated...
Note: I am completely new to coding
EDIT: How do I set iBalance to the args value?
now it keeps displaying There is less than $500 in the account.
This is the what I'm trying to do:
A bank account starts out with an initial balance, iBalance. Interest is computed monthly at 6% per year (0.5% per month). Every month $500.00 is withdrawn to meet education expenses. After how many years is the account depleted?
Java Code:/* Class Args read a number from argument list and print its square. to run the program after compiling type: java Args 30 in command window. */ public class zzz { public static void main (String[] args) { float aNumber=0; // Read input argument if (args.length < 1){ System.out.println("Input a number"); System.exit(0); // Terminate the program if user has not input a number } else aNumber = Float.parseFloat(args[0]); double balance = iBalance + iBalance*interest; //initial balance with interest while (balance >= 500+500*interest) { balance = iBalance + iBalance*interest - 500; month = month + 1; } if (balance <= 500+500*interest) { System.out.println("There is less than $500 in the account."); } if (month >= 12) { year = month/12; month = month%12; System.out.println("The account will be depleted in "+year+"year(s) and"+month+"months"); } else if(month >=12){ System.out.println("The time it takes the account to be depleted is"+month+"month(s)"); } // Your codes go here System.out.println(aNumber*aNumber); // Sample output to test the program } }Last edited by nonny; 10-04-2012 at 07:40 AM.
- 10-04-2012, 06:39 AM #2
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
Re: I don't understand why my java code doesn't work.....:(
When you run this program you have to enter the arguments after the filename.
First compile the java file using "javac filename" and run the program using "java classfilename arguments". Here arguments is the value you are passing. Try this and let me know if it works.
- 10-04-2012, 06:41 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
- 10-04-2012, 06:54 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: I don't understand why my java code doesn't work.....:(
What would I call the value of args in my code?.. I used balance = args, and that failed..
- 10-04-2012, 07:59 AM #5
Member
- Join Date
- Jan 2012
- Posts
- 49
- Rep Power
- 0
Re: I don't understand why my java code doesn't work.....:(
What do you mean it failed?
- 10-04-2012, 08:01 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
- 10-04-2012, 08:01 AM #7
Re: I don't understand why my java code doesn't work.....:(
Please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-04-2012, 08:08 AM #8
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
- 10-04-2012, 10:04 AM #9
Re: I don't understand why my java code doesn't work.....:(
You can't assign a String[] value to a variable of type double. Learn about variables here: Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-04-2012, 10:40 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: I don't understand why my java code doesn't work.....:(
'args' is an array of Strings (representing the stuff entered at the end of the 'java <yourclass>' line).
So you want to take the argument that represents the balance (say args[0]) and assign that to the balance.
However args[0] is a String, so you need to find a method to parse a String and convert it into a double (which is what balance is).
Have a look at the Double API.Please do not ask for code as refusal often offends.
- 10-04-2012, 01:04 PM #11
Member
- Join Date
- Sep 2012
- Location
- Guntur, India
- Posts
- 27
- Rep Power
- 0
Re: I don't understand why my java code doesn't work.....:(
try this,
args[0]=Float.parseFloat(iBalance).toString();
First of all what is the use of setting the iBalance to the args value?
- 10-04-2012, 02:27 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: I don't understand why my java code doesn't work.....:(
Because they want to use the number in their calculations.
Hard to do sums with Strings...
At least this time you didn't link to your blog.Please do not ask for code as refusal often offends.
- 10-04-2012, 04:09 PM #13
Re: I don't understand why my java code doesn't work.....:(
Since when does Float#parseFloat(...) return a double? And how does assigning a value to args[0] make use of a command line argument?
Please learn Java before attempting to answer questions here. This is just one of many incorrect responses you have posted today.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-04-2012, 07:45 PM #14
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Why doesn't this code work?
By abv in forum New To JavaReplies: 3Last Post: 08-07-2012, 12:32 PM -
Java noob doesn't understand importing
By Javanoobguy in forum New To JavaReplies: 1Last Post: 04-05-2012, 11:35 PM -
My code doesn't work! Pane.showOptionDialog()
By 0070071 in forum AWT / SwingReplies: 5Last Post: 01-18-2012, 05:56 AM -
Repaint calling Statement Doesn't Work in my code ?
By nitin_daviet88 in forum CLDC and MIDPReplies: 2Last Post: 07-24-2010, 03:09 PM -
This code doesn't work. Why? thanks.
By seanzhang in forum New To JavaReplies: 10Last Post: 07-09-2010, 05:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks