So i figured out that theres something going on with this task. My solution works in the range of -128 to 127. This probably means that there´s trouble on the binary interpretation. So i tried the list in Long witch is able to store longer numbers (tho 127 in not anywhere close to the max 'int' can store) but there´s still the same Problem.
I know other approaches i can try, but i wonder why this happens, is it because of the 'list functions' ? Please tell me if you know why.
(Also, please don´t tell me to use BufferedReader, that was my first approach and seems to have no impact.)
package com.codegym.task.task08.task0812;
import java.io.*;
import java.util.ArrayList;
/*
Longest sequence
1. Create a list of numbers.
2. Use the keyboard to add 10 numbers to the list.
3. Display the length of the longest sequence of repeating numbers in the list.
Example for the list 2, 4, 4, 4, 8, 8, 4, 12, 12, 14:
3
The value is 3, because the longest sequence of repeating numbers is three fours.
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
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int len = 10;
ArrayList<Integer> list = new ArrayList<>();
int a = 1;
int b = 1;
list.add(Integer.parseInt(bf.readLine()));
for (int i = 1; i < len ; i++)
{
list.add(Integer.parseInt(bf.readLine()));
if(list.get(i) == list.get(i-1))
{
a++;
}
else if ( a > b)
{
b = a;
a = 1;
}
else
a = 1;
}
if ( a > b) {
b = a;
a = 1;
}
System.out.println(b);
}
}