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
Alex Balandinos
Level 18
Lusaka
  • 07.03.2019
  • 1023views
  • 2comments

Program not displaying and not exiting

Question about the task The end
Java Syntax,  Level 7,  Lesson 12
Under discussion


Create a list of strings.
Enter strings from the keyboard and add them to the list.
Enter strings from the keyboard until the user enters "end". The string "end" is ignored.
Display the strings on the screen, each on a new line.

Requirements:
  • Declare a string list variable and immediately initialize it.
  • Read strings from the keyboard and add them to a list until the user enters "end".
  • Do not add "end" to the list.
  • Display the list, each value on a new line.
  • Use a for loop.
package com.codegym.task.task07.task0722; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; /* The end */ public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> arr= new ArrayList<String>(); //write your code here for(int i = 0; i<arr.size(); i++){ // String a= reader.readLine(); if( !(reader.readLine().equals("end"))){ arr.add(reader.readLine()); System.out.println(arr.get(i)); } else System.exit(0); } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Serg
Level 17 , Minsk, Belarus
13 May 2019, 15:47
use "while" to make an infinite loop instead "for" and another problem which i faced too is: "Every time the reader.readLine() method is called, it does its work. Therefore, it is better to first write the value of reader.readLine() into a temporary variable and then compare that variable with the conditions and put the value of that variable in the list." (c) Roman Ivanov
0
Khurram
Level 16 , Lahore, Pakistan
8 March 2019, 17:02
in the line for(int i = 0; i<arr.size(); i++) when the code runs this the first time, i = 0 and the array size is also 0 at this moment, note that the array is empty, the user has not entered anything yet. So the condition i < arr.size() becomes 0 < 0 which is false so the loop does not run at all and the program simply exits
+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.