Answer the question
In order to leave comments, you need to log in
Why doesn't the value of the returned array from the method change to the main method?
Good afternoon. I ran into a problem - the method does not return an array, the values remain the same as during initialization.
Task: Find the shortest and longest number. Print the found numbers and their length.
The Create method creates an array with the length of the base array elements, the findMin and findMax methods look for the minimum and maximum, respectively. But due to the fact that it is not possible to return the lgth array, the search for the minimum and maximum does not work. This is also confirmed by IDEA, with the message: "Return value of the method is never used".
Tell me how to solve this problem? Code below:
Code:
public class FindBylength {
public static void main(String[] args) {
String [] base = args;
int [] lgth = new int[base.length];
create(base);
findMin(lgth);
findMax(lgth);
}
public static int [] create(String [] base) {
int [] lgth = new int[base.length];
for (int i = 0; i <= (base.length - 1); i++) {
lgth[i] = base[i].length();
System.out.println(lgth[i]);
}
return lgth;
}
static void findMin (int [] lgth) {
int min = - 1;
for (int i = 0; i <= (lgth.length - 1); i++) {
min = i;
if (lgth[i] < min) {
min = lgth[i];
}
}
System.out.println(min);
}
static void findMax (int [] lgth) {
int max = - 1;
for (int i = 0; i <= (lgth.length - 1); i++) {
max = i;
if (lgth[i] > max) {
max = lgth[i];
}
}
System.out.println(max);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question