Help Pulling out data from arrays and arraylist


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Help Pulling out data from arrays and arraylist

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Help Pulling out data from arrays and arraylist

    Okay I need help here with one method I have an array and It works fine I can pull out the string that was put into the array with the other arraylist, I have an inconsistency and it only pulls out nulls or numbers? I would like to pull out the user input and print it out, Here's the one that works,

    public void print()
    {
    int word = 0;
    while(word < words.size()){
    System.out.println(words.get(word));
    word++;
    }
    }

    Here's the one that doesn't work, can't figure it out

    public void print()
    {

    int word = 0;
    while(word < wordArray1.length){
    System.out.println(wordArray1[word]);
    word++;
    }
    }

    It's not printing out correctly, it prints nulls, It's not grabbing the user input

  2. #2
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    nothing wrong with that code above.
    where you set values of wordArray1 , look at there.

  3. #3
    Join Date
    Oct 2005
    Posts
    22
    As mr1yh1 said, there is nothing wrong in the code, But see the scope of wordArray1(whether it is still available in the method print) and also the way you set the values.

    thanks
    G.Rajasekhar

  4. #4
    Join Date
    Sep 2004
    Posts
    103
    public void print()
    {

    int word = 0;
    while(word < wordArray1.length){
    System.out.println(wordArray1[word]);
    word++;
    }
    }

    When I print it out wordArray1[word] prints out the numbers 0 through 9 In the one above

    System.out.println(words.get(word)); This prints out a string of words that the user inputed, so If I typed in "Bug" it would print out the word "Bug" when I call on this method, in the one
    System.out.println(wordArray1[word]);

    it prints out what I set the array to which the length was 10? And I want it to print out "Bug", I don't understand what mr1yh1 is trying to say, let me start over again

    this is the program that works, the print methods prints the users string input

    public class WordList
    {

    // Storage for a number of notes.
    private ArrayList<String> words;

    /**
    * Constructor for objects of class WordList
    */
    public WordList()
    {
    words = new ArrayList<String>();
    }

    public void add(String s)
    {
    words.add(s);
    }

    public void print()
    {
    int word = 0;
    while(word < words.size()){
    System.out.println(words.get(word));
    word++;
    }
    }

    /**
    * <put your description of the search method here>
    *
    * @param s if string s is in the list, return true.

    public boolean search(String s)
    {
    int word = 0;
    while(word < words.size()){
    String s2 = words.get(word);
    if(s.equals(s2)){
    return true;

    }
    word ++;
    }
    return false;
    }


    /**
    * This is a test method that adds words to your word list so
    * you don't have to do it by hand. You should change this method
    * so that it adds more than one word to the word list.
    */
    public void generateWordList()
    {
    add( "telephone" );
    add( "cell phone");
    add( "oil change");
    add( "mail");
    }
    }

  5. #5
    Join Date
    Sep 2004
    Posts
    103
    Here's the program that doesn't print out the string array

    public class WordArray
    {


    private String[] wordArray1;


    public WordArray()
    {
    wordArray1 = new String[10];
    }

    public void add(String s)
    {
    int a = 0;
    a = (s.length()-1);
    }

    public void print()
    {

    int word = 0;
    while(word < wordArray1.length){
    System.out.println(wordArray1[word]);
    word++;
    }
    }

    public boolean search(String s)
    {
    for(int word = 0; word < wordArray1.length; word ++){
    String s2 = wordArray1[word];
    if(s.equals(s2)){
    return true;

    }
    }
    return false;
    }


    public void generateWordList()
    {
    add( "telephone" );
    add( "microwave" );
    add( "dentist" );
    add( "groceries" );
    }
    }

  6. #6
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    public void add(String s)
    {
    int a = 0;
    a = (s.length()-1);
    }

    that code does nothing,
    where you set values of wordArray1 ?.

    you can put a value into an array in such way:
    wordArray1 [ indice ] = value

    0<= indice < wordArray1.length

  7. #7
    Join Date
    Sep 2004
    Posts
    103
    what does this mean?
    wordArray1 [ indice ] = value

    what's the indice mean?

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    indice == index

  9. #9
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Your "add()" method isn't adding anything to your array - it is just giving a value to variable called s.

    perhaps you were thinking about something like:

    Code:
    int s = 0;
    public void add( String newWord )
    {
         if (s < wordArray1.length )
         {
              wordArray1[ s ] = newWord;
              s++;
         }
         else
         {
              System.out.println( "Error. The list is full." );
         }
    }

Similar Threads

  1. Alternative data structures to ArrayList
    By jamiecottonuk in forum Java
    Replies: 1
    Last Post: 04-03-2005, 11:33 AM
  2. Rob - Re: Control Arrays and Accessible........
    By Rob Teixeira in forum .NET
    Replies: 11
    Last Post: 03-28-2001, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center