So the problem is to get two words from the user and then see if they cross and if they do type them out like the example:
word1= lottery
word2= boat

Crosses would be:
b
lottery
a
t

b
o
a
lottery

b
o
a
lottery

I am pretty lost, but I know that it is probably easiest to make a 2 dimensional array and go from there.
Here is what I have so far:
Any ideas/suggestions/anything will help.
Thanks

public class Cross
{
	public static void main(String[] args)
	{
		//Creates a two dimensional array for the inputs
		private String[][] cross = new String[w1pos][w2pos];
	
		//Getting the words from the user
		Scanner kbd = new Scanner(System.in);
		System.out.println("Enter first word: ");
		String word1 = input.next();
		System.out.println("Enter second word: ");
		String word2 = input.next();

		//Turn words into arrays
		
		//See if words cross

		//write first word horizontally 

		//write second word vertically

		//make the words cross at matching letters

		//print crossing

	}
}

How about using String class methods to do this for you instead of convert words to array? You could use substring(int beginIndex, int endIndex) to get a character for a word as string, and then use indexOf(String str, int fromIndex) to search for the selected substring inside another string. If the returned value of indexOf() is greater than or equal to 0, the substring is found and print the result; otherwise, it is not found and just move on to the next character.

The String api doc is here.

Edited 6 Years Ago by Taywin: n/a

ok so, so far I have this but I am having trouble writing the main method. I know I have to check to see if the characters are the same using the boolean to see if they cross and if so then print them out. But I'm not sure how to start the main method

import java.util.Scanner;

public class Cross
{
	public static void main (String[]arg)
	{

		printCross("boat", 1, "goat", 1); //test for printcross		
	}
	
	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 y = 0; y < w2.length(); ++y)
		{
			if (y == pos2)
			{
				System.out.println(w1);
			}
			else
			{
				for (int i = 0; i < pos2; ++i)
				{
					System.out.print(" ");
				}
				System.out.println(w2.charAt(y));
			}
		}
	}
}

You can keep the main as what you had in the first post; however you need to fix it a little bit because you modified it before. The code for main should be below (fixed from yours).

public static void main(String[] args) {
    //Getting the words from the user
    Scanner kbd = new Scanner(System.in);  // you use kbd instead of input here
    System.out.println("Enter first word: ");
    String word1 = kbd.next();  // not input.next()
    System.out.println("Enter second word: ");
    String word2 = kbd.next();  // not input.next()
    Cross.printCross(word1, word2);
  }

You do not need crossAt() but you should check whether words are crossed right away inside your printCross() method. Your printCross() will need only 2 strings and must be public method or others can't call the method. Something like below code...

public static void printCross(String w1, String w2) {
    // iterate through each character in word 1
    for (int y = 0; y < w1.length(); y++) {
      // check if the char exists in word 2
      if (w2.indexOf(w1.substring(y,y+1))>=0) {  // found matched
        // found the match so display it here
      }
    }
    // You may have a flag to check if there is any matched found and
    // display a message if nothing is match (but I didn't see the
    // requirement in your first post).
  }

I have already showed you most of the code. The only part you need to do is to display it as you describe in your first post.

Edited 6 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.