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 9 of 9

Thread: Class of arrays

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    23
    My Mood
    Daring
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Class of arrays

    I'm doing an assignment that's a separate class for a program called "MadChoosers" and THIS class creates an array of four MadChoosers called chooserList and the four chooserList arrays are chooserList[0]-to hold proper names; chooserList[1]-to hold adjectives; chooserList[2]-for nouns; and chooserList[3]- for verbs. I am new to arrays...still learning them in class and I understand the "idea" behind them and what not...I get the feeling that my code is wrong, I have been looking at the tutorial on here and that helps a little but when I try to use the same codes for Strings, it doesn't work...this is the code I have so far (I'll post both classes so you can see what's going on)
    public class MadChooser {
        //intstance variables
     
        String wordlist[];
        int firstEmpty;
     
        public static void main(String[] args) {
        }
        /*default constructor creates a size 20 wordlist(no strings yet) starts
         firstEmpty = 0*/
     
        public MadChooser() {
            wordlist = new String[20];
            int firstEmpty = 0;
        }
     
        /*parameterized constructor that takes an array of Strings as a parameter.
         Set wordlist to this array and firstEmpty to the length of the array (it 
         is totally filled). EC+ 10: instead of just setting worldist to the list 
         passed in, make wordlist a new array of the same size and copy all the 
         Strings over from the other wordlist.*/
        public MadChooser(String[] b) {
            wordlist = new String[b.length];
     
            for (int v = 0; v < wordlist.length; v++) {
                wordlist[v] = b[v];
            }
            firstEmpty = wordlist.length;
        }
     
        /*parameterized constructor that takes integer and makes wordlist that size
         (no words in it yet so firstEmpty is still 0)*/
        public MadChooser(int a) {
            a = 0;
            firstEmpty = a;
        }
     
        /*method add(String newWord) which given a string, adds it to wordlist in the first 
         * empty position, unless the array is full (check firstEmpty against the 
         * size of the array); when you add a new string, remember to update firstEmpty*/
        public void Add(String newWord) {
            if (firstEmpty < wordlist.length) {
                wordlist[firstEmpty] = newWord;
                firstEmpty++;
            }
     
        }
     
        /*Overload Add with a second method add(String[] newWords) which, given an
         * array of Strings adds all of them to wordlist(until it runs out of room)
         * Think abt what the loop needs to look like to make sure you don't run off
         * either array (there are several correct ways to do this)*/
        public void Add(String[] newWords) {
            for (int i = 0; i < newWords.length; i++) {
                this.Add(newWords[i]);
            }
        }
     
        /*Method pickWord(), returns a randomly chosen string from wordlist(it is
         * possible wordlist may not be full, make sure to only use words from that
         * are filled in (use firstEmpty); if wordlist has no words, return null.*/
        public String pickWord() {
            int w = (int) (Math.random() * firstEmpty);
            return wordlist[w];
        }
     
        /*EC+20: add method find, give String parameter, returns true if that string
         is already in wordlist, false otherwise. (hint == doesn't work with 
         Strings, need something like "firstString.equals(secondString)" Have the 
         add() method use find to check if a word being added is already in the list
         if so, don't add it*/
        public String find(String h) {
            if (h.equals(wordlist)) {
            }
            return h;
        }
    }

    **THIS IS THE CLASS I'M CONFUSED ON**
     
    public class ChooserList {
       /*write a program that creates an array of four MadChoosers, chooserList. 
        * chooserList[0] will be used to hold proper names, chooserList[1] will hold
        * adjectives, chooserList[2] nouns, and chooserList[3] verbs.*/
     
       String[] chooserListNames; //do I even need these?
       String[] chooserListAdj;
       String[] chooserListNouns;
       String[] chooserListVerbs;
     
     
      chooserList = new String [4]; //this isn't working right
     
    }


    Thx in advance guys!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Class of arrays

    "it doesn't work" provides us with zero information. Do you get errors? Then copy and paste the full and exact error messages here and indicate were in your code they are occurring.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    23
    My Mood
    Daring
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Class of arrays

    Yeah sorry abt that...I spoke with my professor today and have corrected the coding...but I am running into another problem with the chooserList class...here is the revised code
    public class MadChooser {
        //intstance variables
     
        String wordlist[];
        int firstEmpty;
     
        public static void main(String[] args) {
        }
        /*default constructor creates a size 20 wordlist(no strings yet) starts
         firstEmpty = 0*/
     
        public MadChooser() {
            wordlist = new String[20];
            int firstEmpty = 0;
        }
     
        /*parameterized constructor that takes an array of Strings as a parameter.
         Set wordlist to this array and firstEmpty to the length of the array (it 
         is totally filled). EC+ 10: instead of just setting worldist to the list 
         passed in, make wordlist a new array of the same size and copy all the 
         Strings over from the other wordlist.*/
        public MadChooser(String[] b) {
            wordlist = new String[b.length];
     
            for (int v = 0; v < wordlist.length; v++) {
                wordlist[v] = b[v];
            }
            firstEmpty = wordlist.length;
        }
     
        /*parameterized constructor that takes integer and makes wordlist that size
         (no words in it yet so firstEmpty is still 0)*/
        public MadChooser(int a) {
            wordlist = new String [a];
            firstEmpty = 0;
        }
     
        /*method add(String newWord) which given a string, adds it to wordlist in the first 
         * empty position, unless the array is full (check firstEmpty against the 
         * size of the array); when you add a new string, remember to update firstEmpty*/
        public void Add(String newWord) {
            if (firstEmpty < wordlist.length) {
                wordlist[firstEmpty] = newWord;
                firstEmpty++;
            }
     
        }
     
        /*Overload Add with a second method add(String[] newWords) which, given an
         * array of Strings adds all of them to wordlist(until it runs out of room)
         * Think abt what the loop needs to look like to make sure you don't run off
         * either array (there are several correct ways to do this)*/
        public void Add(String[] newWords) {
            for (int i = 0; i < newWords.length; i++) {
                this.Add(newWords[i]);
            }
        }
     
        /*Method pickWord(), returns a randomly chosen string from wordlist(it is
         * possible wordlist may not be full, make sure to only use words from that
         * are filled in (use firstEmpty); if wordlist has no words, return null.*/
        public String pickWord() {
            int w = (int) (Math.random() * firstEmpty);
            return wordlist[w];
        }
     
        /*EC+20: add method find, give String parameter, returns true if that string
         is already in wordlist, false otherwise. (hint == doesn't work with 
         Strings, need something like "firstString.equals(secondString)" Have the 
         add() method use find to check if a word being added is already in the list
         if so, don't add it*/
        public String find(String h) {
            if (h.equals(wordlist)) {
            }
            return h;
        }
     
    }

    Since this is like a "mad lib" thing she wants me to add more words to the to the chooserList class. Here's what I have so far and it's giving me errors (I'll point out where exactly.
    public static void main(String args[]) {
        /*write a program that creates an array of four MadChoosers, chooserList. 
        * chooserList[0] will be used to hold proper names, chooserList[1] will hold
        * adjectives, chooserList[2] nouns, and chooserList[3] verbs.*/
     
        MadChooser[] chooserList = new MadChooser[4];
     
        chooserList[0] = new MadChooser();
     chooserList[0].Add("James Bond", "Nemo"); /*when I add the extra strings I get error "no suitable method found for Add(String,String)
     method MadChooser.Add(String[]) is not applicable (actual and formal argument lists differ in length) method MadChooser.Add(String) is not applicable (actual and formal argument lists differ in length)"*/
     
        /*parameterized constructor (FOR 1) with an array of Strings*/
        String [] str = {"gigantic", "super"};
        chooserList[1] = new MadChooser(str);
     
       /*parameterized constructor (FOR 1) with a size given*/ 
        String [] n = {"avenue", "jewelry"};
        chooserList[2] = new MadChooser();
        chooserList[2].Add(n);
     
     
       /*(FOR 1) add words using both versions of the add method*/ 
        chooserList[3] = new MadChooser();
        String [] v = {"give", "multiply"};
        chooserList[3].Add(v);
        chooserList[3].Add(args);      
     
      /*print out 10 randominzed sentences using the MadChoosers in the array to fill
      * the words ([NAME] saw the [ADJECTIVE] [NOUN] [VERB])....at the end print a 
      completely random word by choosing a random element of chooserList and then
      using it to pick a random word.*/ 
        for (int i = 0; i <10; i++) {
             System.out.println(chooserList[0].pickWord() + " saw the " +
             chooserList[1].pickWord() + " " + chooserList[2].pickWord() 
             + " " + chooserList[3].pickWord() );
        }  
     
     
    }    
     
    }

    I added all the instructions for the assignment for each specific part of the assignment (please let me know if I somehow have something in the wrong place)...thx again in advance...I really appreciate it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,017
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Class of arrays

    it's giving me errors
    Please copy full text of error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    23
    My Mood
    Daring
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Class of arrays

    I did post the error...I'll shorten it lol
      /*write a program that creates an array of four MadChoosers, chooserList. 
        * chooserList[0] will be used to hold proper names, chooserList[1] will hold
        * adjectives, chooserList[2] nouns, and chooserList[3] verbs.*/
     
        MadChooser[] chooserList = new MadChooser[4];
     
        chooserList[0] = new MadChooser();
        chooserList[0].Add("James Bond", "Nemo","Capt America")//this is the issue;

    The error given is
     no suitable method found for Add(String,String,String)
        method MadChooser.Add(String[]) is not applicable
          (actual and formal argument lists differ in length)
        method MadChooser.Add(String) is not applicable
          (actual and formal argument lists differ in length)

    Any ideas? (If you're looking for my add() it's in my previous post)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,017
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Class of arrays

    The compiler can not find a version of the add() method with args that match those in the statement with the error.
    Either add an overload for the add() method that matches the args you want to use, or rewrite the call to the method to use an existing method.

    Try putting all the Strings into a String array. There is a version of the add() method that takes a String[]
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    23
    My Mood
    Daring
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Class of arrays

    To put all the Strings into a String array (the one I've already created that takes a String[]) how do I do that? I know this
    chooserList[0].Add(["James Bond", "Nemo", "Capt America"]);
    isn't it because I get an error saying
    illegal start of expression
     
    ']' expected
     
    incompatible types
      required: int
      found:    String

    So I was thinking of it being like this perhaps since I want it to "point" to the method (at least I believe that's what I'm trying to do)
    chooserList[0].Add() = {"James Bond", "Nemo", "Capt America"};



    But then I get this error
    illegal start of expression
     
    ';' expected
     
    not a statement
     
    no suitable method found for Add()
        method MadChooser.Add(String[]) is not applicable
          (actual and formal argument lists differ in length)
        method MadChooser.Add(String) is not applicable
          (actual and formal argument lists differ in length)


    What am I not understanding?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,017
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Class of arrays

    put all the Strings into a String array
    This line does it:
      String [] n = {"avenue", "jewelry"};
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Vessel06 (April 9th, 2013)

  10. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    23
    My Mood
    Daring
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Class of arrays

    Oooooooooh!!!!! THANK YOU!!!! THAT MAKES SENSE!!!!! I was totally missing that part! So it should be
      String[] w = {"James Bond", "Nemo", "Capt America"};
            chooserList[0] = new MadChooser();
            chooserList[0].Add(w);

Similar Threads

  1. Copying arrays from one class to another
    By hannah87 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2013, 01:54 PM
  2. Do Class Casts Work With Arrays?
    By snowguy13 in forum Collections and Generics
    Replies: 8
    Last Post: February 23rd, 2012, 07:18 AM
  3. [SOLVED] Arrays being initialized from another class
    By Java Man 9000 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: January 11th, 2012, 03:34 PM
  4. [SOLVED] Writing a program with arrays and class methods... PLEASE HELP?
    By syang in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 8th, 2011, 07:02 AM
  5. Issue with setting up different class with arrays
    By D-COD3R in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 06:09 PM

Tags for this Thread