I am creating a program that takes two english words from the command line and prints one out vertical and the other horizontal and sees how many times the words cross. If they don't cross it then prints out an error message.

Here is what I have so far for the program. I don't know what the next step should be.

public class Assg2
{
    public static void main(String[] args)
    {
        String w1 = args[0];
        String w2 = args[1];

        int numberOfCrosses =0;
        int pos1 = 0;
        int pos2 = 0;


        for(int i=0; i < w1.length(); i++)
        {
            for(int j=0; j < w2.length(); j++)
            {
                if(w1.charAt(pos1) == w2.charAt(pos2))
                {
                    numberOfCrosses++;
                    System.out.println(w1 + " " + w2);
                }
            }
        }

        if(numberOfCrosses == 0)
        {
            System.out.print("Words do not cross");
        }
    }




}

Recommended Answers

All 7 Replies

Tag this with Java so it shows up in the best place to get an answer.

That looks like a decent piece of code - what do you want it yo do that it doesn't do now?

I need it to take the two words and see how many times the word can cross and print that out like so:
b
lottery
a
t

b
o
a
lottery

.... and so on

Those pos1 and pos2 variables are always 0 so it's constantly comparing the first two letters. Your i and j however remain unused.

Break it down into easier pieces...
suppose you have successfully worked out a place where the two words cross - say pos1 and pos2 for example. Now you have a simpler task of printing the two words at those positions, eg

void printCrossedWords(String word1, String word2, int pos1, int pos2) {
  // print the letters of word 1, one letter per line, preceeded by (pos2-1) blanks
  // except when you get to the pos1'th line, print word2 instead
}

I changed my code and now this is what it is printing out:

lottery boat
lottery boat
lottery boat
b
o
a
t

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

            String w1 = args[0];
            String w2 = args[1];

            int numberOfCrosses = 0;

            int pos1 = 0;
            int pos2 = 0;


            for(int i=0; i < w1.length(); i++)
            {
                for(int j=0; j < w2.length(); j++)
                {   
                    if(w1.charAt(i) == w2.charAt(j))
                    {
                        numberOfCrosses++;
                        System.out.println(w1 + " " + w2);
                    }
                }
            }
            for(char ch : w2.toCharArray())
                {
                    System.out.println(ch);
                }

        if(numberOfCrosses == 0)
        {
            System.out.println("Words do not cross ");
        }
    }

    private static boolean crossesAt(String w1, int pos1, String w2, int pos2)
    {
        if(w1.charAt(pos1) == w2.charAt(pos2))
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    private static void printCross(String w1, int pos1, String w2, int pos2)
    {

        for(int i=0; i < w1.length(); i++)
        {
            for(int j = 0; j < w2.length(); j++)
            {


                    if(j== pos1)
                    {
                    char letter = w2.charAt(i);
                    System.out.print(letter);
                    }
                    if(i== pos2)
                    {
                    char letter1 = w1.charAt(j);
                    System.out.print(letter1);
                    }
                    else
                    {
                    System.out.print(' ');
                    }




            }

        }



    }



}

I am so stuck please help!!

I posted some pseudocode earlier that gives you the logic for printing crossed words.
Try to convert that into Java and see how far you can get.
I'm on holiday right now in the desert in Dubai, but if you do as I suggest and you get stuck I'll help
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.