CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
null
Level 26
Orlando
  • 16.01.2020
  • 741views
  • 3comments

I have out of bound issue. still trying to figure out how to fix. please help

Question about the task Longest sequence
Java Syntax,  Level 8,  Lesson 6
Resolved


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:
  • The program must display a number on the screen.
  • The program should read values from the keyboard.
  • In the main method, declare an ArrayList variable with Integer elements and immediately initialize it.
  • The program should add 10 numbers to the collection in accordance with the conditions.
  • The program should display the length of the longest sequence of repeating numbers in the list.
package com.codegym.task.task08.task0812; import java.io.*; import java.util.ArrayList; import java.lang.*; /* Longest sequence */ public class Solution { public static void main(String[] args) throws IOException { //write your code here int i=0, j, max=0; ArrayList<Integer> list= new ArrayList<Integer>() ; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); for(int a=0;a<10;a++){ String sNumber = reader.readLine(); int number = Integer.parseInt(sNumber); list.add(number); } int count=1; while(i<list.size()){ j= i+1; if(list.get(i) == list.get(j)){ count = 1; while(list.get(i) == list.get(j)){ j++; count++; } } if(max < count){ max = count; } i = j; if(i==9) break; } System.out.println(max); } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Mikayla Pace Frontend Developer at Malouf Home
16 January 2020, 19:28
Because j = i+1, you will get an out of bounds error on the last iteration when you try to call the get() method on list. You have set i to stop incrementing when you reach the size of the list, but j tries to call the list size + 1 during the last pass.
while(i<list.size()){

            j= i+1;

            if(list.get(i) == list.get(j)){
                count = 1;
                while(list.get(i) == list.get(j)){
                    j++;
                    count++;
                }
            }
Try using the length of list-1, and remove the variable j all together.
for(int i = 0; i < listOfNumbers.size()-1; i++){
            if(listOfNumbers.get(i).equals(listOfNumbers.get(i+1))) {
                tempCounter++;
Also use .equals instead of == for comparing ArrayList values.
+1
null
Level 26 , Orlando, United States
22 January 2020, 20:23
Awesome way of solution. I should have thought of getting of j and using i variable only. Although i knew what caused the error, I still wasn't able to fix it until seeing your proposed solution. really APPRECIATE your time.
0
Mikayla Pace Frontend Developer at Malouf Home
22 January 2020, 21:07
Glad you were able to get it all figured out!
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.