tags:

views:

132

answers:

2

i'm trying to make a program that reads two English words from the command line, then outputs all possible ways the words can cross eachother. and print an error message if they do not cross. i want to use the charAt and length methods.. not sure where to even start..

here's what i got so far:

public class Cross2
{
    public static void main(String[] args)
    {

    // create two dimentional array that stores input
    private String[][] cross = new String[w1pos][w2pos];

    // get input from user
    Scanner input = new Scanner(System.in);
    System.out.println("Enter two words: ");
    String word1 = input.next();
    String word2 = input.next();

    // loop through length of words
    for(int w1pos = 0; w1pos < word1.length(); w1pos++) {
        for(int w2pos = 0; w2pos < word2.length(); w2pos++) {

        // if characters are equal, print the array
        if (word1.charAt(w1pos) == word2.charAt(w2pos))
             System.out.println(cross[w1pos][w2pos]);
        }
    }
+2  A: 

i think so. i would need to loop through the length of the string, then use charAt(i) to print each character.

That's a good start.

and yes crossing as in letter matching.. so i need to compare each character using charAt for each position in each of the two words

That's good. Hint: So how many loops?

and print an error message if they do not cross

Hint: ... and how will you do that?

Don't answer my questions. They are hints. Write some code based one them.

Stephen C
two loops. for(int position=0; position < word1.length(); position++) so that loop runs through the word1 String.. so that local variable now contains the number of characters.. so i would need to compare that position each time with a statement like: if(word1.charAt(w1pos) == (word2.charAt(w2pos)).. i think that makes a little bit of sense
Bill S
well, now i'm not sure how to get my array to print once i loop through it and compare the characters.. getting an out of bounds error
Bill S
+1  A: 

So for the words "abra" and "cadabra", would the output look like this?

c   
abra
d   
a   
b   
r   
a   


c   
a   
d   
abra
b   
r   
a   


c   
a   
d   
a   
b   
r   
abra


 c  
 a  
 d  
 a  
abra
 r  
 a  


  c 
  a 
  d 
  a 
  b 
abra
  a 


   c
abra
   d
   a
   b
   r
   a


   c
   a
   d
abra
   b
   r
   a


   c
   a
   d
   a
   b
   r
abra

If so, I would recommend that you use a two-dimensional array of characters filled with spaces that you write the words into before displaying. Printing each character one at a time might be significantly more difficult.

[edit:]

I don't know Java so I couldn't use functions and I couldn't write much more than what you had, but I hope this helps.

// This is a comment explaining what the code does.

.

/* This is a comment explaining what you need to add to make the code work. */

.

public class Cross2
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter two words: ");
        String word1 = input.next();
        String word2 = input.next();
        String[][] cross = new String[word1.length()][word2.length()];
        /* Fill 'cross' with spaces */
        for(int w1pos = 0; w1pos < word1.length(); w1pos++) {
            for(int w2pos = 0; w2pos < word2.length(); w2pos++) {
                if (word1.charAt(w1pos) == word2.charAt(w2pos)) {
                    // Store 'word1' horizontally into 'cross'.
                    for(int w1posAgain = 0; w1posAgain < word1.length(); w1posAgain++) {
                        /* Store 'word1.charAt(w1posAgain)' horizontally into 'cross'
                        at row 'w2pos' and column 'w1posAgain'. */
                    }
                    // Store 'word2' vertically into 'cross'.
                    for(int w2posAgain = 0; w2posAgain < word2.length(); w2posAgain++) {
                        /* Store 'word2.charAt(w2posAgain)' vertically into 'cross'
                        at row 'w2posAgain' and column 'w1pos'. */
                    }
                    for(int w1posAgain = 0; w1posAgain < word1.length(); w1posAgain++) {
                        for(int w2posAgain = 0; w2posAgain < word2.length(); w2posAgain++) {
                            System.out.print(cross[w1posAgain][w2posAgain]);
                        }
                        System.out.println();
                    }
                    /* Fill 'cross' with spaces.
                    Yes, really.*/
                }
            }
        }
    }
}
dln385
exactly. so store the user input as an array, then loop through one time for each array, comparing each position in word one to the other positions. then if they are equal.. ummm thats where i get goofed.
Bill S
So you've already checked that `word1.charAt(w1pos) == word2.charAt(w2pos)`. In my example above, `word1` is printed horizontally, so let's go with that. So the next step would be to store each character of `word1` into the two-dimensional array. Since `word1` needs to intersect `word2` at `w2pos`, you would want to store the characters of `word1` into the row `w2pos` of the two-dimensional array. Even if you can't get the whole solution now, please post some code so we can better help you.
dln385
really appreciate the help here guys. don't know what i would do without ya. i feel so alone in this world sometimes.. here's some of what i got: Sting word1; String word2; // create two dimentional array that stores input private String[][] cross = new String[ROWS][COLS]; // get input from user Scanner input = new Scanner(System.in); System.out.println("Enter two words: "); String word1 = input.next(); String word2 = intput.next();
Bill S
// determine length of each array with length() for(int w1pos = 0; w2pos < word1.length(); w1pos++) { for(int w2pos = 0; w2pos < word2.length(); w2pos++) { // if (word1.charAt(w1pos) == word2.charAt(w2pos)) // store characters of word1 into the row of w2pos of 2d array
Bill S
really wish i could display it a little better.. i feel like i'm on the right track.. make the array, run the loops, compare each position, if they are equal, store word1 into the row? so instead of ROW and COL i should have w1pos and w2pos? because they are the looping variables that hold the length of the string.. i think. the gears are turning, that's for sure.
Bill S
You can edit your question to add the code that you have written so far. After your code is typed in the box, be sure to select it and click the "format as code" button so it shows properly. As for what you posted so far, I noticed that your first `for` loop has a typo. It should have the condition that `w1pos < word1.length()`, not `w2pos < word1.length()`. After you verify that the characters are the same, you would write the first word horizontally into the row, then the second word vertically into the column. Then you would print the two-dimensional array.
dln385
ruh oh.. just as i'm making some progress on this guy, i get hit with another assignment.. now i have to make the first part of a tic-tac-toe game using a 2d array. hmmmmm
Bill S
@dln385 please come back and help me :-(
Bill S
@Bill S I have two reasons I can't help you further. First, [check out the homework tag](http://stackoverflow.com/tags/homework/info). Considering all the help we've given you and that you're expected to be able to complete your homework on your own, you really should be able to finish this. Perhaps you need to study your book again. The other reason I can't help you further is because I don't know Java. This is why I can only help you with the general design and where to start. Sorry.
dln385
i totally understand.. it was just a pathetic attempt for some help. well i know i'm not going to use an array. will just use charAt() and length() while i loop through each string. and my homework requires the two words to be passed as parameters.. i really appreciate it though man. programming is probably the most difficult thing i've ever done. with so many people out there being awesome at java, it's only natural for me to seek help.. and sometimes take things a little too far in moments of frustration and despair.
Bill S
@dln385 i got a quick one for ya, how would i go about printing the proper indent for the vertical word? i know i want to indent based on the position of word2, which i determined by the charAt()? so maybe set an indent variable equal to the current position when they are equal.. but then how would i quantify the number of spaces?
Bill S
The original idea was that you would use a two-dimensional array filled with spaces so that you wouldn't have to worry about the indent for the vertical word. I'll post a structure from the code you put up. Hopefully it should help.
dln385