how do i get rid of my error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sparky159357
    New Member
    • Jun 2010
    • 5

    #1

    how do i get rid of my error

    i need to get rid of my error just compile and run
    help!!!
    it is for adding up monthly income dont copy my idea!!!

    help i am n00b

    i know alot of stuff is not used it is not even half way done!!

    ps it is very messy i have still not cleaned it up

    pss my batery is dieing so i have to type fast i deleted a section and cant get it back what i am trying to do is add up the array Bnum HELP!!!!!


    Code:
    import java.util.Scanner;
    
    public class Money {
    	public static void main(String[] args){
    		
    		Scanner Scan = new Scanner(System.in);
    		double Income, Tax, TaxTotal, Bills, OfficeSpace, LeftOver, Bill;
    		int NBill, i, a, b;
    		
    		Bills = 0.;
    		i = 1;
    		
    		System.out.println("Imput this month's income:");
    		
    		Income = Scan.nextDouble();
    		
    		System.out.println("Your current balence is $" + Income );
    		System.out.println("How much of your income will you save for tax? (percentage, no percentage sign, whole num.)");
    		
    		Tax = Scan.nextDouble();
    		
    		System.out.println("$" + Income * (Tax / 100) + " goes to the Unchel Sam.");
    		
    		TaxTotal = Income * (Tax / 100);
    		Income = Income - TaxTotal;
    		
    		System.out.println("Your new current balence is $" + Income );
    		System.out.println("How many bills do you have?");
    		
    		NBill = Scan.nextInt();
    		
    		double[] Bnum = new double[NBill];
    		Bnum[0] = 0;
    		
    		do{
    			System.out.println("how much is value of bill " + i);
    			Bnum[i] = Scan.nextDouble();
    			i++;
    			
    		}while(i <= NBill);
    
    
    
    		\\ this is deleted part
    
    
    		System.out.println("Your new current value of used money is $" + Bnum);
    		
    
    		
    	}
    }
    Last edited by Niheel; Jun 25 '10, 12:20 AM. Reason: please use code tags to display code
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    OK. What you want to achieve? And what error's you are getting?

    I suspect you are getting array index out of bound exception.

    Code:
    System.out.println("Your new current value of used money is $" + Bnum);
    What you want to print here?

    Please post error messages too. And please wrap the code in code block.

    Regards
    Dheeraj Joshi

    Comment

    • sparky159357
      New Member
      • Jun 2010
      • 5

      #3
      Originally posted by dheerajjoshim
      OK. What you want to achieve? And what error's you are getting?

      I suspect you are getting array index out of bound exception.

      Code:
      System.out.println("Your new current value of used money is $" + Bnum);
      What you want to print here?

      Please post error messages too. And please wrap the code in code block.

      Regards
      Dheeraj Joshi
      the error is Exception in thread "main" java.lang.Array IndexOutOfBound sException: 2
      at Income.main(Mon ey.java:37) the 2 is for how many bills you say you have so if i say i have 5 bill then the 2 will change to a 5

      i am trying to make it so i can put in my monthly income and see how much money i have left over so say i made $200 this month (i am not poor) i would save 20% for tax then i say how many bills i have and the i add it up.

      i am new at this i am only half way through the java book i am reading so if you have any recommendations i would happily take them.


      also i see your picture is the eclipse symbol if you know any tricks for the program it would be much appreciated

      sorry there is a bunch of stuff not used i am a very unorganized person and i half been trying to fix this error for 3 days strait last night was the only night i got some sleep

      thank you for taking your time to help me

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Indexes start at index 0.
        Change it to
        while(i < NBill);
        removing the equal sign.
        Couldn't you tell you were trying to input one too many bills?

        Comment

        • sparky159357
          New Member
          • Jun 2010
          • 5

          #5
          Originally posted by jkmyoung
          Indexes start at index 0.
          Change it to
          while(i < NBill);
          removing the equal sign.
          Couldn't you tell you were trying to input one too many bills?
          i have not slept in 3 days how should i know and plus i changed it and ran it and it said (this is what the entire program said )
          "Imput this month's income:
          200
          Your current balence is $200.0
          How much of your income will you save for tax? (percentage, no percentage sign, whole num.)
          20
          $40.0 goes to the Unchel Sam.
          Your new current balence is $160.0
          How many bills do you have?
          2
          how much is value of bill 1
          2
          Your new current value of used money is $[D@1fb8ee3
          "


          compile it and run you will see what i mean

          Comment

          • Dheeraj Joshi
            Recognized Expert Top Contributor
            • Jul 2009
            • 1129

            #6
            Code:
            System.out.println("Your new current value of used money is $" + Bnum);
            It Should print the value in ith index.

            So change it to

            Code:
            System.out.println("Your new current value of used money is $" + Bnum[index]);
            Regards
            Dheeraj Joshi

            Comment

            • sparky159357
              New Member
              • Jun 2010
              • 5

              #7
              Originally posted by dheerajjoshim
              Code:
              System.out.println("Your new current value of used money is $" + Bnum);
              It Should print the value in ith index.

              So change it to

              Code:
              System.out.println("Your new current value of used money is $" + Bnum[index]);
              Regards
              Dheeraj Joshi
              i tried that and it gave me an error the error is
              Exception in thread "main" java.lang.Error : Unresolved compilation problem:
              index cannot be resolved

              at Money.main(Mone y.java:42)




              thanks for the help i am so frustrated i have been dreaming about ways to fix the program

              Comment

              • Dheeraj Joshi
                Recognized Expert Top Contributor
                • Jul 2009
                • 1129

                #8
                Code:
                System.out.println("Your new current value of used money is $" + Bnum[index]);
                Should not be this inside the while loop?

                Regards
                Dheeraj Joshi

                Comment

                • sparky159357
                  New Member
                  • Jun 2010
                  • 5

                  #9
                  Originally posted by dheerajjoshim
                  Code:
                  System.out.println("Your new current value of used money is $" + Bnum[index]);
                  Should not be this inside the while loop?

                  Regards
                  Dheeraj Joshi
                  it is not my entire script is
                  Code:
                  import java.util.Scanner;
                  
                  public class Money {
                  	public static void main(String[] args){
                  		
                  		Scanner Scan = new Scanner(System.in);
                  		double Income, Tax, TaxTotal, Bills, OfficeSpace, LeftOver, Bill;
                  		int NBill, i, a, b;
                  		
                  		Bills = 0.;
                  		i = 1;
                  		
                  		System.out.println("Imput this month's income:");
                  		
                  		Income = Scan.nextDouble();
                  		
                  		System.out.println("Your current balence is $" + Income );
                  		System.out.println("How much of your income will you save for tax? (percentage, no percentage sign, whole num.)");
                  		
                  		Tax = Scan.nextDouble();
                  		
                  		System.out.println("$" + Income * (Tax / 100) + " goes to the Unchel Sam.");
                  		
                  		TaxTotal = Income * (Tax / 100);
                  		Income = Income - TaxTotal;
                  		
                  		System.out.println("Your new current balence is $" + Income );
                  		System.out.println("How many bills do you have?");
                  		
                  		NBill = Scan.nextInt();
                  		
                  		double[] Bnum = new double[NBill];
                  		Bnum[0] = 0;
                  		
                  		do{
                  			System.out.println("how much is value of bill " + i);
                  			Bnum[i] = Scan.nextDouble();
                  			i++;
                  			
                  		}while(i < NBill);
                  		
                  		System.out.println("Your new current value of used money is $" + Bnum[index]);
                  		
                  
                  		
                  	}
                  }

                  and it still gives me an error

                  thanks for the help

                  Comment

                  • Dheeraj Joshi
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1129

                    #10
                    index is undefined in your program. Move your print statement inside the while loop. In my last post i gave you the example. Not exact code.

                    Code:
                    System.out.println("Your new current value of used money is $" + Bnum[i]);
                    Note: You need to print it before incrementing i.

                    Regards
                    Dheeraj Joshi

                    Comment

                    Working...