Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help Passing Variables

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Help Passing Variables

    Based on the method descriptions below, how can I pass the Scanner from the main() method to the promptUserForSeed() method, and how can I use the long variable that the promptUserForSeed() method returns as the seed that gets passed to the findWords() method? I've tried creating static variables but I'm not sure that's helping. I just need to figure out how to configure the main() and promptUserForSeed() methods properly. Thank you!

    Method Descriptions:
    public static void main(String[] args)
    - Your main method should create a new Scanner object to read from the keyboard, pass the scanner to promptUserForSeed(), and use the long that it returns as the seed that gets passed to findWords(). Print out the WordBoard that you receive from findWords(), and then use WordBoard's checkAnswers() method to see if your code works as expected.

    public static long promptUserForSeed(Scanner input) - This method should print a prompt to the user asking them to input a seed. Read in their input using input.nextLong() and return that value. You do not need to account for the user inputting invalid seeds.

    public static WordBoard findWords(int rows, int cols, long seed) - This method should contain the bulk of your code. Using the given seed and dimensions, create a new WordBoard object, fetch the 2D array that WordBoard has generated, and perform your word search on that 2D array. Whenever you find a word, use the highlightWord() method to tell the WordBoard that you've found it. Return the WordBoard object once you are finished finding all of the words.

    Here's the code I'm working with:
    import java.util.Scanner;
    public class WordSearch
    {
    	static long seed;
    	static WordBoard board;
     
    	public static void main(String[] args)
    	{
    		board = new WordBoard(10, 10, seed);
    		System.out.println(WordSearch.findWords(10, 10, seed));
    		System.out.println(board.getSeed());
    	}
     
    	public static long promptUserForSeed(Scanner input)
    	{
    		System.out.println("Enter seed:");
    		seed = input.nextLong();
    		return seed;
    	}
     
    	public static WordBoard findWords(int rows, int cols, long seed)
    	{
    		board = new WordBoard(10, 10, seed);
    		System.out.println(board);
    		System.out.println(board.getSeed());
     
    		boolean foundLetter = false;
    		boolean foundWord = false;
    		int i2 = 0;
    		int j2 = 0;
    		String tempWord = new String();
     
    		char[][] puzzle = board.getBoard(); //Create a 2D char array called puzzle that is equal to the 2D array generated by WordBoard
    		for(String word : board.getDictionary()) //For each word in WordBoard's dictionary
    		{
    			char letter = word.charAt(0);
    			boolean up = false;
    			boolean down = false;
    			boolean left = false;
    			boolean right = false;
    			boolean upLeft = false;
    			boolean upRight = false;
    			boolean downLeft = false;
    			boolean downRight = false;
     
    			for(int i1 = 0; i1 < puzzle.length; i1++) //Loop through the puzzle rows
    			{
    				for(int j1 = 0; j1 < puzzle.length; j1++) //Loop through puzzle columns
    				{
    					if(puzzle[i1][j1] == letter) //When matched with a letter in the first index of a word in WordBoard's dictionary
    					{	
    						i2 = i1; //Copy location
    						j2 = j1;
     
    						foundLetter = true; //We found a letter
    						foundWord = false;
     
    						if(i1 >= word.length()) //Possible to go up in the grid
    						{
    							up = true;
    						}
    						if((i1 + word.length()) <= puzzle.length) //Possible to go down in the grid
    						{
    							down = true;
    						}
    						if(j1 >= word.length()) //Possible to go left in the grid
    						{
    							left = true;
    						}
    						if((j1 + word.length()) <= puzzle.length) //Possible to go right in the grid
    						{
    							right = true;
    						}
    						if(up == true && left == true) //Possible to go up and left in the grid
    						{
    							upLeft = true;
    						}
    						if(up == true && right == true) //Possible to go up and right in the grid
    						{
    							upRight = true;
    						}
    						if(down == true && left == true) //Possible to go down and left in the grid
    						{
    							downLeft = true;
    						}
    						if(down == true && right == true) //Possible to go down and right in the grid
    						{
    							downRight = true;
    						}
     
    						if(up == true && foundLetter == true && foundWord == false) //While at a matched letter, go up
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string of letters going up
    							{
    								tempWord += puzzle[i2][j2];
    								i2--;
    							}
     
    							if(word.equals(tempWord)) //If the dictionary word matches the temporary word going up
    							{
    								foundLetter = false;
    								foundWord = true; //We found a word
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position since we didn't find a word
    						j2 = j1;
    						if(down == true && foundLetter == true && foundWord == false) //While at a matched letter, go down
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string of letters going down
    							{
    								tempWord += puzzle[i2][j2];
    								i2++;
    							}
     
    							if(word.equals(tempWord)) //If the dictionary word matches the temporary word going down
    							{
    								foundLetter = false;
    								foundWord = true; //We found a word
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position since we didn't find a word
    						j2 = j1;
    						if(left == true && foundLetter == true && foundWord == false) //While at a matched letter, go left
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string of letters going left
    							{
    								tempWord += puzzle[i2][j2];
    								j2--;
    							}
     
    							if(word.equals(tempWord)) //If the dictionary word matches the temporary word going left
    							{
    								foundLetter = false;
    								foundWord = true; //We found a word
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
    							}
    						}
     
    						i2 = i1; //Reset the index for current letter position since we didn't find a word
    						j2 = j1;
    						if(right == true && foundLetter == true && foundWord == false) //While at a matched letter, go right
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string of letters going right
    							{
    								tempWord += puzzle[i2][j2];
    								j2++;
    							}
     
    							if(word.equals(tempWord)) //If the dictionary word matches the temporary word going right
    							{
    								foundLetter = false;
    								foundWord = true; //We found a word
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position since we didn't find a word
    						j2 = j1;
    						if(upLeft == true && foundLetter == true && foundWord == false) //While at a matched letter, go up and left
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string of letters going up and left
    							{
    								tempWord += puzzle[i2][j2];
    								j2--;
    								i2--;
    							}
     
    							if(word.equals(tempWord)) //If the dictionary word matches the temporary word going up and left
    							{
    								foundLetter = false;
    								foundWord = true; //We found a word
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
    							}
    						}
     
    						i2 = i1; //Reset the index for current letter position since we didn't find a word
    						j2 = j1;
    						if(upRight == true && foundLetter == true && foundWord == false) //While at a matched letter, go up and right
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string of letters going up and right
    							{
    								tempWord += puzzle[i2][j2];
    								j2++;
    								i1--;
    							}
     
    							if(word.equals(tempWord)) //If the dictionary word matches the temporary word going up and right
    							{
    								foundLetter = false;
    								foundWord = true; //We found a word
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position since we didn't find a word
    						j2 = j1;
    						if(downLeft == true && foundLetter == true && foundWord == false) //While at a matched letter, go down and left
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string of letters going down and left
    							{
    								tempWord += puzzle[i2][j2];
    								j2--;
    								i2++;
    							}
     
    							if(word.equals(tempWord)) //If the dictionary word matches the temporary word going down and left
    							{
    								foundLetter = false;
    								foundWord = true; //We found a word
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position since we didn't find a word
    						j2 = j1;
    						if(downRight == true && foundLetter == true && foundWord == false) //While at a matched letter, go down and right
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string of letters going down and right
    							{
    								tempWord += puzzle[i2][j2];
    								j2++;
    								i2++;
    							}
     
    							if(word.equals(tempWord)) //If the dictionary word matches the temporary word going down and right
    							{
    								foundLetter = false;
    								foundWord = true; //We found a word
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
    							}
    						}
    					}
    				}
    			}
    		}
    		return board; //Return the WordBoard object with highlighted words
    	}
    }
    Last edited by dataghost; April 30th, 2019 at 05:26 PM.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Help Passing Variables

    To pass Scanner object to promptUserForSeed
    public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            promptUserForSeed(input);
                      ....
        }
    Whatever you are, be a good one

Similar Threads

  1. Methods and passing variables
    By bamxmejia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2013, 10:09 PM
  2. Please help! Passing variables between modules.
    By Aberdeen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 7th, 2013, 10:18 PM
  3. passing variables from java applet to php
    By sabertooth in forum Java Networking
    Replies: 2
    Last Post: April 19th, 2013, 12:44 AM
  4. Help Passing Variables
    By jo15765 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 7th, 2012, 08:22 PM
  5. Passing variables with void methods
    By knightmetal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 21st, 2012, 08:46 PM

Tags for this Thread