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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:32:20+00:00 2026-06-13T14:32:20+00:00

Im writing a method to return a specific record in an array however it

  • 0

Im writing a method to return a specific record in an array however it throws up two errors and Im not sure how to fix it. Can anyone explain what I’m doing wrong?

public String find(String searchName) 
{ // ERROR - MISSING RETURN STATEMENT
    Iterator<TelEntry> iterator = Directory.entries.iterator();
    boolean hasFound = false;
    while (iterator.hasNext()) 
    {
        TelEntry entry = iterator.next();

        if (entry.name.equalsIgnoreCase(searchName)) {
            return entry.name + entry.telNo;
            hasFound = true; // ERROR UNREACHABLE STATEMENT
        }

    }
    if (hasFound==false)
    {
        System.out.println("sorry, there is noone by that name in the Directory. Check your spelling and try again");
    }
}

Can anyone explain what I’ve done wrong?

  • 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-13T14:32:21+00:00Added an answer on June 13, 2026 at 2:32 pm

    The basic problem you have is that when a match is not found, you have no return statement. Usually, a method will return null is such cases, but you may want to return searchName, or even the error message – it depends on what the intention/contract of the method is (not stated).

    However, the other problem you have is that your code is way too complicated for what it’s doing, especially the hasFound variable is completely useless.

    Change your code to this, which does exactly the same thing but is expressed more elegantly:

    public String find(String searchName) {
        for (TelEntry entry : Directory.entries) {
            if (entry.name.equalsIgnoreCase(searchName)) {
                return entry.name + entry.telNo;
            }
        }
        System.out.println("sorry, there is noone by that name in the Directory. Check your spelling and try again");
        return null; // or return "searchName", the error message, or something else
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base method that I'm writing in order to not repeat the
I'm trying to writing a generic method that will load a record of a
I have a doubt about orkut's new URL Writing method. Earlier the home page
I'm writing a method which approximates the Kolmogorov complexity of a String by following
I'm writing a method that will resize the columns in a datagridview so that
I'm writing a method to wrap another method as part of a class. It
Validating Primitive Arguments and Complex Data Validating Arguments When writing a method, arguments should
I wonder if there is a way of writing a method or a class
I want to write clean code. So When writing a method I want to
I'm using StreamWriter to write log file. And this is the writing log method,

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.