HI,
Then I run 5 tests and everything is ok:
1- repeated number In the middle:
2- repeated number at the end:
3- repeated number at the beginning:
4- 10 Different numbers
5- The same number:
Why my code failed?
package com.codegym.task.task08.task0812;
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
Longest sequence
Requirements:
1. The program must display a number on the screen.
2. The program should read values from the keyboard.
3. In the main method, declare an ArrayList variable with Integer elements and immediately initialize it.
4. The program should add 10 numbers to the collection in accordance with the conditions.
5. The program should display the length of the longest sequence of repeating numbers in the list.
*/
public class Solution {
public static void main(String[] args) throws IOException {
//write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) );
String sNumber = "";
int number = 0;
int max = 1;
int current = 1;
for (int i = 0; i < 10; i++) {
sNumber = reader.readLine();
number = Integer.parseInt(sNumber);
list.add(number);
}
reader.close();
for (int i = 0; i < 9; i++) {
if( list.get(i) == list.get(i+1)) {
current ++;
} else {
if (max < current) {
max = current;
current = 1;
}
}
if (max < current) {
max = current;
}
//System.out.println(i + " - current: " + current + " maximo: " + max + " number: " + number);
}
System.out.println(max);
}
}