1

I am a beginner in Java and very new to StackOverflow.

I have to write a program that asks the user for Words then prints out all the words in a Single dimension Array and Then Print out all the Substrings of each word from that array in a 2-dimensional array (each word on a separate row.)

When the program is run it asks the user for the number of words. and then asks the user to enter all the words All the words must be equal length(**rectangular 2d array only)

This is my code its not working it doesnt print the 2d array pls help.

    //Ari
import javax.swing.JOptionPane;
public class twoDArrays
{
    public static void main (String[] args)
    {



        String totalWords = JOptionPane.showInputDialog(null,"How Many Words?");
        int val = Integer.parseInt(totalWords);

        String[] words = new String[val];

        for (int i =0; i<val; i++)
        {
            words[i]= JOptionPane.showInputDialog(null,"Enter Words");
            System.out.print(words[i] + " ");
        }
        System.out.println();

        int len = words[0].length();

        int subs = ((len)*(len+1))/2;

        String table [][] = new String[val][subs];

        for(int i=0; i<val; i++)
        {
            String [] temp = totalSubs(words[i]);
            for (int a = 0; a <subs; a++)
            {  
                table[i][a] = temp[a];
            }
        }
        for (int i= 0; i < table.length; i++)   
        {
            System.out.print(words[i] + " ");
            for (int a = 0; a< subs; a++)
            {
                System.out.print(table[i][a] + " ");
            }
            System.out.println();
        }
    }
    public static String[] totalSubs(String s1)
    {
        int length = s1.length();
        int num = ((length)*(length+1))/2;
        String[] sub = new String[num];
        int loop=0;
        int b =0;
        int pos = 0;
        while(loop < num)
        {
            int a=0;
            for(int i = pos; i < length; i++ )
            {
                if((a+1) < (length +b))
                {
                    sub[loop] = s1.substring(pos, (a + 1 + b));
                    loop++;
                    a++;
                }
            }
            pos++;
            b++;
        }
        return sub;
    }

}

This is how it should work

When u Run the Program it ask u for the number of words u want to enter. Then it asks to enter all the words. (all words to be same length). When u enter all the words it prints out all the words u entered in an 1-D array. ex: cat dog mom dad Then 2nd part of the program prints out all the Susbtring of each word in a 2 dimesional array (each word on a different row.) ex:

  • c ca cat a at t
  • d do dog o og g
  • m mo mom o om m
  • d da dad a ad d

Thanks for Help if anyone can help I am new to this.

  • The problem is in your `totalSubs()`, which loops forever. Try calling with `totalSubs("AB")` and step through the code with a debugger. You'll find that it adds `"A"`, then `"B"` to the result array, but never adds `"AB"`, so it loops forever, since result array is never filled. – Andreas Mar 09 '19 at 02:30
  • Bro most of the things u said i dont understand i am learning java from 1 month. i have no other experience. Also i dont know how to debug. Can u edit it inside my code? – Arif Rashid Mar 09 '19 at 04:31
  • also give line #'s pls i am new to this – Arif Rashid Mar 09 '19 at 04:43
  • I linked to instructions about debugging, told you symptom of what is wrong to help get you started. Fixing the code is your job as a programmer. I gave a small 2-letter example to help you, so you can go through the code on paper, if needed, until you find out why it doesn't come up with a 2-letter result, i.e. only `"A"` and `"B"`, but not `"AB"`. – Andreas Mar 09 '19 at 05:06

0 Answers0