Zip code size test C# - c#-4.0

I have a small piece of code that I have been trying to get to work, but nope. So I've come to the experts. I'm trying to test a zip code entry and limit it to 5 digits only. My first issue is with the attached code. It doesn't seem to be counting the length properly. Also, how can I limit the user so if they try to type a 6th character, it won't show or be accepted?
private void textBoxZip_TextChanged(object sender, EventArgs e)
{
String userInputString;
int length,
max = 5;
userInputString = textBoxZip.Text;
numberTest(userInputString);
length = userInputString.Length;
if (length > max)
{
labelErrorMessage.Text = "Maximum length 5 numbers";
}
}

I think using a MaskedTextBox would be an easy solution. This will allow you to easily accept only a certain # of characters in a specified format.
The MaskedTextBox is a standard .NET control in the control toolbar in Visual Studio.

You should look at events KeyDown and KeyPress. See example in MSDN Control.KeyPress Event

If you are in a WinForms application, you can limit a textbox's length by setting the MaxLength property.

Related

How to scroll recyclerview onItemClick in another recyclerview?

I am building a small dictioanry app, screenshot below:
Screenshot
When I click on a letter from mRecyclerView1, I want to scroll mRecyclerView2 to the the first word which starts with this particular letter.
Inside the mRecyclerView1 adapter I've made an interface which gets the value of the clicked letter.
Then inside the fragment I found a workaround with scrollToPositionWithOffset inside the onLetterClick method which looks like this:
override fun onLetterClick(letter: String) {
when (letter) {
"А" -> {
(mRecyclerView2!!.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(
0,
0
)
}
"Б" -> {
(mRecyclerView2!!.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(
140,
0
)
}
}
The problem is that I have to add all letters from the alphabet together with the positions of the first words that start with them. And if I decide to add more words later on, these positions will be shifted and will change.
Is there a smarter way to achieve this scroll effect? If not, is it possible to make a for/while loop instead of using when with all alphabet letters?
Any help will be appreciated! Thank you ^^
One way to do it is to maintain a Map<String,Int> or Map<Char, Int> (Your choice) which contains index of first word starting with given character. for example ("A" -> 0 , "B" -> 5 ) etc.
In your case you can prepare the map when you load the data in RecyclerView like following
val list = // This is list of words
val map = mutableMapOf<String, Int>()
list.forEachIndexed { index, s ->
map.putIfAbsent(s[0].toString(), index) // Handle lowercase/uppercase if required
}
Now pass this map to your adapter and when any item is clicked, you can get the position of item from map as
map[word[0].toString()]
You can use collection like this Map<String, Int>, where String is letter and Int is position, then pass position value by letter to onLetterClick(position: Int) and call scrollToPositionWithOffset(position,0).
IMHO it's better to use a prepared dictionary in your situation.

How can I get an integer out of a String with sections

I try to get an integer (right after "PLAYING:STATION\nID:"out of the String shown in my screenshot by using the following code:
int zahl = Integer.parseInt(sentence.substring(sentence.indexOf("PLAYING:STATION\n
ID:")+1).trim());
But all I get is a NumberFormatException. How can I tell the trim()-method that it has to stop right after the number?
Screenshot of the complete String
Since you must select a specific ID: entry among several ones, I think the best way to proceed is using a regular expression. Using your output sample, I wrote the following code:
String text = "PLAYING_MODE\nID:\nPLAYING:STATION\nID:2\nNAME:wazee.org";
Pattern p = Pattern.compile("PLAYING:STATION\nID:(\\d+)");
Matcher m = p.matcher(text);
if (m.find()) {
int number = Integer.parseInt(m.group(1));
System.out.println(number);
}
which correctly parses the ID number that immediately follows PLAYING_STATION.
You could as well repeatedly work with the overloaded String.indexOf() method (find PLAYING:STATION, then the following ID:, then the following \n). I think the code might be harder to read, but it would still do the job.
I hope this will be helpful...
Cheers,
Jeff

Guessing game random number to static

I have created a guessing game that generates a random number, takes the user's guess, and basically outputs whether the user's guess is too high or too low(outputs blue, colder) or closer (warmer, outputs red) using both a text box background color and label, as you can see in my code. The issue I am having difficulties with is that every time I click submit, the program generates a new random number. I would like for the program to use the same number until the user's guess is correct, then it can generate a new number if the user would like to play again.
I'm thinking about using a while loop, such as
How could I possible make the random number stay static (same) until it's guessed correctly and if I do need a while loop, where would be the ideal place to place it?
Assign the generated random number to a variable, and then use that variable until you need a new random number.
In this particular instance, the line of code number = rand() % 1000 + 1; needs to be outside of your button click method. Otherwise, every time you click the button, it will generate a new random number.
What you want to do is move your number variable's declaration to the top of the class, outside any functions, like so:
class NumberGuessingGame
{
public: int number = 0;
}
Then, in your MyForm_Load method, you can generate the random number, and set it's value to that variable, like:
private: System::Void MyForm_Load()
{
//Set the value of number here to a newly generated random integer
}
You should then be able to access the number variable inside your button1_Click function:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (input > this->number)
{
//code
}
//Rest of your ifs
}

Why CreateSolidBrush returns random number instead of constant number in c++?

Here is my code :
int clrresult = (int)CreateSolidBrush(RGB(20, 30, 40));
std::wstringstream wss;
wss << clrresult;
Edit_SetText(CLtbx,wss.str().c_str());
every time , I clicked on button , it return rand number with 10 length like this : 1341117845
but the color of the background window is the same. why this happened ?
Why CreateSolidBrush returns random number instead of constant number ?
Thanks.
According the the MSDN documentation, CreateSolidBrush returns an HBRUSH object, which is a handle to a brush, not a meaningful value. You aren't meant to use the numerical value of it directly. It's essentially a pointer to the actual brush object which is managed by Windows.

Find a specific char in an array of chars

I am doing an application where I want to find a specific char in an array of chars. In other words, I have the following char array:
char[] charArray= new char[] {'\uE001', '\uE002', '\uE003', '\uE004', '\uE005', '\uE006', '\uE007', '\uE008', '\uE009'};
At some point, I want to check if the character '\uE002' exists in the charArray. My method was to make a loop on every character in the charArray and find if it exists.
for (int z = 0 ; z < charArray; z ++) {
if (charArray[z] == myChar) {
//Do the work
}
}
Is there any other solution than making a char array and finding the character by looping every single char?
One option is to pre-sort charArray and use Arrays.binarySearch(charArray, myChar). A non-negative return value will indicate that myChar is present in charArray.
char[] charArray = new char[] {'\uE001', '\uE002', '\uE003', '\uE004', '\uE005', '\uE006', '\uE007', '\uE008', '\uE009'};
Arrays.sort(charArray); // can be omitted if you know that the values are already sorted
...
if (Arrays.binarySearch(charArray, myChar) >= 0) {
// Do the work
}
edit An alternative that avoids using the Arrays module is to put the characters into a string (at initialization time) and then use String.indexOf():
String chars = "\uE001...";
...
if (chars.indexOf(myChar) >= 0) {
// Do the work
}
This is not hugely different to what you're doing already, except that it requires less code.
If n is the size of charArray, the first solution is O(log n) whereas the second one is O(n).
You can use hash/map to check existance of the characer. This approach has better time of O(log n) or O(1) depending on hash/map inner structure.
If you don't have access to Arrays, since you are working in JavaME, then you should try to:
Or implement a sorted array and the binary search youself
Or just use a O(n) solution, wich is a good solution anyways.
Your solution is O(n), along with the one stated by aix.
You could try to use a Map, but it would depends on how much elements you are in your array. If you think there won't be more than 1000 elements in the array, just use a O(n) solution. But if you think you could have an unkown number of elements, a Map would be a reasoable choice, providing a better solution.
You can use net.rim.device.api.util.Arrays.getIndex(char[] array, char element)
It depends on what Java ME configuration / profile you are using. If you're on CDC then check for what parts of Java SE 1.3 Collections framework are supported (just find javadocs targeted for your device and look in java.util package). Another thing worth checking is whether your device has some blackberry-specific API extensions to work with collections.
If you are limited to bare minimum of CLDC/MIDP then other solution than you mentioned would be to add chars to Vector and use Vector.contains(Object)
If you don't want to implement it yourself you could use ArrayUtils in apache commons project:
ArrayUtils apache-commons

Resources