okay i have question about how to apply those to my code.

first the Hashmap, i used the code it and i entered information inside

dic.put(studentName, new Student( studentName, studentSurname, studentSubject, daysBookBorrowed, booksName));

this is what i put inside. now i want to extract studentName , i want the user to type the name , and the object type should be retrieved. i tried to use containsKey, but it doesnt work well.

System.out.println("Whose data do you want to look at?");
        studentName=console.readLine();
        for(int i=0;i<dic.size();i++){
            if(dic.containsKey(book.studentName)){
                System.out.println(dic.get(book));
            }
        }

The other question which i have:
i made a linked list with nodes in that array, and i want to sort the nodes using comparator<> interface (e.g. by name..) is it possible?

i have a comparator function.. in the class:

class Facebookfriends{
     Facebookfriends info;
     String name;
     int age;
     Facebookfriends next;
    public Facebookfriends(String studentName,int age) {
        super();
        this.name = name;
        this.age = age;
    }
     public int compareTo(Facebookfriends another) {
            return this.name.compareTo(another.name);
     
     }
     
 }

it doesnt work well.

Please explain what happens.
For debugging: print out the String being searched for with delimiters to verify the computer is using what you expect.
What is the relationship between studentName and book.studentName?

i have a comparator function.. in the class:

Can you explain what a "comparator function" is? Java doesn't have functions.

Where is the class that implements the Comparator interface?

class Student{
    String studentName;
    String studentSurname;
    String studentSubject;
    int daysBookBorrowed;
    String booksName;
    @Override
    public String toString() {
        return "Student [booksName=" + booksName + ", daysBookBorrowed="
                + daysBookBorrowed + ", studentName=" + studentName
                + ", studentSubject=" + studentSubject + ", studentSurname="
                + studentSurname + "]";
    }
    public Student(String studentName, String studentSurname, String studentSubject, int daysBookBorrowed, String booksName) {
        super();
        this.studentName = studentName;
        this.studentSurname = studentSurname;
        this.studentSubject = studentSubject;
        this.daysBookBorrowed = daysBookBorrowed;
        this.booksName = booksName;
    }
    
}

this is my object class. what i want to do is to compare the name that the user gives me to the name in a particular object type that is inside the hashmap.

so i thought to use containskey, but it wont do the iteration and print..

Did you read and understand my previous post? Let me repeat it in case you didn't read it:

For debugging: print out the String being searched for with delimiters to verify the computer is using what you expect.
What is the relationship between studentName and book.studentName?

import java.io.*;
import java.util.HashMap;

public class StudentSearchSystem{
    public static void main(String[] args){
        Console console=System.console();
        String studentName = null;
        String studentSurname = null;
        String studentSubject = null;
        int daysBookBorrowed = 0;
        String booksName = null;
        String stop;
        
        HashMap<String, Object> dic=new HashMap<String, Object>();
        Student book=new Student( studentName,  studentSurname, studentSubject,  daysBookBorrowed,  booksName);
        while (true) {
                        
            System.out.println("Whats the students name");
            studentName=console.readLine();
            
            
            System.out.println("Whats the students surname ");
            studentSurname=console.readLine();
            
            System.out.println("Whats the students subject name ");
            studentSubject=console.readLine();
            
            
            
            System.out.println("How many days has the student been borrowing the book? ");
            daysBookBorrowed=Integer.parseInt(console.readLine());
            
            System.out.println("Whats the books name ");
            booksName=console.readLine();
            
            System.out.println("Do you want to put another students details or to stop (press Stop to stop) ");
            stop=console.readLine();
            if(stop.equals("STOP"))
                break;
                 
                 
            dic.put(studentName, new Student( studentName,  studentSurname, studentSubject,  daysBookBorrowed,  booksName));
        }
//////////////////////////////////////////////////////////////////////////////////        
        System.out.println("Whose data do you want to look at?");
        studentName=console.readLine();
        for(int i=0;i<dic.size();i++){
            if(dic.containsKey(book.studentName)){// what i am trying to do is to search the input (studentName)..in the hashmap , which contains objects
                System.out.println(dic.get(book));
            }
        }
    }
    
}
/////////////////////////////////////////////////////////////////////////////





 class Student{
    String studentName;
    String studentSurname;
    String studentSubject;
    int daysBookBorrowed;
    String booksName;
    @Override
    public String toString() {
        return "Student [booksName=" + booksName + ", daysBookBorrowed="
                + daysBookBorrowed + ", studentName=" + studentName
                + ", studentSubject=" + studentSubject + ", studentSurname="
                + studentSurname + "]";
    }
    public Student(String studentName, String studentSurname,
            String studentSubject, int daysBookBorrowed, String booksName) {
        super();
        this.studentName = studentName;
        this.studentSurname = studentSurname;
        this.studentSubject = studentSubject;
        this.daysBookBorrowed = daysBookBorrowed;
        this.booksName = booksName;
    }
    
}

Thats the code. i am thinking how to get one limb out of the object. that is put studentName(the user will type it) and the system will search for the name in different parts of the objects in the hashmap

Did you read and understand my previous post?
Where is the debug code?
What is the relationship between studentName and book.studentName?

Did you read and understand my previous post?
Where is the debug code?
What is the relationship between studentName and book.studentName?

lol StudentName is th input the user types in.

book.studentName... is a reference to the student class


my question was concertning this line alone:

if(dic.containsKey(book.studentName)){//

i think think that i need to re-write it in a different way.. may be add nother Arraylist<> and use its methods to extract the data

Still don't see any debug code. I guess you'll keep changing the code until it works.

Good luck with your project.

Norm i could debug the whole thing using eclipse.

my question was,,
can you search a string inside an object which holds string

dic.containskey(person.name);


i think i need to change

HashMap<String, Object> dic=new HashMap<String, Object>();

i think i need to change Object to ArrayList<object>//

thats my mistake.. cause obviously the containskey method doesnt work

Hello again NewOrder. It looks like you just drove Norm away again. I've had a chance to re-charge my batteries, so I'll give this a quick try.
1. Going to "ArrayList<object>" won't help.
2. Do you just want to search on the student's name? If so, this is the key in the HashMap, so you can use containsKey. The problem with lines 47,48 is that you get the search name into the local variable studentName, but you then ignore that and use book.studentName for the search. book.studentName is an instance variable in some instance "book", but is NOT what you just entered for the search.
If you had attempted to debug this as Norm suggested (using either prints or Eclipse debug) you would have discovered this bug very quickly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.