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: Method Adding elements to an array with certain restrictions

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Method Adding elements to an array with certain restrictions

    import java.util.Scanner;

    class WordTest1
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    WordGen.initialise(input);
    System.out.print("Enter the number of words you wish to generate: ");
    int n = input.nextInt();
    WordStore words = new WordStore(n);
    for(int i=0; i<n; i++)
    words.add(WordGen.make());
    String line = input.nextLine();
    System.out.println("Enter words to test, empty line to exit");
    line = input.nextLine();
    while(!line.equals(""))
    {
    String[] wordlist = line.split(" ");
    for(int i=0; i<wordlist.length; i++)
    {
    int count = words.count(wordlist[i]);
    System.out.print("\""+wordlist[i]+"\" ");
    if(count==0)
    System.out.println("NOT generated");
    else if(count==1)
    System.out.println("generated once");
    else
    System.out.println("generated "+count+" times ");
    }
    line = input.nextLine();
    }
    }
    }

    import java.util.*;

    class WordStore
    {

    public int a = 0;
    public WordStore(int n){
    a = n;

    }


    public String[] storewords = new String[a];
    public int count=0;

    public void add(String word){
    storewords[count] = word;
    count=count+1;
    }

    }

    WordGen.java just cretaes random words

    Why do i get an ArrayIndexOutOfBounds Exception: 0
    at WordStore.add(WordStore.java:20)
    at WordTest1.main(WordTest1.java:13)

    U cant change the code in WordTest1.java, but can change WordStore.java, but add method has to be declared

    public void add(String word)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Method Adding elements to an array with certain restrictions

    Quote Originally Posted by Newoor View Post
    Why do i get an ArrayIndexOutOfBounds Exception: 0
    at WordStore.add(WordStore.java:20)
    at WordTest1.main(WordTest1.java:13)
    You receive this error because you are intialiizing the array storewords with zero (the initial value of a). A more appropriate way is to initialize the array in your constructor:


     
    public String[] storewords;
     
    public WordStore(int n){
        a = n;
        storeworkds = new String[a];
    }

  3. The Following User Says Thank You to copeg For This Useful Post:

    Newoor (December 13th, 2009)

Similar Threads

  1. Java program to do Matrix operation
    By saladfingers73 in forum Collections and Generics
    Replies: 5
    Last Post: March 7th, 2012, 09:17 AM
  2. adding main method to a code
    By IDK12 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 31st, 2009, 08:52 PM
  3. adding get mothods to a class extending thread
    By aliaa2a in forum Object Oriented Programming
    Replies: 6
    Last Post: August 3rd, 2009, 06:41 AM
  4. Converting a method from ArrayList so it is capable with an Array
    By BlueJ1 in forum Collections and Generics
    Replies: 2
    Last Post: July 8th, 2009, 05:22 PM
  5. adding rows to coloumns. Netbeans
    By haygaurav in forum Java IDEs
    Replies: 1
    Last Post: April 1st, 2009, 03:16 PM