CodeGym
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Marius Moldovan
Level 8
WGC
  • 23.12.2020
  • 265views
  • 3comments

The code is not going through verify

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; /* Longest sequence */ public class Solution { public static void main(String[] args) throws IOException { //write your code here BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<Integer> numbers = new ArrayList<Integer>(); ArrayList<Integer> count = new ArrayList<Integer>(); int temp = 0; int countN = 1; for(int i = 0; i < 10; i++) { int tempNo = Integer.parseInt(reader.readLine()); if (i == 0) temp = tempNo; else if(temp == tempNo) { countN++; } else { temp = tempNo; count.add(countN); countN = 1; } if (i == 9) count.add(countN); numbers.add(tempNo); } int max = 0; for(int i = 1; i < count.size(); i++) { if (max < count.get(i)) max = count.get(i); } System.out.println(max); } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Cristian
Level 13 , Ploiesti, Romania
23 December 2020, 18:31useful
Try this code: ArrayList<Integer> list = new ArrayList<Integer>(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) ); int number = 0; int max = 1; int current = 1; for (int i = 0; i < 10; i++) { number = Integer.parseInt(reader.readLine()); list.add(number); } reader.close(); for (int i = 0; i < 9; i++) { if( list.get(i).equals(list.get(i+1))) { current ++; if (max < current) { max = current; } } else { current = 1; } } if (max < current) { max = current; } System.out.println(max);
+1
Guadalupe Gagnon
Level 37 , Tampa, United States
23 December 2020, 17:58solution
The bug is on line 36. Consider if you entered something like: 1, 1, 1, 1, 1, 1, 1, 1, 2, 2
+2
Marius Moldovan
Level 8 , WGC, United Kingdom
4 January 2021, 10:54
Thank you Guadalupe! I have replaced int max = 0; with int max = count.get(0); and it works. That's what I wanted to do in first place but I forgot!
+1
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.