I've made a program for a task: "Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2. Beware: r must be without duplicates."
And everything is working fine, but for some reason when I run my program I receive an error message from the method "array.second_array_size();" although I didn't even put any input. Both first and second methods for size are made to force the user to input a proper integer array size before he could do anything else via recursion. Here is the code of the main class responsible for all array methods in my program.
`
package Task1;
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Arrays;
public class Array_Shenanigans
{
int size;
Scanner in = new Scanner(System.in);
public String[] a1, a2, r;
public int first_array_size()
{
try
{
String line = in.nextLine();
size = Integer.parseInt(line);
}
catch (NumberFormatException e) {
System.out.println("Please, input a proper size of the first array");
first_array_size();
}
return size;
}
public void first_array_input()
{
a1 = new String[size];
for (int i = 0; i != size; i++)
{
System.out.println("Input the array element number " + (i+1));
a1[i] = in.next();
}
}
public int second_array_size()
{
try
{
String line = in.nextLine();
size = Integer.parseInt(line);
}
catch (NumberFormatException e) {
System.out.println("Please, input a proper size of the second array");
second_array_size();
}
return size;
}
public void second_array_input()
{
a2 = new String[size];
for (int i = 0; i != size; i++)
{
System.out.println("Input the array element number " + (i+1));
a2[i] = in.next();
}
}
public void uber_array_creation()
{
ArrayList r1 = new ArrayList();
for (int i = 0; i != a1.length; i++) {
for (int j = 0; j != a2.length; j++) {
if (a2[j].contains(a1[i])) {
r1.add(a1[i]);
}
}
}
Set<String> set = new HashSet<>(r1);
r1.clear();
r1.addAll(set);
r = (String[]) r1.toArray(new String[r1.size()]);
}
public void uber_array_sort()
{
Arrays.sort(r);
}
public void uber_array_output()
{
for (int i = 0; i < r.length; i++)
{
System.out.print(r[i] + " ");
}
}
}
`
I managed to hide the bug by not making a "println" message for the second size method, so a warning message from the caught exception is working like regular method message, but it obviously feels that something is wrong, although, still working.
Here is my main function, as Paul asked, which was a good idea.
`
import Task1.Array_Shenanigans;
public class Test2 {
public static void main(String[] args) {
Array_Shenanigans array = new Array_Shenanigans();
System.out.println("Please, input a proper size of the first array");
array.first_array_size();
array.first_array_input();
array.second_array_size();
array.second_array_input();
array.uber_array_creation();
array.uber_array_sort();
array.uber_array_output();
}
}
` As you can see, there is no
System.out.println("Please, input a proper size of the second array");
before the
array.second_array_size();
because the bug is doing that instead of me and I would get two prints in the console instead of 1.