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

Thread: Bug with an ArrayList

  1. #1
    Junior Member
    Join Date
    Dec 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Bug with an ArrayList

    Hello, community!

    I've been working on a small app with a very simple algorithm. Said app must create 50 different objects with randomly generated atributtes (name, id and phone number must be random for each one of them) and store them in a Collection (ArrayList) which must be place in a .dat archive. A second app must retrieve the List and print it via console.

    First part seems to be well implemented, for each individual object can be printed. Override of the Object toString() is as follows:

    @Override
        public String toString(){
            String challenger = name+"\t\t"+id;
            return challenger;
        }

    The problem begins when trying to print the list, for the console prints only:

    // [Adrian 9109720 - Q] Whitneyener // when the print of the whole list is called via the System.out.println(list) or System.out.println(in.readObject()) command.

    Thanks in advance, for any small help is much appreciated!!

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
     
            String name = "", id = "";
            int phone;
     
            ArrayList<Aspirante> list = new ArrayList<>();
     
            for (int i = 0; i < 50; ++i) {
                name = nameRandom();
                id = Integer.toString((int) (Math.random() * 10000000)) + " - " + capitalRandomChar();
                phone = (int) (Math.random() * 10000000);
                list.add(new Aspirante(name, id, phone, i));
            }
     
            try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("lib\\list.dat"))) {
                out.writeObject(list);
                out.close();
            }
     
            try (ObjectInputStream in = new ObjectInputStream(new 
    FileInputStream("lib\\list.dat"))) {
                System.out.println(in.readObject());
                in.close();
            }
        }
    Last edited by salvadorInfor; December 30th, 2022 at 03:39 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bug with an ArrayList

    Can you make a small, simple, complete program that compiles, executes and shows the problem so we can see what is happening when it is executed for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bug with an ArrayList

    Thanks a million Norm.

    As it turned out, writing a fresh, new code did result in a bugless execution. Sorry if I made you lose time. You do an incredible job here, keep up the good work!!

Similar Threads

  1. My first Post......
    By SKYMEE in forum Totally Off Topic
    Replies: 1
    Last Post: March 16th, 2021, 04:56 PM
  2. i cant post
    By KingShoes88 in forum Forum Updates & Feedback
    Replies: 1
    Last Post: August 26th, 2014, 07:19 AM
  3. Swing: allow / prohibit focus change
    By aigarzs in forum AWT / Java Swing
    Replies: 0
    Last Post: June 21st, 2014, 06:18 PM
  4. Why does my post always get deleted at every forum I post at?
    By WebDevGuy in forum Forum Updates & Feedback
    Replies: 3
    Last Post: April 30th, 2014, 11:30 AM
  5. First post!
    By coreymcdonald855 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 27th, 2014, 03:59 AM