CodeGym
Promotion
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
MLA
Level 16
Philadelphia
  • 29.04.2020
  • 505views
  • 2comments

Code fails "Be sure that the length of the longest sequence is calculated correctly when it is located at the end of the list of entered numbers. " even though output succeeds.

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

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 { BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); ArrayList<Integer> lst = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) { lst.add(Integer.parseInt(kb.readLine())); } int longest = 0, current = 0; for (int i = 0; i < 10; i++) { try { if (lst.get(i).equals(lst.get(i + 1))) { current++; } } catch (Exception e) { if (longest < current) { longest = current; } } } if (longest == 0) System.out.println(1); else System.out.println(longest); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Show How
Level 13 , Manchester, United Kingdom
6 May 2020, 18:23
after the condition where you check if longest is less than current and assigning current to longest, you need to reset the current so the new sequence can be captured. Hope it helps.
0
Nouser
Level 36 , Germany
30 April 2020, 07:47
you save current into longest only once, when the loop is at its end If you have two sequences 11112222 current gets increased to 3 for all the ones. Then it's getting increased for additional 3 cause of the twos. So current is 6 and it's getting saved now into longest
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.