Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

arrays & sorting arrays  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..i seem to be having problem making array seach work..i have phone book.. there is a search which would find the name and print name also phone number..it works for first name seach but not the others..thanks in advance..

public class phoneDetails
{
String name;
String phoneNum;

public String getName() {return name;}

public String getPhoneNum() {return phoneNum;}

public void setName(String n ) { name = n;}

public void setPhoneNum(String pn) {phoneNum = pn;}

phoneDetails() {} // default constructor

phoneDetails(String n, String pn)
{
name = n;
phoneNum = pn;
}
}

public class phoneDemo
{
public static void main(String[]args)
{


phoneDetails [] phoneBook = new phoneDetails [4];

phoneBook[0] = new phoneDetails ("sarah connor", "0116-325637");
phoneBook[1] = new phoneDetails ("john smith", "0033-3444530");
phoneBook[2] = new phoneDetails ("will green", "7778-876896");
phoneBook[3] = new phoneDetails ("fire starter", "9111-999999");

System.out.println("Name\t\tNumber");
for(int i = 0; i < phoneBook.length;i++)
System.out.println(phoneBook[i].getName() +"\t" +
phoneBook[i].getPhoneNum());

int loc = Search.linearSearch(phoneBook, "will green");

if (loc != -1)
{
System.out.println(phoneBook[loc].name + "\t"+
phoneBook[loc ].phoneNum);
}else
{
System.out.println("Not found");
}

}
}

class Search
{
// define method as static method; we can access it without
// creating an object of this class

// set to -1 for failed search

public static int linearSearch(phoneDetails [] arr, String name)
{
int location = -1;
for(int i = 0; i < arr.length;i++)
{
if (arr[i].name.equals(name)) // use equals() method to compare
// two string objects
// if true, store location
location = i;
break;
}
return location;
}
}
 
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "break" statement near the end of the program is always executed. You need to add a set of braces around the statements that are to be controlled by the "if".
 
kieran pattni
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!