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
Cristian
Level 13
Ploiesti
  • 17.11.2020
  • 349views
  • 5comments

The last requirement is not verified

Question about the task Lonely arrays interact
Java Syntax,  Level 7,  Lesson 4
Under discussion

1. Create an array of 10 strings.
2. Create an array of 10 numbers.
3. Enter 10 strings from the keyboard, and put them in an array of strings.
4. In each element of the number array, record the length of the string whose string array index coincides with the current index of the number array.

Display the contents of the number array, each value on a new line.

Requirements:
  • The program must create an array of 10 strings.
  • The program must create an array of 10 integers.
  • The program should read strings for the array from the keyboard.
  • The program must record the lengths of the strings in the number array, and then display them on the screen.
package com.codegym.task.task07.task0703; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; /* Lonely arrays interact */ public class Solution { public static void main(String[] args) throws Exception { //write your code here String[] array1 = new String[10]; int[] array2 = new int[10]; int nameLength = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < array1.length; i++) { array1[i] = reader.readLine(); nameLength = array1[i].length(); } for (int j = 0; j < array2.length; j++) { array2[j] = nameLength; } System.out.print(Arrays.toString(array2)); } }
0
Comments (5)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Cristian
Level 13 , Ploiesti, Romania
17 November 2020, 05:53
Thank you, sourav!
0
sourav bamotra
Level 20 , jammu, India
17 November 2020, 05:18
nameLength is int variable so its value keep on updating with completetion of each loop iteration (i.e the final value of nameLength will be the length of the last element in String array) SOLUTION :
for(int i =0; i<array1.length; i++){
array1[i] = reader.readLine();
array2[i] = array1[i].length();
}
here in this case you will be reading String element store it in array1 and for the same String you will find its length and put it in array2. remove or comment line 17,27,28,29
0
Cristian
Level 13 , Ploiesti, Romania
17 November 2020, 05:55
package com.codegym.task.task07.task0703; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; /* Lonely arrays interact */ public class Solution { public static void main(String[] args) throws Exception { //write your code here String[] array1 = new String[10]; int[] array2 = new int[10]; //int nameLength = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); for(int i =0; i < array1.length; i++){ array1[i] = reader.readLine(); array2[i] = array1[i].length(); } // for (int j = 0; j < array2.length; j++) { // array2[j] = nameLength; //} System.out.print(Arrays.toString(array2)); } } But the last requirement isn't verified. Why?
0
Guanting Liu
Level 16 , Springfield, United States
17 November 2020, 07:03
First,you already update the second array by the length of each string in the first array,now,everything is done instead of printing output. Second,u don't need to use namelength(array2[j] = namelength) because u can just array2[j] to express the contents,and now u get the contents the requirement needs to println every array2[j] in a new line,what u should do?That's so simple,use a for loop to println every contents in your array2 ,which should be System.out.println(array2[j]) by using a for loop to traverse.
+1
Cristian
Level 13 , Ploiesti, Romania
17 November 2020, 08:04
Okay, Liu! It's clear! Thanks a lot!
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.