Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problems with arrays of objects

    Hey, ok well im trying to create a kind of phone book in java. I have created an array of objects, each as a contact.
    This is a snip from my code

    public static void main(String[] args) {
    for(int i=0; i < nocont; i++){
    mycontact[i] = new Contact(); System.out.println("New Contact"); System.out.print("\nEnter First Name: "); mycontact[i].firstname = UserInput.readString(); int lenfirst = mycontact[i].firstname.length(); if (lenfirst == 0){ System.out.println("ERROR, No first name was entered\n");
    break;

    The array is 3 in length. The problem is, when you enter the first name, it fills all the array up with the firstname. Therefore you get no unique contacts they are all the very last one you input.

    Any help would be greatly accepted!

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Is there javascript or java language what you are looking for?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, well ive realised that my original post isnt easy to understand so ive changed it and reduced my code completely. I just want to put in 3 names into the array of objects and get it to repeat it back to me.


    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);

    }


    }


    ok so for example you run the script, and put in the names, john, tom, andrew.
    john should be array[0].firstname
    tom should be array[1].firstname
    andrew should be array[2].firstname

    I dont understand why it doesnt print those all out again for you. Instead i get tom repeated 3x
    Could you explain what i am doing wrong?

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    As I have said... Your code is in Java, but here is a Javascript Forum. Java and Javascript have nothing in common... Ask moderator to move this thread to a proper Forum
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i see what u mean lol ok thanks

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There's also a java section under computer programming...
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •