Using a MaskFormatter to restrict a JFormattedTextField to 2 OR 3 integers? - maskedtextbox

I am currently using a MaskFormatter to limit my JFormattedTextFields to two integers like so:
textField = new JFormattedTextField(createFormatter("##"));
but what should I do if I want the text field to accept two OR three integers
this is how I'm thinking of it logically in my head: [ ("##") || ("###") ]

Related

how to use while loop in pseudocode

I am trying to add the user inputs using a while loop and If statements. I am having trouble figuring out how to add all the userNumbers to each other. Any help would be appreciated.
//variables
Declare Integer userIn = 0
Declare Integer total = 0
//read numbers and calculate
While decision == decY
Display “Please enter your numbers: ”
Input decision
If UserIn > 0
Display userNumbers
Set total = userIn + 1
Display “Would you like to enter another number Y/N?”
Input decision
If decision == decN
Display “Done reading numbers, your total is ”, total
End If
End If
End While
Decide on a separator for the input, unless they're only allowed to enter a single number at a time in which case you can skip to 3.
Use string splitting to cut the input up and then loop through that list with for, while, do, until, or etc.
Create a sum total variable and add each input value to it, e.g. sum = sum + split_input[index], or if it will only allow a single input at a time sum = sum + input.
Some Notes:
Adding a value to a variable can be shortened to variable += value to add a value to an existing variable and assign the result to the variable, but not all languages support this syntax.
Not all programming languages start at 0 for list indices, so be sure to change the starting index accordingly.

Creating a nested array with unspecified number of columns

I'm trying to create a nested array that takes the user's input and stores it in the sub arrays. The number of subarrays is given by the user. I need the columns to be unspecified, and any number of elements can go in.
Is there a way to achieve this?
I tried:
puts "How many groups do you want to create?"
number_of_teams = gets.chomp
team_array = [Array.new(number_of_teams.to_i){Array.new()}]
team_array = Array.new(number_of_teams.to_i) { [] }
So if input is 3 then team_array will have [ [], [], [] ]
This is true for any Array. Ruby doesn't have the concept of an Array of a fixed number of elements. You don't even need to ask the user for the number of groups.

How to check if a string has a more than n repetitive patterns in GO?

I want to check if a string contains repetitive patterns above a threshold .
For example, these two strings both exceed a threshold of 2:
"xyzxyzxyz" // contains "xyz" 3 times in succession
"abxyxyxyns" // contains "xy" 3 times in succession
Does anyone know how this is possible?
Use the "repetitions" modifier.
re := regexp.MustCompile(`(xy){3,}`) // match "xy" 3 or more times
fmt.Println(re.MatchString("abxyxyns")) // false
fmt.Println(re.MatchString("abxyxyxyns")) // true
The available options for the regpexp package's RE2 implementation are documented here:
https://github.com/google/re2/wiki/Syntax

write cell array into text file as two column data

I have two different variables which are stored as cell arrays. I try to open text file and store these variables as two column arrays. Below is my code, i used \t to seperate x and y data, but in the output file, the x data is written first which is followed by the y data. How can I obtain two column array in the text file?
for j=1:size(data1,2)
file1=['dir\' file(j,1).name];
f1{j}=fopen(file1,'a+')
fprintf(f1{j},'%7.3f\t%20.10f\n',x{1,j}',y{1,j});
fclose(f1{j});
end
Thanks in advance!
You can use dlmwrite as well to accomplish this for numeric data:
x = [1;2;3]; y = [4;5;6]; % two column vectors
dlmwrite('foo.dat',{x,y},'Delimiter','\t')
This produces the output:
1 4
2 5
3 6
Use a MATLAB table if you have R2013b or beyond:
data1 = {'a','b','c'}'
data2 = {1, 2, 3}'
t = table(data1, data2)
writetable(t, 'data.csv')
More info here.

matching array items in rails

I have two arrays and I want to see the total number of matches, between the arrays individual items that their are.
For example arrays with:
1 -- House, Dog, Cat, Car
2 -- Cat, Book, Box, Car
Would return 2.
Any ideas? Thanks!
EDIT/
Basically I have two forms (for two different types of users) that uses nested attributes to store the number of skills they have. I can print out the skills via
current_user.skills.each do |skill| skill.name
other_user.skills.each do |skill| skill.name
When I print out the array, I get: #<Skill:0x1037e4948>#<Skill:0x1037e2800>#<Skill:0x1037e21e8>#<Skill:0x1037e1090>#<Skill:0x1037e0848>
So, yes, I want to compare the two users skills and return the number that match. Thanks for your help.
This works:
a = %w{house dog cat car}
b = %w{cat book box car}
(a & b).size
Documentation: http://www.ruby-doc.org/core/classes/Array.html#M000274
To convert classes to an array using the name, try something like:
class X
def name
"name"
end
end
a = [X.new]
b = [X.new]
(a.map{|x| x.name} & b.map{|x| x.name}).size
In your example, a is current_user.skills and b is other_users.skills. x is simply a reference to the current index of the array as the map action loops through the array. The action is documented in the link I provided.

Resources