Reverse an Array

Reverse Array with output in JOptionPane

Page 1 of 1

10 Replies - 1620 Views - Last Post: 24 June 2009 - 07:48 PM Rate Topic: -----

#1 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Reverse an Array

Post icon  Posted 22 June 2009 - 08:35 PM

Hello Java gods. I am have a couple of compile errors reversing an array. I have tried modifying the code several times using different methods and just can't get the right result. Any help would be great.

ERRORS:

reversal.java:38: cannot find symbol
symbol : variable modifyArray
location: class reversal
int k = modifyArray.length;
^
reversal.java:40: cannot find symbol
symbol : variable modifyArray
location: class reversal
reverse[counter] = modifyArray[--k];
^
2 errors

   import javax.swing.JOptionPane;
   import java.util.ArrayList;
   import java.lang.Math;
	public class reversal
   /* Main Method */
   {
	   public static void main (String args[])
	  {
	  //Declarations
		 double[] inputArray = new double[10];
	  //Receive amount entered from keyboard, load the array
		 for (int i = 0; i < inputArray.length; i++)
		 {
			String numString = JOptionPane.showInputDialog(null, "Enter a number: ");
			inputArray[i] = Double.parseDouble(numString);
		 }
		 String message = "Original Array is: ";//Create Msg
		 for (int i = 0; i < inputArray.length -1; i++)
		 {
			message += inputArray[i] + ", ";
		 }
		 modifyArray(inputArray);
		 message += "\nThe Reversed Array is: ";//Create Msg2
		 for (int i = 0; i < inputArray.length -1; i++)
		 {
			message += inputArray[i] + ", ";
		 }	
		 JOptionPane.showMessageDialog(null, message);	
	  }
	   public static double[] modifyArray(double[] newArray)
	  {
		 double[] reverse = new double[newArray.length];
		 int k = modifyArray.length;
		 for (int counter = 0; counter < newArray.length; counter++)
			reverse[counter] = modifyArray[--k];
		 return reverse;
	  }
   }


This post has been edited by MntMan: 22 June 2009 - 09:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reverse an Array

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reverse an Array

Posted 22 June 2009 - 08:45 PM

I cannot see how you can "reverse an array" using JOptionPane
JOptionPane is a GUI stuff displaying something
reversing an array is done throiugh code
Was This Post Helpful? 0
  • +
  • -

#3 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Reverse an Array

Posted 22 June 2009 - 08:50 PM

Unfortunately the coursework requires input and output in JOptioPane. I started with int and had it output the origional array, but then when I started working on the reversal with output...well not so much luck there.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reverse an Array

Posted 22 June 2009 - 08:57 PM

What I am saying is that your topic title is badly named
Kind of difficult to understand what you are trying to achieve
Was This Post Helpful? 0
  • +
  • -

#5 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Reverse an Array

Posted 22 June 2009 - 09:09 PM

I see, I changed the title.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reverse an Array

Posted 23 June 2009 - 04:23 PM

int k = modifyArray.length;	<---- this is newArray  (badly named but it is the name of your parameter)

for (int counter = 0; counter < newArray.length; counter++)
			reverse[counter] = modifyArray[--k];   <----- this is newArray  (badly named but it is the name of your parameter)


Was This Post Helpful? 0
  • +
  • -

#7 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Reverse an Array

Posted 23 June 2009 - 07:54 PM

Thanks pbl, I realized that last night and changed it. It isn't printing my array in reverse though.
I think I am using the wrong string When I output. What do you think?

   import javax.swing.JOptionPane;
   import java.util.ArrayList;
   import java.lang.Math;
	public class reversal
   /* Main Method */
   {
	   public static void main (String args[])
	  {
	  //Declarations
		 double[] inputArray = new double[6];
	  //Receive amount entered from keyboard, load the array
		 for (int i = 0; i < inputArray.length; i++)
		 {
			String numString = JOptionPane.showInputDialog(null, "Enter a number: ");
			inputArray[i] = Double.parseDouble(numString);
		 }
		 String message = "Original Array is: ";//Create Msg
		 for (int i = 0; i < inputArray.length; i++)
		 {
			message += inputArray[i] + ", ";
		 }
		 modifyArray(inputArray);
		 message += "\nThe Reversed Array is: ";//Create Msg2
		 for (int i = 0; i < inputArray.length; i++)
		 {
			message += inputArray[i] + ", ";
		 }
		 double minNum = inputArray[0];
		 for (int i = 1; i < inputArray.length; i++)
		 {
			if (minNum > inputArray[i])
			   minNum = inputArray[i];
		 }
		 message += ("\nThe minimum entry is: " + minNum);
		 JOptionPane.showMessageDialog(null, message);	
	  }
	   public static double[] modifyArray(double[] newArray)
	  {
		 double[] reverse = new double[newArray.length];
		 int k = newArray.length - 1;
		 for (int counter = 0; counter < newArray.length; counter++)
		 {
			reverse[counter] = newArray[k];
			k--;
		 }  
		 return reverse;


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reverse an Array

Posted 23 June 2009 - 08:33 PM

View PostMntMan, on 23 Jun, 2009 - 06:54 PM, said:

Thanks pbl, I realized that last night and changed it. It isn't printing my array in reverse though.
I think I am using the wrong string When I output. What do you think?

   import javax.swing.JOptionPane;
   import java.util.ArrayList;
   import java.lang.Math;
	public class reversal
   /* Main Method */
   {
	   public static void main (String args[])
	  {
	  //Declarations
		 double[] inputArray = new double[6];
	  //Receive amount entered from keyboard, load the array
		 for (int i = 0; i < inputArray.length; i++)
		 {
			String numString = JOptionPane.showInputDialog(null, "Enter a number: ");
			inputArray[i] = Double.parseDouble(numString);
		 }
		 String message = "Original Array is: ";//Create Msg
		 for (int i = 0; i < inputArray.length; i++)
		 {
			message += inputArray[i] + ", ";
		 }
		 modifyArray(inputArray);
		 message += "\nThe Reversed Array is: ";//Create Msg2
		 for (int i = 0; i < inputArray.length; i++)
		 {
			message += inputArray[i] + ", ";
		 }
		 double minNum = inputArray[0];
		 for (int i = 1; i < inputArray.length; i++)
		 {
			if (minNum > inputArray[i])
			   minNum = inputArray[i];
		 }
		 message += ("\nThe minimum entry is: " + minNum);
		 JOptionPane.showMessageDialog(null, message);	
	  }
	   public static double[] modifyArray(double[] newArray)
	  {
		 double[] reverse = new double[newArray.length];
		 int k = newArray.length - 1;
		 for (int counter = 0; counter < newArray.length; counter++)
		 {
			reverse[counter] = newArray[k];
			k--;
		 }  
		 return reverse;


your modifyArray() method returns an array of double but you don't use it

double[] reverse = modifyArray(inputArray);
or even
inputArray = modifyArray(inputArray);

yopur method name is cheating... it does nt modify an array at all it just creates a new reversed array that it returns
Was This Post Helpful? 0
  • +
  • -

#9 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Reverse an Array

Posted 23 June 2009 - 09:03 PM

I guess I'm not getting it, I tried implementing what you said and it caused errors. I tried several variations. Here is the one that compiled, but gave a run error of:

Exception in thread "main" java.lang.StackOverflowError

	  }
	   public static double[] modifyArray(double[] inputArray)
	  {
		 double[] reverse = modifyArray(inputArray);
		 int k = inputArray.length - 1;
		 for (int counter = 0; counter < inputArray.length; counter++)
		 {
			reverse[counter] = inputArray[k];
			k--;
		 }  
		 return reverse;
	  }


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reverse an Array

Posted 24 June 2009 - 07:32 AM

View PostMntMan, on 23 Jun, 2009 - 08:03 PM, said:

I guess I'm not getting it, I tried implementing what you said and it caused errors. I tried several variations. Here is the one that compiled, but gave a run error of:

Exception in thread "main" java.lang.StackOverflowError

	  }
	   public static double[] modifyArray(double[] inputArray)
	  {
		 double[] reverse = modifyArray(inputArray);
		 int k = inputArray.length - 1;
		 for (int counter = 0; counter < inputArray.length; counter++)
		 {
			reverse[counter] = inputArray[k];
			k--;
		 }  
		 return reverse;
	  }


You had it correct the first time
double[] reverse = new double[inpytArray.length];

it is in your main method that you were wrong calling modifyArray() without saving the reverse[] array

modifyArray(iinputArray); instead of
double[] reverse = modifyArray(inputArray);

the change you made will just put you in an infinite recursion calling modifyArray() within modifyArray()
double[] reverse = modifyArray(inputArray);
Was This Post Helpful? 1
  • +
  • -

#11 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Reverse an Array

Posted 24 June 2009 - 07:48 PM

I feel like such an idiot sometimes!
Thanks for the help pbl...Your the man!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1