Results 1 to 2 of 2
  1. #1
    mikethebiker is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default i have a problem with search in java

    I need some help whit a search function in a program. Iám a beginner in java. My program is below.
    What I need is, I will search by name and number how shall i do that.

    import java.util.Scanner;
    public class phonebook
    {

    public static void main(String[] args) // huvudmetoden
    {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Storleken på boken?");
    int size = keyboard.nextInt();
    int choice = -1;
    int pos = 0;

    String[]names = new String[size];
    int[]numbers = new int[size];
    while(choice != 6)
    {
    choice = menu(keyboard);
    switch(choice)
    {
    case 0:
    break;
    case 1:
    addPerson(names, numbers, keyboard, pos);
    pos++;
    break;
    case 2:
    break;
    case 3:
    break;
    case 4:
    sortera(names, numbers);
    break;
    case 5:
    showAll(names, numbers);
    break;
    case 6:
    System.out.print("Välkommen åter!");
    break;
    }
    }

    }

    public static int menu(Scanner keyboard)
    {
    System.out.println("\n1.Lägg in person");
    System.out.println("2.Sök på namn");
    System.out.println("3.Sök på telefonnummer");
    System.out.println("4.Sortera namnvis");
    System.out.println("5.Visa alla");
    System.out.println("6.Avsluta");
    System.out.print("Ditt val: ");

    int choice = keyboard.nextInt();

    return choice;
    }

    public static void addPerson(String[]names,int[]numbers, Scanner keyboard, int pos)
    {

    System.out.print("Namn: ");
    String name = keyboard.next();
    names[pos]= name;
    keyboard.next();
    System.out.print("Nummer: ");
    int number = keyboard.nextInt();
    numbers[pos] = number;
    }

    private static void sortera(String[]names, int[]numbers)
    {
    String tempstr1 = "";
    String tempstr2 = "";

    for (int i = 0; (i + 1) < names.length;)
    {
    if ( (names[i] != null) & (names[i+1] != null))
    {
    if (names[i].toUpperCase().compareTo(names[i + 1].toUpperCase()) > 0)
    {
    tempstr1 = names[i];
    tempstr2 = numbers[i];
    names[i] = names[i + 1];
    numbers[i] = numbers[i + 1];
    names[i + 1] = tempstr1;
    numbers[i + 1] = tempstr2;
    i = 0;
    }
    else
    {
    i++;
    }
    }
    else
    {
    i++;
    }
    }
    }

    public static void showAll(String[]names, int[]numbers)
    {
    for(int i = 0; i <= names.length-1; i++)
    {
    System.out.println(names[i] +" "+ numbers[i]);
    }
    }

    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Well, you could start by posting your code in code tags, otherwise it's really hard to read.

    And then you can tell us what you've tried and where it is going wrong.

Similar Threads

  1. Binary search problem
    By billy in forum New To Java
    Replies: 3
    Last Post: 10-08-2010, 08:43 PM
  2. problem with linear search
    By Metastar in forum New To Java
    Replies: 14
    Last Post: 09-14-2010, 08:01 PM
  3. Binary Search Problem
    By sharpnova in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 10:22 AM
  4. Help. Binary Search Problem
    By Krooger in forum Advanced Java
    Replies: 1
    Last Post: 11-03-2008, 06:19 AM
  5. Trouble with a search hw problem.
    By frasifrasi in forum New To Java
    Replies: 3
    Last Post: 07-10-2008, 09:26 PM

Posting Permissions

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