Results 1 to 4 of 4
  1. #1
    ziongio is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Unhappy searching for a name in my 2d array and printing the info that is on its row

    I am trying to input the name and phone number of a person in the 2d array, then enter that same name and have the system search the array for the name and print out the name and number. I don't know what is wrong. Thanks for the help.


    import java.io.BufferedReader;
    import java.io.InputStreamReader;


    public class phone {
    public static void main(String arg[])throws java.io.IOException {
    InputStreamReader istream = new InputStreamReader(System.in);
    BufferedReader read = new BufferedReader(istream);
    {
    String numbers[][] = new String[3][2];
    int i;
    System.out.println("Enter new name: ");
    String r = read.readLine();
    for(i= 0; i < numbers.length; i++)
    r = numbers[i][0];
    System.out.println("Enter new number: ");
    String s = read.readLine();
    for(i= 0; i < numbers.length; i++)
    s = numbers[i][1];

    System.out.println("Enter name you are looking for: ");
    String x = read.readLine();

    for(i= 0; i < numbers.length; i++)
    if(numbers[i][0].equals(x))
    {
    System.out.println(numbers[i][0] +" : "+numbers[i][1]);
    }

    }
    }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Couldn't you use an Object rather than a 2D array?

    e.g.

    Java Code:
    public class People {
    
      private String name;
      private String phoneNo;
    
      People(String name, String phoneNo) {
        this.name = name;
        this.phoneNo = phoneNo;
      }
    
      public String getPhoneNoByName(String searchName) {
        String foundNumber = "No match.";
        for (People p:People) {
          if (p.name == searchName) foundNumber =  p.phoneNo;
        }
        return foundNumber;
      }
    
    }

    I mean... that's the whole point of Java :P

  3. #3
    ziongio is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the help but I have to use an array because multiple names and numbers have to be input by a user. And later the user can check for the numbers of people by inputing their name.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    but you can do that with the class I wrote for you

    to create a new "person":
    Java Code:
    People person1 = new People("John Doe","555-555-555");
    but use a Scanner to get the name and number strings into variables, then create 'new People' instance with those variables.

    to get the phone number:
    Java Code:
    String phoneNumber = People.getPhoneNoByName("John Doe");
    but again use a Scanner to allow the user to input the search term

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2011, 07:20 AM
  2. Searching and comparing Array elements
    By jmanswrd in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 07:06 AM
  3. how to grab info from array
    By skuzzie in forum New To Java
    Replies: 3
    Last Post: 12-09-2010, 02:26 PM
  4. Searching In a String Array - Problem
    By DillMan in forum New To Java
    Replies: 4
    Last Post: 12-07-2008, 10:12 PM
  5. Replies: 0
    Last Post: 04-14-2008, 09:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •