public class MinDif {
		public static void main(String[] args) {
			
			 EasyReader keyboard = new EasyReader();
			 EasyWriter screen = new EasyWriter();
			 
			 //Create an array to the length of the specified entry
			 int numItems=keyboard.readInt("Enter the length of the array: "); 
		         String[] myArray = new String[numItems];
			 
		         // Read in the contents of the array 
		         for (int i=0; i<numItems; i++) 
			 myArray[i]=keyboard.readString("Enter number "+(i+1)+" (index"+i+") : ");
		 
		 	 // Set a vairable and test it
		 	 int dMin = 0;
		 	 int i,j;  
		 	 for(i=0; i< numItems; i++)
		 	   {
		 	     for(j=i+1; j< numItems;j++)
		 	     {   
		 	       dMin = Math.min(dMin,( myArray[i]-myArray[j] ));
		 	       }
		 	     }
		         System.out.println(dMin);
		 	 	 
		}
	}

I need to find the minimum difference between two adjacent elements within an array. This is the code i have so far. When compiling the program i get the error message:

bad operand types for binary operator '-'

and it points at the '-' used in (myArray-myArray[j])

Cheers

myArray is a string array
The error you are seeing bad operand types for binary operator '-' is telling you that you cannot subtract a string from a string which is what you currently are asking.

The place it points to is where you're asking the string to be subtracted from a string.

So if you want a number array you could try :

Integer[] myArray = new Integer[numItems];

Thank you that really did help, and has fixed that problem, although i have now encountered another problem.

public class MinDif {
		public static void main(String[] args) {
			
			 EasyReader keyboard = new EasyReader();
			 EasyWriter screen = new EasyWriter();
			 
			 //Create an array to the length of the specified entry
			 int numItems=keyboard.readInt("Enter the length of the array: "); 
		         Integer[] myArray = new Integer[numItems];
			 
		         // Read in the contents of the array 
		         for (int i=0; i<numItems; i++) 
			 myArray[i]=keyboard.readInt("Enter number "+(i+1)+" (index"+i+") : ");
		 
		 	 // Create a loop to find the minimum difference between two neighbouring components
		 	 int dMin = myArray[1]-myArray[0];
		 	 int i,j;  
		 	 for(i=0; i< numItems-1; i++)
		 	   {
		 	     for(j=i+1; j< numItems-1;j++)
		 	     {
		 	     	    if (j>=i)
		 	     	     dMin = Math.min(dMin,( myArray[j]-myArray[i] ));
		 	     	    else
		 	     	     dMin = Math.min(dMin,( myArray[i]-myArray[j]));		 	      
		 	     }
		 	  }
		 	     
		 	     int pdMin= Math.abs(dMin);
		 	     System.out.println("The smallest difference between two neighbouring components is: "+dMin);
		 	 	 
		}
	}

The problem i face is when it comes to compiling. I enter an array of numbers and everything works fine. Although if the numbers i enter are not in numerical order say (5 2 1) the minimum difference returned is -3? Not really sure why it does this, as if i enter the numbers in as (1 2 5) then in returns the minimum difference as 1, which is correct.

Ta

Sounds like you need to debug your code to see why it is not doing what you want.
If you don't have an interactive debugger, try using printlns to show the values of the variables that you are using and the execution flow of your code.

You appear to be comparing the element numbers i and j when I think you intended to compare the contents of those elements (myArray with myArray[j] in the condition:
if (j>=i).
j is always going to be greater than i because you have defined it as i+1 in the loop initialiser.

I agree with JeffGrigg if you wrap myArray - myArray[j] in Math.abs() which stands for take the absolute value of whatever is inside the parenthesis then you should get a positive value out.

The reason you are getting a negative when you do [5 2 1] is because 2-5 is -3 and less than 2-1 which is 1. So -3 is the minimum. You get 1 as the minimum when you do [1 2 5] because 2-1 is 1 and 5-2 is 3. The minimum there is 1.

Just browsing old threads... weird I know.... but I overlooked the lines

int pdMin= Math.abs(dMin);
System.out.println("The smallest difference between two neighbouring components is: "+dMin);

the issue was you printed dMin instead of pdMin.. Probably too late to help now...

Edited 4 Years Ago by woodenduck

This article has been dead for over six months. Start a new discussion instead.