Results 1 to 14 of 14
  1. #1
    nonny is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    java.java is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  3. #3
    nonny is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: I don't understand why my java code doesn't work.....:(

    Quote Originally Posted by java.java View Post
    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.
    Thank you! It works. Now to fix my messed up code...

  4. #4
    nonny is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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..

  5. #5
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: I don't understand why my java code doesn't work.....:(

    What do you mean it failed?

  6. #6
    nonny is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: I don't understand why my java code doesn't work.....:(

    Quote Originally Posted by Reeling View Post
    What do you mean it failed?
    I mean it's incorrect.
    error: incompatible types
    double iBalance=args;
    ^
    required: double
    found: String[]

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: I don't understand why my java code doesn't work.....:(

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    nonny is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: I don't understand why my java code doesn't work.....:(

    Quote Originally Posted by DarrylBurke View Post
    Please go through the Forum Rules -- particularly the third paragraph.

    db
    Sorry. Won't happen again. I don't know how to change the title now. I tried and can only change my first posts title

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default 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)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default 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.

    ** This space for rent **

  11. #11
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default 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?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default 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.

    ** This space for rent **

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: I don't understand why my java code doesn't work.....:(

    Quote Originally Posted by gowthamgutha View Post
    try this,

    args[0]=Float.parseFloat(iBalance).toString();

    First of all what is the use of setting the iBalance to the args value?
    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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    nonny is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: I don't understand why my java code doesn't work.....:(

    Thanks for the help/links everyone

Similar Threads

  1. Why doesn't this code work?
    By abv in forum New To Java
    Replies: 3
    Last Post: 08-07-2012, 12:32 PM
  2. Java noob doesn't understand importing
    By Javanoobguy in forum New To Java
    Replies: 1
    Last Post: 04-05-2012, 11:35 PM
  3. My code doesn't work! Pane.showOptionDialog()
    By 0070071 in forum AWT / Swing
    Replies: 5
    Last Post: 01-18-2012, 05:56 AM
  4. Repaint calling Statement Doesn't Work in my code ?
    By nitin_daviet88 in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-24-2010, 03:09 PM
  5. This code doesn't work. Why? thanks.
    By seanzhang in forum New To Java
    Replies: 10
    Last Post: 07-09-2010, 05:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •