Reversing an Array

exception in thread "main" error

Page 1 of 1

5 Replies - 727 Views - Last Post: 05 April 2010 - 08:31 AM Rate Topic: -----

#1 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Reversing an Array

Posted 04 April 2010 - 09:58 PM

My assignment is to write a program that asks the user for an integer number and then asks to input that many numbers to create an array of that size. Then, I am am supposed to reverse the order of the array (not just the value but the memory of the value) and print the revered array. I am getting the following error when I execute my program when it reaches the reversed part. Does anyone know this means and how I can correct it?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at Reverse.main(Reverse.java:50)

----jGRASP wedge2: exit code for process is 1.

My code is as follows:

import java.util.Scanner;

public class Reverse
{
  public static void main(String[] args)
  {
    int sizeOfArray = 0;
	 int temp = 0;
	 
	 Scanner scan = new Scanner (System.in);
	 
	 System.out.print("Please enter an integer number: ");
	 sizeOfArray = scan.nextInt();
	 System.out.println("Please enter " + sizeOfArray + " integer numbers:");
	 
	 int [] array = new int[sizeOfArray];
	 
	 for(int i=0; i<array.length; i++)
	 //Creating the array
	 {
	   System.out.print("Enter number " + i + ": ");
		array[i] = scan.nextInt();
	 }
	 System.out.println("\n Position    Number");
	 System.out.println("---------------------");
	 
    //Printing the array  
	 for (int i=0; i<array.length; i++)
	 {
		System.out.println("     " + i + "         " + array[i]);
	 }
    
	 int m = array.length;
	 //Reversing the array
	 for(int i=0; i<m/2; i++) 
	 {
	   temp = array[i];
		array[i] = array[m];
		array[m] = temp;
		m--;
	 }
	 
	 //Printing the reversed array  
	 for (int i=0; i<array.length; i++)
	 {
		System.out.println("     " + i + "         " + array[i]);
	 }
	 
  }
}  


Is This A Good Question/Topic? 0
  • +

Replies To: Reversing an Array

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Reversing an Array

Posted 04 April 2010 - 10:11 PM

Change the initial value of m to be the length of the array - 1:
int m = array.length-1;


This way, on your first iteration, array[m] will be inside the bounds of the array.

Also, your method will not work on an input size of 2.
Was This Post Helpful? 0
  • +
  • -

#3 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Re: Reversing an Array

Posted 04 April 2010 - 10:46 PM

That does fix the error, however, now the last value in the array is not changing position because the size of the array - 1 does not catch the last value in the loop. I am trying to work through this problem another way, but each time I change something, it either does not reverse the order or it still leaves the last value in the same position. I have not addressed the problem of the array of size 2 yet. I am just trying to get it to reverse first. Not sure how to get the last number to change position now. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Reversing an Array

Posted 04 April 2010 - 10:53 PM

Just changing this: for(int i=0; i<m/2; i++) to this: for(int i=0; i<m; i++) seemed to work for me. This also allows it to reverse an array of size 2 as well.
Was This Post Helpful? 1
  • +
  • -

#5 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Re: Reversing an Array

Posted 05 April 2010 - 08:12 AM

Thanks so much. You are a life saver! :) We were given the hint to use the array length divided by 2, so I would not have even thought to change that part.
Was This Post Helpful? 0
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Reversing an Array

Posted 05 April 2010 - 08:31 AM

Glad your problem was solved :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1