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));
}
}
The last requirement is not verified
Under discussion
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Cristian
17 November 2020, 05:53
Thank you, sourav!
0
sourav bamotra
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 :
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
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
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
17 November 2020, 08:04
Okay, Liu! It's clear! Thanks a lot!
0