Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Project

  • 02-04-2012 3:06pm
    #1
    Registered Users, Registered Users 2 Posts: 4,698 ✭✭✭


    Hi, doing a Word Search in java for a project in college. I'm pretty new to programming (only been doing it the past few months), and so this is a pretty basic question.

    I have a constructor in which the user specifies the words to be used in the 2d array (word search grid). Basically I think I have to store these input words so that I can use them in a later method to generate the array. Here is the code:

    public class WordSearchPuzzle
    {
    private char[][] puzzle ;
    private ArrayList<String> puzzleWords ;

    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {

    // puzzle generation using user specified words
    // The user passes in a list of words to be used
    // for generating the word search grid.
    }

    I have tried userSpecifiedWords = new ArrayList<String>(); but it doesn't seem to work. Thanks in advance, keep in mind I am a complete newbie when it comes to programming.


Comments

  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Hi OP,

    How exactly is it not working? are you sure you have this at the top of your code:

    import java.util.*;

    Array list needs this import to work...


  • Registered Users, Registered Users 2 Posts: 4,698 ✭✭✭Gumbi


    Yup, I have that (import java.util.* ;
    import java.io.* ;) as my first lines. By using userSpecifiedWords = new ArrayList<String>(); I am getting errors when inputting the string (ie the userspecifiedwords).

    Lol the wink is supposed to be code and my closing bracket haha


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    How are you adding the Strings to the list? Are you reading them from user input? Are you sure they are actual Strings (user input can arrive to the JVM in may forms)
    ArrayList<String> aList = new ArrayList<String>();
            
    String aString = "How de do de";
            
    aList.add(aString);
    


  • Registered Users, Registered Users 2 Posts: 4,698 ✭✭✭Gumbi


    Yes, the strings are user specified words. For example, the user may wish to specify the words cat, dog and mouse. AFAIK, this section of the code has nothing to do with generating the array, but rather "storing" the words to be used by the method which will generate the array. I hope I'm making myself clear :)

    Thanks a lot for your help so far.


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Not the most eloquent solution but perhaps a pointer for you...
    String[] days = {"Monday", "Tuesday", "Wednsday"};
            
    ArrayList<String> aList = new ArrayList<String>();
            
    for(String eachString : days)
    {
        aList.add(eachString);
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,698 ✭✭✭Gumbi


    I'm not sure you understand. The words are user input, ie when the programme is run. The words aren't specified within the code.


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    Could you confirm whether you are having difficulty:
    1. Reading user inputs into your application,
    2. Populating an ArrayList<string>, (if so Kidchamelons approach is the same except you change hard coded words for user input words)
    3. Passing the user input data to the WordSearchPuzzle class.

    You should have all your data read before calling the constructor as opposed to reading the data in it.


  • Registered Users, Registered Users 2 Posts: 2,089 ✭✭✭henryporter


    This looks suspiciously like the UL Games First Year Project:D


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    Gumbi wrote: »
    I have tried userSpecifiedWords = new ArrayList<String>();
    Since userSpecifiedWords is the array of words being pass in ... shouldn't this be reversed? i.e. you set something to this value rather than set this value?
    Something like
    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
       this.puzzleWords = userSpecifiedWords
    
    // puzzle generation using user specified words
    // The user passes in a list of words to be used
    // for generating the word search grid.
    }
    
    You don't need to initialize the userSpecifiedWords with
    userSpecifiedWords = new ArrayList<String>();
    
    You effectively do this when you defined the method
    WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    


  • Registered Users, Registered Users 2 Posts: 4,698 ✭✭✭Gumbi


    I feel like a fool now... I'm almost certain that's correct. I completely over thought it. Thanks much, I'll let ye know how I get on when I start coding more tomorrow.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,698 ✭✭✭Gumbi


    This looks suspiciously like the UL Games First Year Project:D

    It is what it is and that is what it is. ;)


  • Registered Users, Registered Users 2 Posts: 4,698 ✭✭✭Gumbi


    Getting the following error. What's going on? Thanks again. Here's my code so far:

    public class WordSearchPuzzle
    {
    private char[][] puzzle ;
    private ArrayList<String> puzzleWords ;

    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
    this.puzzleWords = userSpecifiedWords ;

    // puzzle generation using user specified words
    // The user passes in a list of words to be used
    // for generating the word search grid.
    }


  • Registered Users, Registered Users 2 Posts: 806 ✭✭✭Niall09


    It is expecting an arrayList object, but you are entering an array of 2 strings.

    Do you have a driver method for this project? (or you create instances of another class which you have specified a method where you can add words to the arrayList). You would create an arrayList in that, and then add your words one by one. You then pass that arrayList to your new instance of Wordsearch puzzle.


  • Registered Users, Registered Users 2 Posts: 131 ✭✭CuAnnan


    Hi OP,

    How exactly is it not working? are you sure you have this at the top of your code:

    import java.util.*;

    Array list needs this import to work...
    No it doesn't.
    You don't need util.* and importing .* because you're using one class in that package is bad practice.
    import java.util.ArrayList


  • Registered Users, Registered Users 2 Posts: 28 Dmeaney92


    Niall09 wrote: »
    It is expecting an arrayList object, but you are entering an array of 2 strings.

    What do you mean by its expecting an arrayList object? Why wont it accept the two strings entered?


  • Registered Users, Registered Users 2 Posts: 806 ✭✭✭Niall09


    Dmeaney92 wrote: »
    What do you mean by its expecting an arrayList object? Why wont it accept the two strings entered?

    Because in your constructer, you have
    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    

    If you wanted to enter 2 strings, it would be
    public WordSearchPuzzle(String wordOne, String wordTwo)
    


  • Registered Users, Registered Users 2 Posts: 28 Dmeaney92


    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
    // puzzle generation using user specified words
    // The user passes in a list of words to be used
    // for generating the word search grid.
    }


    Still having trouble with this one. I dont know how we get the user to input the strings that the user wants to use even though (ArrayList<String> userSpecifiedWords) is in the header. We just dont know how to store the words or even get the user to input the Strings


  • Registered Users, Registered Users 2 Posts: 130 ✭✭irishfeller


    Well have u got static main method in your code?

    If not you won't be able to run as an application.

    Add a static main method, prompt the user to type in words, capture the typed words and add them to an arraylist, then pass the arraylist to your class...


  • Registered Users, Registered Users 2 Posts: 437 ✭✭t1mm


    BlueJ is being used - you can add objects to the program at runtime manually. This is just how some people choose to teach programming.

    Whatever is between the brackets in your constructor defines what data can be passed into it when a new object is created. Your constructor is set to accept an ArrayList of type String. (ArrayList<String>). When you're creating a new object in BlueJ at runtime, it looks like you're trying to pass in an array of strings (String[]).


Advertisement