Results 1 to 2 of 2
  1. #1
    zoe
    zoe is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with string and array in java

    Hi, OK so for example you run the script, and put in the names, john, tom, andrew.
    Java Code:
    john should be array[0].firstname
    tom should be array[1].firstname
    andrew should be array[2].firstname
    I don't understand why it doesn't print those all out again for you. Instead i get tom repeated 3x
    Could you explain what i am doing wrong?

    Java Code:
    import java.io.*;
    
    
    public class PhoneBook {
    
    final static int nocont = 3;
    static Contact[] array = new Contact[3];
    
    
    public static void main(String[] args) {
    
    for (int i = 0; i < nocont; i++){
    array[i] = new Contact();
    System.out.println("Enter first name: ");
    array[i].firstname = UserInput.readString();
    
    }
    for(int j = 0; j < nocont; j++)
    System.out.println(array[j].firstname);
    
    }
    
    }
    Thanks

  2. #2
    fernando is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Where is the Contact class definition?
    Might be an issue with using static a bit too often.
    Strings are immutable.

    Personally, I would think Vector or ArrayList and StringBuffer would be a bit more suitable for this program.
    Greetings.

Similar Threads

  1. Convert a vector to a string array
    By orchid in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 02:31 AM
  2. How i add string array in vector
    By susan in forum New To Java
    Replies: 7
    Last Post: 02-12-2009, 01:32 PM
  3. JSP to output Java String Array
    By irenavassilia in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-31-2008, 04:11 PM
  4. String []Array
    By Warren in forum New To Java
    Replies: 4
    Last Post: 12-01-2007, 08:03 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 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
  •