I have a question like this:
Say that a "clump" in an array is a series of 2 or more adjacent elements of the same value. Return the number of clumps in the given array.
e.g
Here is my code:countClumps({1, 2, 2, 3, 4, 4}) ? 2
countClumps{(1, 1, 2, 1, 2}) ? 2
countClumps({1, 1, 1, 1, 1}) ? 1
import java.util.*; import java.io.*; import java.text.*; import java.lang.*; public class Program2 { int[] clumps = new int[100]; public static void main(String[] args) { int index; Program2 program = new Program2(); Scanner keyboard; keyboard = new Scanner(System.in); System.out.println("Please enter an array of numbers seperated by space:"); for(index = 0;index <= program.clumps.length;index++) program.clumps[index] = keyboard.nextInt(); System.out.print("countClumps({"); for(index = 0;index <= program.clumps.length;index++) System.out.print(program.clumps[index] + ", "); System.out.print("}) ? " + program.countClumps(program.clumps)); } public int countClumps(int[] nums) { int count = 0; for (int i = 0; i <= clumps.length; i++) { for (int j = 1; j <= clumps.length; j++) { if (clumps[i] == clumps[j]) count =+ 1; } } return count; } }
The program compiles fine, but after I enter any array and press enter key doesn't seems to have anything going... Hope someone can clarify what I did wrong here...