Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare a Vector element with a user inputted variable

Status
Not open for further replies.

chriszd

Technical User
Apr 21, 2008
42
0
0
MT
Hi Guys,

I am trying to write a program in which the user has to enter a list of schools names with an ID according to the school.

So far all is working fine and the data inputed is being written to a Vector:

My Problem is how am i going to validate data in a Vector to verify that the user didn't enter the same ID for two different schools?


----------- MY CODE ---------------
//Ask user to enter School id
System.out.print("Enter School ID: ");
SchoolId = Keyboard.readInt();

boolean Check = schools.contains(SchoolId);

//if the schools id entered is already in vector dispaly user to re-enter the ID
if (Check == true)
{
do
{
System.out.print("School ID already entered ");
SchoolId = Keyboard.readInt();
}
while (Check == true);
}

// add school Id to the vector
schools.add(new Integer (SchoolId));

------------------- PROBLEM ------------------
Since the Boolean check is not being inputed in the loop the Boolean check is remaining obviously with his initial value (this creating an infinite loop when a double entry in inputed)

I if i put the Boolean statement in the DO brackets the While will give me an error that the Variable Check is not being found since it will be used locally in the DO brackets
 
Instead of using a vector have a look at using maps. With a map you have a key and a value. For you application the school id could be the key (assuming it's unique to each school) and the value could be the school name.

Maps contain methods such as containsValue and containsKey. You can use these to check the use hasn't reused a key.
 
Thanks for your response, i'll check them out but i'd rather use vectors since me and my friend who is building the application with me are more into vectors and we are kind of in a hurry to finish off the program for the client. in which at least he has the input functions working.

there is no other validation type which can be used in vectors?
 
If your program takes the id and name of the school how exatly does it store these two bits of information in the vector? The vector can only store one object (either the id or the name) unless you are placing both of these into another object and then storing that.

In your code you just seem to store the id of the school. You can see if the vector contains the id by using the contains method. See the java doc for the vector classes for more details
 
i managed to solve the problem i just initialized the boolean in the main and then compared the boolean variable with the current records in the vector...


thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top