help on array searching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfshake
    New Member
    • Nov 2007
    • 16

    help on array searching

    I am try to make the user enter name and password then ask them if they want to search. The menu has the options and i am having trouble with the search method. Here's my server:

    [CODE=java]import java.util.Scann er;

    public class StudentPassword {

    static Scanner reader = new Scanner (System.in);
    private int option;//the user option
    private int count = 0;//the count
    private String names[] = new String [50];
    private int passwords[] = new int [50];

    public StudentPassword (){

    while(true){
    setOption();
    selection();
    }//close whlie
    }//close main

    public int menu()
    {
    while(true){
    String menu = "----------\n1)Enter name and password\n2)Sea rch"
    + "\n3)Print\n4)Q uit\nMake a selection: ";
    try{
    System.out.prin t(menu);
    option = reader.nextInt( );
    if(option >= 1 && option <= 4){
    reader.nextLine ();//CONSUME EXTRA CHARACTER
    break;
    }else{
    System.out.prin t("\nEnter a number answer");
    reader.nextLine ();
    }//close if
    }catch(Exceptio n e){
    System.out.prin t("\nEnter a valid answer");
    reader.nextLine ();
    }//exception
    }//close whlie
    return option;
    }//close menu

    public void setOption()
    {
    option = menu();
    }//close setOption

    public int getOption(){
    return option;
    }//close getOption

    public void selection()
    {
    if(getOption() == 4){
    quit();
    }else if (getOption() == 3){
    print();
    }else if (option == 2){
    search();
    }else{
    getNames();
    }//close if
    }//close selection

    public void setName(String [] names2)
    {
    names = names2;
    }//close setRun

    public String [] getNames()
    {
    String nm;//the user local name
    for(int i = count; count < names.length; i++){
    System.out.prin t("Enter Your name or enter 'bye' to quit: ");
    nm = reader.nextLine ();
    if (nm.equalsIgnor eCase("bye")){
    break;
    }else{
    names[count] = nm;
    getPass();
    }//close if
    }//close for
    return names;
    }//close getRun

    public void setPass(int [] pass2)
    {
    passwords = pass2;
    }//close setRun

    public int [] getPass()
    {
    int pass;//the user local password
    for(int i = count; count < passwords.lengt h; i++){
    while(true){
    try{
    System.out.prin t("Enter Your password: ");
    pass = reader.nextInt( );
    passwords[count] = pass;
    count++;
    reader.nextLine ();
    break;
    }catch(Exceptio n e){
    System.out.prin t("\nEnter a number answer: ");
    reader.nextLine ();
    }//close exception
    }//close while
    break;
    }//close for
    return passwords;
    }//close run

    public void quit()
    {
    System.exit(99) ;
    }//close quit

    public void search()
    {
    int coresspondingPa ssword;
    for(int i = count; count < names.length; i++){
    String searchName;
    System.out.prin t("Enter Name: " );
    searchName = reader.nextLine ();
    if(searchName.e quals(names[count])){
    coresspondingPa ssword = passwords[count];
    System.out.prin t("The password is " + coresspondingPa ssword);
    break;
    }else{
    System.out.prin t(searchName + " not found");
    }//close if
    count++;
    }//close for
    }//close search

    public void print()
    {
    for(int i = count; count < names.length; i++){
    System.out.prin t(names[i]);
    }//close for
    }//close getPrint

    }//StudentPassword[/CODE]
    Last edited by Ganon11; Feb 16 '08, 04:48 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I'm sorry, what was your question?

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      I've run your codes below....

      It is recommended to use System.exit(0) than System.exit(99) ....
      it is for debugging purposes....

      names.length is the length of your array
      As you enter name and password, your count increments...
      So, your count is the number of elements+1 occupied by the inputs.

      about your code below,

      if i inputted 5 names and passwords,
      and your names.length is set to 20...
      your count = 6,

      @ search()

      for(int i = count; count < names.length; i++)
      (this code will start from the count until it reaches the max length...)
      How could you access your names and passwords? from 6 to 20?
      Yet your names and passwords are stored at reference 0 to 5 in your array....

      alternatives:

      for(int x=0; x<count; x++){
      names[x] and passwords[x].... print them...
      }


      hope it helps,
      sukatoa.... "Shadow Shaman"
      Last edited by sukatoa; Feb 17 '08, 01:03 AM. Reason: adding

      Comment

      • mfshake
        New Member
        • Nov 2007
        • 16

        #4
        Originally posted by sukatoa
        I've run your codes below....

        It is recommended to use System.exit(0) than System.exit(99) ....
        it is for debugging purposes....

        names.length is the length of your array
        As you enter name and password, your count increments...
        So, your count is the number of elements+1 occupied by the inputs.

        about your code below,

        if i inputted 5 names and passwords,
        and your names.length is set to 20...
        your count = 6,

        @ search()

        for(int i = count; count < names.length; i++)
        (this code will start from the count until it reaches the max length...)
        How could you access your names and passwords? from 6 to 20?
        Yet your names and passwords are stored at reference 0 to 5 in your array....

        alternatives:

        for(int x=0; x<count; x++){
        names[x] and passwords[x].... print them...
        }


        hope it helps,
        sukatoa.... "Shadow Shaman"
        So when i put data in my array when i choose option 1, how can i access that data and print it out. I didn't get your explanation.

        Comment

        • mfshake
          New Member
          • Nov 2007
          • 16

          #5
          Originally posted by mfshake
          So when i put data in my array when i choose option 1, how can i access that data and print it out. I didn't get your explanation.
          never mind i figured it out

          Comment

          Working...