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.