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 8075283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:56:24+00:00 2026-06-05T14:56:24+00:00

I need to search an object array for a Name and then print out

  • 0

I need to search an object array for a Name and then print out all info corresponding to that name.

I have

public class AccessFriendlyFile {
private Friendlies[] fr = new Friendlies[100];
private int size = 0;

public AccessFriendlyFile (){
try {
    Scanner scFile = new Scanner(new File("Friends.txt"));
    String line, name, surname, cell, mail, landline;

    while (scFile.hasNext()){
        line = scFile.nextLine();
        Scanner sc = new Scanner(line).useDelimiter("#");
        name = sc.next();
        surname = sc.next();
        cell = sc.next();

        if (sc.hasNext()){
            mail = sc.next();
            landline= sc.next();
            fr[size] = new ExtendFriendlies(name, surname, cell, mail, landline);

        }

        else {
            fr[size]= new Friendlies(name, surname, cell);
        }

        size++;
        sc.close();
    }

}catch (FileNotFoundException ex){
    System.out.println("File not found");
}

How do I code a method that will search “fr” for a name and print out all corresponding info?

Many Thanks
Jesse

Edit:
Here is my Search method, that is currently not working.

public int Search(String name) {
    int loop = 0;
    int pos = -1;
    boolean found = false;
    while (found == false) {
        if (fr[loop] == name) {
            found = true;
            pos = loop;
        } else {
            loop++;
        }
    }

    return pos;
}

Incomparable types error on the if statement.

  • 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-06-05T14:56:25+00:00Added an answer on June 5, 2026 at 2:56 pm

    I would suggest that you rename your variables here. The Friendlies class stores, I think, a single contact, a Friend. The list of Friend objects is an array that you might beter name friendList or even friendlies. I would also encourage you to not use size as a counter variable. Size is how many friends you have, and you can iterate through them using i, or friendCounter, or use a for each loop as I demonstrate below,

    public Friendlies find(String name) {
        for(Friendlies friend : fr) {
            if(friend.getName().equalsIgnoreCase(name))
                return fiend;
        }
        return null;
    
    }
    
    //now to print the info you can do this:
    Friendlies findJoe = find("Joe");
    if(findJoe==null)
        System.out.println("You have no friends namd Joe.");
    else
        System.out.println(findJoe);
    

    My code assumes that you implement toString() in Friendlies. If you use netbeans, you can auto-generate this code and then tweak it to get the format you want. (Just right-click where you want to write the method and choose insert code)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often have the need to search a javascript array that contains objects. I
So I have a search controller that returns a array of Object IDs (in
Considering I have an array of objects, and all objects represent something out of
I need to search an array of objects with an object of search terms
I have an array with dictionaries, and need to search trough the array and
I have this object of class Array >> answers_to_problem => [#<Answer id: 807, problem_id:
I need to implement a Query Object Pattern in Java for my customizable search
I need to search for all users containing a certain text string in their
I need to search a name which is stored in collection. Search criteria: eg:
I need to get the URL search paramentes in an object, for eg; http://example.com/?a=x&b=y&d#pqr

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.