java - Method taking in 2 arguments (array of ints, and a single int) -
i've created method, that, given array of numbers , integer n, calculates longest occurrence of integer n. example 1 0 0 0 1 1 1 1
, given number 1, longest sequence 4. i'm using 0's , 1's @ moment keep simple, i'm stuck on implementing main method test function - i'm not sure how separate integer "n" , array of ints when reading command line, , i'm hoping given pointers/help. here's code:
public class oneb { public static int longestseq(int[] nums, int n) { int max = 0; int curlength = 0; (int = 0; < nums.length; i++) { if (i == n) { curlength++; if (curlength > max) max = curlength; } else curlength = 0; } return max; } public static void main(string[] args) { int[] nums = new int[args.length-2]; int n = integer.parseint(args[args.length-1]); (int = 0; args.length-1 > i; i++) { nums[i] = integer.parseint(args[i]); } int result = longestseq(nums, n); system.out.println(result); } }
what i'm aiming last number in command line used integer n, whilst before used array nums.
with input 1 1 0 0 0 1 1 1 1 1
(the last 1 being "n" value - 4 being expected output") error:
exception in thread "main" java.lang.arrayindexoutofboundsexception: 8 @ oneb.main(oneb.java:26)
check javadoc arrayoutofbounds. put break point in code (you have information needed figure out line) , step through code slowly, watching values of all variables.
Comments
Post a Comment