javafor-loopprintingcrossword

How do I get one of my words to print vertically?


The words in this code are supposed to intersect at a common letter and print off as many times as the words can intersect each other.

I have the correct amount of times but the words are not formatting the correct way.

It is supposed to look like this:

 b
lottery
 a
 t

  b
  o
  a
lottery

   b
   o
   a
lottery

But mine are printing out like this:

b
lotto
    a
    t
                b
lotto
    a
    t
                b
lotto
    a
    t

My code is displayed below, what is wrong with my printing method that is causing this?

 public class Assg23
{
    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++;
                        crossesAt(w1, pos1, w2, pos2);
                        printCross(w1, pos1, w2, pos2);
                    }
                }
            }

        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) && i<w2.length())
                {
                    System.out.println(w2.charAt(i));
                }
                if((i == pos2) && j<w1.length())
                {   
                    System.out.print(w1.charAt(j));
                }
                else
                {
                    System.out.print(" ");
                }   

            }
        }



    }

Solution

  • Here is a correct version of your main() method. You'll notice that I removed the crossesAt() method because all it does is to compare two characters which can be done in less than one line of code.

    public static void main(String[] args) {
        String w1 = args[0];
        String w2 = args[1];
    
        int numberOfCrosses = 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++;
                    printCross(w1, i, w2, j);
                }
            }
        }
    
        if (numberOfCrosses == 0) {
            System.out.println("Words do not cross");
        }
    }
    

    And here is a correctly working implementation of your printCross() method:

    private static void printCross(String w1, int pos1, String w2, int pos2) {
        // you can replace this for-loop with the line that follows it
        // if you don't get compiler errors
        String spaces = "";
        for (int i=0; i < pos1; ++i) spaces += " ";
        //String spaces = String.format(String.format("%%0%dd", pos1), 0).replace("0", " ");
    
        for (int i=0; i < w2.length(); ++i) {
            if (i == pos2) {
                System.out.println(w1);
            }
            else {
                System.out.println(spaces + w2.charAt(i));
            }
        }
    }
    

    Here is an example of printCross() being used:

    printCross("boat", 1, "lottery", 1);
    printCross("coffee", 4, "beverage", 3);
    

    Output:

     l
    boat
     t
     t
     e
     r
     y
        b
        e
        v
    coffee
        r
        a
        g
        e