473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help on array searching

mfshake
16
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:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class StudentPassword {
  4.  
  5.     static Scanner reader = new Scanner (System.in);
  6.     private int option;//the user option
  7.     private int count = 0;//the count 
  8.     private String names[] = new String [50];
  9.     private int passwords[] = new int [50];
  10.  
  11.     public StudentPassword(){
  12.  
  13.         while(true){
  14.                 setOption();
  15.                 selection();    
  16.         }//close whlie
  17.     }//close main
  18.  
  19.     public int menu()
  20.     {
  21.         while(true){
  22.             String menu = "----------\n1)Enter name and password\n2)Search" 
  23.                 + "\n3)Print\n4)Quit\nMake a selection: ";
  24.             try{
  25.                 System.out.print(menu);
  26.                 option = reader.nextInt();
  27.                 if(option >= 1 && option <= 4){
  28.                     reader.nextLine();//CONSUME EXTRA CHARACTER
  29.                     break;
  30.                 }else{
  31.                     System.out.print("\nEnter a number answer");
  32.                     reader.nextLine();
  33.                 }//close if
  34.             }catch(Exception e){
  35.                 System.out.print("\nEnter a valid answer");
  36.                 reader.nextLine();
  37.             }//exception    
  38.         }//close whlie
  39.         return option;            
  40.     }//close menu
  41.  
  42.     public void setOption()
  43.     {
  44.         option = menu();
  45.     }//close setOption 
  46.  
  47.     public int getOption(){
  48.         return option;
  49.     }//close getOption
  50.  
  51.     public void selection() 
  52.     {
  53.         if(getOption() == 4){
  54.             quit();
  55.         }else if (getOption() == 3){
  56.             print();
  57.         }else if (option == 2){
  58.             search();
  59.         }else{
  60.             getNames();
  61.         }//close if
  62.     }//close selection
  63.  
  64.     public void setName(String [] names2)
  65.     {
  66.         names = names2;
  67.     }//close setRun
  68.  
  69.     public String [] getNames()
  70.     {
  71.         String nm;//the user local name
  72.         for(int i = count; count < names.length; i++){
  73.             System.out.print("Enter Your name or enter 'bye' to quit: ");
  74.             nm = reader.nextLine();
  75.             if (nm.equalsIgnoreCase("bye")){
  76.                 break;
  77.             }else{
  78.                 names[count] = nm;
  79.                 getPass();    
  80.             }//close if
  81.         }//close for
  82.         return names;
  83.     }//close getRun
  84.  
  85.     public void setPass(int [] pass2)
  86.     {
  87.         passwords = pass2;
  88.     }//close setRun
  89.  
  90.     public int [] getPass()
  91.     {
  92.         int pass;//the user local password 
  93.         for(int i = count; count < passwords.length; i++){
  94.                 while(true){
  95.                            try{
  96.                                System.out.print("Enter Your password: ");
  97.                                pass = reader.nextInt();
  98.                               passwords[count] = pass;
  99.                             count++;
  100.                             reader.nextLine();
  101.                             break;
  102.                         }catch(Exception e){
  103.                                System.out.print("\nEnter a number answer: ");
  104.                             reader.nextLine();
  105.                        }//close exception            
  106.                 }//close while
  107.                 break;
  108.             }//close for
  109.         return passwords;    
  110.     }//close run        
  111.  
  112.     public void quit()
  113.     {
  114.         System.exit(99);
  115.     }//close quit 
  116.  
  117.     public void search()
  118.     {
  119.         int coresspondingPassword;
  120.         for(int i = count; count < names.length; i++){
  121.             String searchName;
  122.             System.out.print("Enter Name: " );
  123.             searchName = reader.nextLine();
  124.                 if(searchName.equals(names[count])){
  125.                     coresspondingPassword = passwords[count];
  126.                     System.out.print("The password is " + coresspondingPassword);
  127.                     break;
  128.                 }else{
  129.                     System.out.print(searchName + " not found");    
  130.                 }//close if
  131.                 count++;
  132.         }//close for
  133.     }//close search
  134.  
  135.     public void print()
  136.     {
  137.         for(int i = count; count < names.length; i++){
  138.             System.out.print(names[i]);
  139.         }//close for
  140.     }//close getPrint
  141.  
  142. }//StudentPassword
Feb 16 '08 #1
4 1443
Ganon11
3,652 Expert 2GB
I'm sorry, what was your question?
Feb 16 '08 #2
sukatoa
539 512MB
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"
Feb 17 '08 #3
mfshake
16
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.
Feb 17 '08 #4
mfshake
16
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
Feb 17 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Sims | last post by:
Hi, Given a string $txt and an array of strings $txt_array what would be the best/fastest way to search in _insensitive_ case if $txt is in $text_array and, if it is, where is it? Because I...
9
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
0
by: Stephen | last post by:
I have been getting on well with help from this forum trying to create an array list and work with it. Everything is working fine apart from displaying my array list items into the labels in my...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
3
by: tsunami | last post by:
hi all; I have an array and want to insert all the elements from this array to a binary search tree.That array is an object of the class of a stringtype which includes overloaded "< > = =="...
2
by: Carlos K | last post by:
Hello I'm having some difficulty searching a set of rows in a DataRow collection.. This is my question What would be an efficient way to search any column of an DataRow array Let me try to...
8
by: hothead098 | last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material) For this assignment you are to: 1) Create and manage arrays a) One of type integers (containing 10 elements). b) One of...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
3
by: jac130 | last post by:
the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many values...but as the user enters the values, i need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.