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
Hari
Level 8
Worcester
  • 04.09.2020
  • 290views
  • 1comment

what is wrong? please help!

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 jdk.internal.joptsimple.internal.Strings; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; /* The end 1. Declare a string list variable and immediately initialize it. 2. Read strings from the keyboard and add them to a list until the user enters "end". 3. Do not add "end" to the list. 4. Display the list, each value on a new line. 5. Use a for loop. */ public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //write your code here ArrayList<String> list = new ArrayList<String>(); Iterator<String> iter = list.iterator(); while(iter.hasNext()){ if(iter.next()=="end"){ break; } else { String s = reader.readLine(); list.add(s); } } for (String l: list) { System.out.println(l); } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Nouser
Level 36 , Germany
4 September 2020, 17:02useful
At program start the list is empty. That's why iter.hasNext() is false. Filling the list should look similar to (may contain typos):
String input = "";
while(!((input = reader.readLine()).equals("end"))) {
    list.add(input);
}
edit: or somewhat easier
while (true) {
    String input = reader.readLine();
    if (input.equals("end"))
        break;
    list.add(input);
}
+3
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.