How would I go about inputting the negative values in the array in case 1 the array comes from the user, case 2 from the text file?
The sum prints perfectly fine with positive values but if I input negative values it just completely ignores them. Thanks
case 2: BufferedReader reader = null; Scanner sc = null; String line = ""; try { reader = new BufferedReader(new FileReader("integers.txt")); sc =new Scanner(new BufferedReader(new FileReader("integers.txt"))); while (sc.hasNext()) { line = sc.next(); } String s[] = line.split(","); a = new int[s.length]; for (int i = 0; i < s.length; i++) { a[i] = Integer.parseInt(s[i]); } } finally { if (reader != null) { reader.close(); } } System.out.println(line); sum=maxSum(a)
public static int maxSum(int [] a) { int maxSum = 0; int this_sum=0; int i=0; for(int j=0; j< a.length; j++) { this_sum += a[j]; if(this_sum > maxSum) { maxSum = this_sum; } else if( this_sum<0) { i=j+1; this_sum=0; } } return maxSum; }