Comparing string array content with EditText content - android

Basically I have a textbox and an edittext box that looks like this:
Username [ ]
Then at the bottom there is a button called "Submit".
Simply enough, I have to type in a username, compare it to a string array that I have in my strings.xml file, and if it does not equal any of the strings in the array, then I am good to go.
The string array looks like this:
<string-array name="usernames">
<item>Bryan
<item>John</item>
<item>Matt
<item>Mike</item>
</string-array>
I am confused as to how I can do a simple if statement that in pseudo-code looks like:
if (username_entered_in_editText == usernameArray[contents])
{ submit_check = true; }
Any advice would be greatly appreciated!

Try something like this. Get an instance of your String[], get the text from your EditText with getText().toString(), and compare is to every name in the list. This isn't optimized, of course. If your String array is sorted, you could implement a binary search to make it faster, but the general theory here will work.
String[] usernames = getStringArray(R.array.usernames);
EditText editText = (EditText)findViewById(R.id.edittext);
String candidate = editText.getText().toString();
boolean submit_check = usernameTaken(candidate, usernames);
public boolean usernameTaken(String candidate, String[] usernames) {
for(String username : usernames) {
if(candidate.equals(username)) {
return true;
}
}
return false;
}

Related

how to set a string array name as a varible

I am trying to make place dropdowns for user locations in my app. I'm using Exposed Drop-Down Menu not spinners. I've set it up so that depending on which country you have your self set to, it changes the next dropdown to reflect that.
this is how I do it:
val country= resources.getStringArray(R.array.nations)
val arrayadapters = ArrayAdapter(requireContext(), R.layout.country_dropdown, country)
view.autoCompleteTextView.setAdapter(arrayadapters)
view.autoCompleteTextView2.setOnClickListener {
val abc = autoCompleteTextView.text.toString()
if(abc == "canada"){
var country2= resources.getStringArray(R.array.canada)
val arrayadapters2 = ArrayAdapter(requireContext(), R.layout.locality_dropdown, country2 )
view.autoCompleteTextView2.setAdapter(arrayadapters2)
}
if (abc == "us"){
var country2 = resources.getStringArray(R.array.american_states)
val arrayadapters2 = ArrayAdapter(requireContext(), R.layout.locality_dropdown, country2)
view.autoCompleteTextView2.setAdapter(arrayadapters2)
}
}
anyways this takes what country you have selected and depending on that gives you a drop-down of all the regions within that country
the problem that I'm having is that of code scalability, I don't think It would be wise or reasonable to copy, paste and modify for each and every locality.
so my question is this. how do you set the name of the string array to a variable so I can have 1 line of code and just switch out the name of the location and have it read the string array that way.
if you're wondering the string array is XML stored int the res\values\strings.xml as something like this
<string-array name="canada">
<item>Alberta</item>
<item>British Columbia</item>
<item>Manitoba</item>
<item>New Brunswick</item>
<item>Newfoundland and Labrador</item>
<item>Nova Scotia</item>
<item>Nunavut</item>
<item>Ontario</item>
<item>Prince Edward Island</item>
<item>Northwest Territories</item>
<item>Quebec</item>
<item>Saskatchewan</item>
<item>Yukon</item>
</string-array>
thank you for reading my question, if you have any questions please ask.
Edited: this was a Java solution, since question was originally tagged as asking about Java. Don't spray language tags at random.
This is what maps are for. A sketch of an approach, rather than full code, follows.
The setup:
class CountryStuff {
Country country;
ArrayAdapter adapter;
...etc...
CountryStuff(Country c, ArrayAdapter a, ...etc...) {
country = c;
adapter = a;
...etc...
}
}
Map<String,CountryStuff> map = new HashMap<>();
map.put("canada", new CountryStuff(...whatever...);
map.put("us", new CountryStuff(...whatever...);
The use:
String abc = "something"; // country name as determined before
CountryStuff stuff = map.get(abc);
if (stuff == null) {
... no such country ...
}
else {
country2 = stuff.country;
adapter2 = stuff.adapter;
... etc ...
}

Create words from random characters using the sqlite database

Let's just say I have the variable String SRand = "KELRSFGLIU", what I want to do is create a word from the SRand variable and search for that word in the database. Or looking for data in a database based on the SRand variable, the words that need to be found do not have to be 10 characters but can be 3, 4 - 10 characters
can this be done?
As an illustration, I want to do something like this:
Ilustration 1:
String SRand = "KELRSFGLIU";
String Suggestion = "";
private void Create_Suggestion(){
//The magic for creating Sugeestion in here
//The result can be "FIRE", "GLUE", "FUR", or something else.
Suggestion = ???;
SearchData(Suggestion);
}
private void SearchData(String Suggest){
//Data Must Be Found
}
Any Idea?
I suggest you use trees in case you want to do that, more info can be found here

How do I check a boolean string array list for a valid response in android?

I'm writing a text based game where movement is dictated by typing "go left," "go right," etc. I have a Boolean Array List with the valid commands, and I want to compare what the user types to the members in the array. The code I have rejects all commands as invalid. I believe it's because I'm returning false, but I'm not sure how to fix it. I'm very new to this, so any and all help is appreciated.
private boolean validCommand() {
ArrayList<Boolean> validCommand = new ArrayList<>();
validCommand.add(Boolean.valueOf("go left"));
validCommand.add(Boolean.valueOf("go right"));
validCommand.add(Boolean.valueOf("go straight"));
validCommand.add(Boolean.valueOf("go back"));
for (boolean checkCommand : validCommand) {
if (typeCommand.getText().toString().equals(checkCommand))
return true;
}
return false;
}
typeCommand is just the EditText the user types their command in.
you don't have to use ArrayList<Boolean>. you have to use ArrayList<String>.
Because you want to compare String to String.
private boolean validCommand() {
List<String> validCommand = new ArrayList<>();
validCommand.add("go left");
validCommand.add("go right");
validCommand.add("go straight");
validCommand.add("go back");
// List#contains() will return true if List contains arg, or false not.
return validCommand.contains(typeCommand.getText().toString());
}
Boolean.valueOf(String s) returns the boolean value of the string, ie "true" would return true. If you have random string, like "go left", it will return false. So if validity is your concern, I would change the array to an array of strings instead and compare the array string with what was typed. If the comparison returns true then you have a valid command.

comparing edittext input to string array

Im new, so sorry if my question is lame.
But, im trying to make an AI chatbot (like, a simpler version of cleverbot that responds to certain input keywords.)
I have an edittext panel, which the user will input words to 'talk' to the AI. But, instead of coding every word in the java file, i want to compare the string input to an existing string array to check if the keyword is there and so that the AI can display the coressponding answer.
example:
if input is: Hellothere!
and on the string array, there is: Hello.
and:
If edittext=Hello, then display this: blah blah.
Here is my (amateurish) code:
public void onClick(View v){
Resources res = getResources();
String[] usernames = res.getStringArray(R.array.input2);
boolean submit_check = input1(wordy, usernames);
public boolean input1(String wordy, String[] input2){
if(candidate.equals(usernames))
{
wahh.start();
myString = res.getStringArray(R.array.OUTPUT);
pic.setImageResource(R.drawable.keel);
String q = myString[rgenerator.nextInt(myString.length)];
display.setText(q);
}
else{
wahh.start();
pic.setImageResource(R.drawable.keel);
myString = res.getStringArray(R.array.OUTPUT);
String q = myString[rgenerator.nextInt(myString.length)];
display.setText(q);
}
I think what you want is something more along the lines of this (pseudocode):
if(EditText.getText().Contains("Hello")) {
EditText.setText("What's up?");
}
You'd want to check if it contains a selection from the array though. If it does, get the index of the array. Based on the index, respond accordingly. The easiest way to do that would be using a for loop and a switch statement. Although AI is actually a lot more complicated than this, and my knowledge.

Compare and return String values to a string-array in Xml Android

Good day, i will try to explain my problem as best as i can. i have in one of my class, getters and setters for a string object. Now in my getString() method, i am trying to compare the string to a string-array of items in my String.xml to see if any of the elements in the string-array file matches the given string and return that matched item element in the string array.
I have something like this so far:
for setPlace():
public void setPlace(String place) {
this.place = place;
}
for getPlace():
public String getPlace() {
//am stuck here and not sure how to compare this and return the correct item
if(place.equals(context.getResources().getStringArray(R.array.myPlacesArray))){
//return context.getResources().getString();
}
my Strings.xml file:
<string-array name="myPlacesArray">
<item>#string/myplace1</item>
<item>#string/myPlace2</item>
<item>#string/myPlace3</item>
</string-array>
<string name="myplace1">home</string>
<string name="myplace2">office</string>
<string name="myplace3">gym</string>
the reason i have to do this, is because it has different locales(languages) values and it would be a lot easier than writing a huge list of if/else or switch statements for different String elements and languages. Please any ideas is highly appreciated. Thank you
Check this Code for getting value from String.xml and compare :
String[] categoriesAndDescriptions =getResources().getStringArray(R.array.myPlacesArray);
for(String cad : categoriesAndDescriptions) {
String categoryAndDesc = cad;
Log.v("CategoryName", categoryAndDesc);
if(place.equalsIgnoreCase(categoryAndDesc)){
//Do your Stuffs here
}
}
You should just loop over the returned array, doing a place.equals on each item in that array, and return the match when they match.
If they're sorted (they're not in this case) you could use Arrays.binarySearch. You could also use Arrays.asList and call contains(place) on the resulting list, which is essentially the same as looping manually.
You'd need to decide what to return if there's no match (if that's even possible).

Categories

Resources