Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6892895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:35:28+00:00 2026-05-27T06:35:28+00:00

I’m trying to output a string from an Object that has been passed into

  • 0

I’m trying to output a string from an Object that has been passed into a set. The Following line is where my problem lies. It outputs [alex, jane] but with correct formatting I believe it should be outputted at alex jane. i.e. without the comma separated value and the brackets from the array.

System.out.print(module.getStudents() + " ");

I’ve tried various solutions including:

System.out.prinf(%s, module.getStudents() + " ");

and

System.out.prinln(module.getStudents().[whatever Netbeans makes available] + " ");

To help you better understand the problem. The idea of the application so far is to allow a user to search for a mosule ans return all students connected to it. The full source code bar the driver is:

import java.util.*;

public class Control {

public void run() {

    Student jane = new Student("jane");
    Student alex = new Student("alex");

    Set<Student> students = new HashSet<Student>();
    students.add(jane);
    students.add(alex);

    Module ufce1 = new Module("UFCE1");
    Module ufce2 = new Module("UFCE2");

    Set<Module> modules = new HashSet<Module>();
    modules.add(ufce1);
    modules.add(ufce2);

    jane.addModule(ufce1);
    jane.addModule(ufce2);
    alex.addModule(ufce2);

    ufce1.addStudent(jane);
    ufce2.addStudent(jane);
    ufce2.addStudent(alex);

    System.out.println("Search module code: ");

    Scanner scan = new Scanner(System.in);        

    scan = new Scanner(System.in);
    String searchModule = scan.nextLine().trim();

    for (Module module : modules) {
        if (searchModule.equalsIgnoreCase(module.getName())) {

            Iterator it = students.iterator();
            Student student = (Student) it.next();
            if (student.getModules().contains(module)) {
                System.out.print(student + " ");
            }
        }
    }


}
}

Module Class:

import java.util.HashSet;
import java.util.Set;

public class Module {
private String name;
private Set<Student> students = new HashSet<Student>();

public Module(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void addStudent(Student student){
      students.add(student);
}

public Set<Student> getStudents() {
    return students;
}

@Override
public String toString() {
    return name; 
}

}

Student Class:

import java.util.HashSet;
import java.util.Set;

public class Student {
private String name;
  private Set<Module> modules = new HashSet<Module>();


public Student(String name) {
    this.name = name;

}

public String getName() {
    return name;
}

public void addModule(Module module){
      modules.add(module);
}

public Set<Module> getModules() {
    return modules;
}

@Override
public String toString() {
    return name;
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:35:29+00:00Added an answer on May 27, 2026 at 6:35 am

    When you do this System.out.print(module.getStudents() + " "); you’re implicitly calling the toString method on the HashSet instance. So to get the formatting you want, you have 2 choices:

    1. iterate over the set and print it the way you want
    2. Subclass HashSet and override toString to display the way you want it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.