Results 1 to 5 of 5
  1. #1
    breakdance77 is offline Member
    Join Date
    Aug 2011
    Location
    Espoo, Finland
    Posts
    3
    Rep Power
    0

    Default Counting exponent with long variables and Arrays, still error .. help anyone?

    Hello!

    i'm new to this forum, hope this comes to the right place ..

    I'm still learning Java, trying to program a method what will calculate exponent from a basenumber.
    it seems to work, but still gives error message.

    here is the code:

    ---------------------------

    import java.util.Scanner;

    public class Count_exponent

    {



    public static void main(String[] args)
    {


    int i, a, b, baseNumber, toExpo;
    int number1, number2;

    Scanner input = new Scanner( System.in );
    System.out.println("Give the base Number: ");
    number1= input.nextInt(); // read first number from user
    System.out.println("Give the toExponent number: ");
    number2= input.nextInt(); // read second number from user



    baseNumber = number1;

    toExpo = number2;



    long[] longArray = new long[toExpo];


    a=0;
    b=1;




    for (i=0; i<toExpo; i++)
    {
    longArray[i]=(long)baseNumber;
    System.out.println(longArray[i]);
    }

    for (i=0; i<toExpo; i++)
    {
    longArray[a]=longArray[a]*longArray[b];
    System.out.println (longArray[0]); // this line prints on screen the exponent counted
    b=b+1; // at every step
    }

    System.out.println ( "Answer is: ");
    System.out.println (longArray[0]); // program seems to count exponents ok, but gives

    } // error before printing the final answer.

    }
    // Can anyone help me with
    // this program, what is it doing
    // wrong?
    ----------------------
    Last edited by breakdance77; 08-10-2011 at 06:57 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default

    but still gives error message.
    Please copy and paste here the full text of the error message.

    what is it doing
    // wrong?
    Please explain by showing the output and saying what is wrong with it and show what it should be.

  3. #3
    breakdance77 is offline Member
    Join Date
    Aug 2011
    Location
    Espoo, Finland
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please copy and paste here the full text of the error message.



    Please explain by showing the output and saying what is wrong with it and show what it should be.
    Give the base Number: 2
    Give the toExponent number: 8

    Step 0 on exponent calculation: 4
    Step 1 on exponent calculation: 8
    Step 2 on exponent calculation: 16
    Step 3 on exponent calculation: 32
    Step 4 on exponent calculation: 64
    Step 5 on exponent calculation: 128
    Step 6 on exponent calculation: 256

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at Count_exponent.main(Count_exponent.java:52)

    ----
    Up there is the output, the program asks for parameters, base number ( the number to be raised to exponent x), and the exponent. There i used number 2 as base number, and 8 as the exponent. seems to calculate it fine. But gives an error still.
    Anyone can help me with this problem?

    Thanx,
    Breakdance


    -----------
    here is the code again.
    -----------
    import java.util.Scanner;

    public class Count_exponent

    {



    public static void main(String[] args)
    {


    int i, a, b, baseNumber, toExpo;
    int number1, number2;

    Scanner input = new Scanner( System.in );
    System.out.println("Give the base Number: ");
    number1= input.nextInt(); // read first number from user
    System.out.println("Give the toExponent number: ");
    number2= input.nextInt(); // read second number from user



    baseNumber = number1;

    toExpo = number2;



    long[] longArray = new long[toExpo];

    /** Array to be initialized, for example if
    * the basenumber is 2, and the exponent is 3,
    * then the array variable longArray[0] will be 2 ..
    * .. longArray[1] will be 2, .. and longArray[2] will be 2.
    *
    * So to say, toExpo -variable is the exponent, and it
    * rules the number of fields in type long[] variable
    * longArray. */
    a=0;
    b=1;




    for (i=0; i<toExpo; i++)
    {
    longArray[i]=(long)baseNumber; /** .. and here the initialization goes on ..
    System.out.println(longArray[i]); * same base number goes to all of the longArray variables
    * fields. */
    }
    for (i=0; i<toExpo; i++)
    {
    longArray[0]=longArray[0]*longArray[b];

    /** And here happens the actual calculation ..
    * for example, if the base number is 2, and is to be
    * raised to exponent 3, the first field of array is
    * calculated like field#0 x field#1, then for-loop
    * loops again after raising help variable b +1,
    * after that new value of field#0 is calculated
    * field#0 x field#2, and field#0 gets a new value
    * again .. and so on.
    * Until field#0, that is longArray[0], should
    * contain the correct answer, with the basenumber
    * raised to its given exponent.
    * To make it even more simple, 2 x 2 x 2 = 8. */

    b=b+1;

    System.out.print("Step " + i + " on exponent calculation: ");
    System.out.println (longArray[0]);
    }

    System.out.println ( "Answer is: ");
    System.out.println (longArray[0]); // program seems to count exponents ok, but gives out
    } // error before printing the final answer.
    }
    Last edited by breakdance77; 08-12-2011 at 05:37 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default

    rrayIndexOutOfBoundsException: 8
    at Count_exponent.main(Count_exponent.java:52)
    Look at line 52 in the source. There is an index to an array that is past the end of the array. Check your logic to see why the index gets to 8 if the array does not have 9 elements. Remember first element is 0.

    Please edit your code and wrap it in code tags. Use the #icon above the input box.

  5. #5
    breakdance77 is offline Member
    Join Date
    Aug 2011
    Location
    Espoo, Finland
    Posts
    3
    Rep Power
    0

    Default

    thanx, that was more simple then i thougt, the for loop was wrong, for (i=0; i<toExpo-1; i++) would have been correct.
    Works now!


Similar Threads

  1. Using Arrays as Variables.
    By wired-in=p in forum New To Java
    Replies: 4
    Last Post: 07-24-2011, 03:32 PM
  2. Code counting error
    By Adde1986 in forum New To Java
    Replies: 2
    Last Post: 03-18-2009, 01:02 AM
  3. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 07:33 PM
  4. Help, find exponent in java
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:31 PM
  5. Error: convert from String to long
    By bbq in forum New To Java
    Replies: 1
    Last Post: 06-29-2007, 07:23 PM

Posting Permissions

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