0

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.

Jermog
  • 31
  • 5
  • Why it's marked like a duplicate if "duplicate" question is related to issues with Scanner.nextInt while my code only uses Scanner.nextLine and going through "Integer.parseInt()"? – Jermog Mar 11 '19 at 22:55
  • Can you add a main function so we can see how you are using your functions? Your code strikes me as a touch odd as you alter member variables and return values from the functions. You should really do one or the other. – Paul Rooney Mar 11 '19 at 23:12
  • Added the main function. If you are talking about unnecessary returns, I've fixed that issue, I've copypasted an old code that still had this problem, should be good now. – Jermog Mar 11 '19 at 23:24
  • The problem does stem from the use of the `.next` method. You should just always read a line at a time i.e. use the same method you use to read the array size to read the array entries. – Paul Rooney Mar 11 '19 at 23:25
  • I had them as the same method and separated them later to try to find what's wrong, as well as to have a variable for array size as an instance variable instead of it being local. I had exactly the same issue when size and input methods were merged and size was a local variable of this method. Just for some reason, the second method that goes through int check for the input gets his check exception triggered for no reason. – Jermog Mar 11 '19 at 23:31
  • What about replacing `a1[i] = in.next();` with `a1[i] = in.nextLine().trim();`? It reads the newline and then trim discards it. – Paul Rooney Mar 11 '19 at 23:34
  • Yes, it helped fix everything. Thanks for that method, I would use it If I would get into a sticky situation like that with a scanner. – Jermog Mar 11 '19 at 23:39

0 Answers0