I read the data.csv file in Java, for the simplicity I make an example for it:
data.csv:
HAIRCOLOR;NAME
brown;John
blonde;Nathan
brown;Emily
gray;William
blonde;Helen
I have a list that contains all the data called peopleList (except the 1st row).
I would like to get the number of each color occurrence
Example for output:
brown:2
blonde:2
gray:1
etc...
I am trying to make it with a HashMap structure, like:
Map number_of_colors <String, Integer> = new HashMap()<>;
for(int i = 0; i<peopleList.size(); i++){
number_of_colors.put(peopleList.get(i).getColor(), /*and what to write here to get the number of each color?*/)
}
Or am I on the wrong path?
I was sure this can be done with HashMap.
Thank you for all your answers!
Map number_of_colors <String, Integer> = new HashMap()<>;
for(int i = 0; i<peopleList.size(); i++){
if(number_of_colors.contains(peopleList.get(i).getColor())){
number_of_colors.put(peopleList.get(i).getColor(), number_of_colors.get(peopleList.get(i).getColor())+1);
}else{
number_of_colors.put(peopleList.get(i).getColor(), 1);
}
}
You can do like this.
Im new to Java and JavaFX and Im trying to check it there is a duplicate in a Tableview, and if that is the case I would like to replace it with a new set of data.
So in essence I'm trying to iterate through the data in my TableView and compare it to something. To be more exact I'd like to compre a value of the String on the first column to a new String. I've done some research and I've found that the most common kind of solution for Filtering Data is using a FilteredList but this doesn't return my original set of items.
my current Code looks like this:
#FXML private TableView<STable> TableV;
public void Replace(String s){
ObservableList<STable> getCurrentData;
for(int i = 0; i < getCurrentData.size(); i++){
// Here is where I get Stuck I've tried:
//TableV.getSelectionModel().getSelectedItem().getCajas();
//getCurrentData.get(i)
}
}
Note: The STable is a class that has all the setters and getters for each of the columns, I've also got the CellFactory set up.
Any guidance on how to do this would be great!
Basically you just have to iterate through your data items, and compare the value representing the content of column 1, to your new string. If both values are equal, you update the value in your dataModel:
(I replaced STable with YourData, because I find the name for a dataModel a little confusing)
for (YourData data : tableView.getItems()) {
if (data.getColumOne().equals(textToCompare)) {
data.setColumnOne("newText");
}
}
Or if you want to replace the row:
for (int idx = 0; idx < tableView.getItems().size(); idx++) {
YourData data = tableView.getItems().get(idx);
if (data.getColumnOne().equals(textToCompare)) {
tableView.getItems().set(idx, someOtherData);
return;
}
}
I have the following ASN1 data
Sequence
Sequence
ObjectIdentifier
Sequence
Sequence
Integer
Integer
Sequence
Integer
Integer
My goal is to get the encoded integer values. My code so far is the following
ByteQueue queue(inputLen);
queue.Put2(input, inputLen, 0, false);
BERSequenceDecoder outer(queue);
BERSequenceDecoder discard(outer); // unnecessary sequence with object_identifier
BERSequenceDecoder obj(discard,
CryptoPP::ASNTag::OBJECT_IDENTIFIER | CryptoPP::ASNIdFlag::UNIVERSAL);
BERSequenceDecoder parent(outer); //BER decode error
for(int i = 0; i < 2; i++) {
BERSequenceDecoder dataSequence(parent);
Integer i1, i2;
i1.BERDecode(dataSequence);
i2.BERDecode(dataSequence);
Problem is, I don't know how to properly get past the object_identifier part, at least I think that is the problem. I'm getting BER decode error on the 4. decoder object.
Also, am I initializing the ByteQueue correctly? this Put2 method doesn't seem like the correct way, but I didn't find any other methods.
ByteQueue queue(inputLen);
queue.Put2(input, inputLen, 0, false);
You could also do something like:
ArraySource as(input, inputLen, false /*pumpAll*/);
as.TransferTo(queue);
Or, if you just want to copy them:
as.CopyTo(queue);
Problem is, I don't know how to properly get past the object_identifier part...
I would probably do something like:
byte b = as.Peek();
if(b == /*some tag*/)
as.Skip(n);
Or:
byte b = as.Peek();
if(b == /*some tag*/)
{
lword length;
bool definiteLength;
if(!BERLengthDecode(as, length, definiteLength))
throw BadParam();
as.Skip(length);
}
The source files with the goodies like above is asn.h and asn.cpp. The others you might be interested in include BERDecodeOctetString and BERDecodeBitString.
I've got a JS array which is writing to a text file on the server using StreamWriter. This is the line that does it:
sw.WriteLine(Request.Form["seatsArray"]);
At the moment one line is being written out with the entire contents of the array on it. I want a new line to be written after every 5 commas. Example array:
BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,
Should output:
BN,ST,A1,303,601,
BN,ST,A2,303,621,
BN,WC,A3,303,641,
I know I could use a string replace but I only know how to make this output a new line after every comma, and not after a specified amount of commas.
How can I get this to happen?
Thanks!
Well, here's the simplest answer I can think of:
string[] bits = Request.Form["seatsArray"].Split(',');
for (int i = 0; i < bits.Length; i++)
{
sw.Write(bits[i]);
sw.Write(",");
if (i % 5 == 4)
{
sw.WriteLine();
}
}
It's not terribly elegant, but it'll get the job done, I believe.
You may want this afterwards to finish off the current line, if necessary:
if (bits[i].Length % 5 != 0)
{
sw.WriteLine();
}
I'm sure there are cleverer ways... but this is simple.
One question: are the values always three characters long? Because if so, you're basically just breaking the string up every 20 characters...
Something like:
var input = "BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,";
var splitted = input.Split(',');
var cols = 5;
var rows = splitted.Length / cols;
var arr = new string[rows, cols];
for (int row = 0; row < rows; row++)
for (int col = 0; col < cols; col++)
arr[row, col] = splitted[row * cols + col];
I will try find a more elegant solution. Properly with some functional-style over it.
Update: Just find out it is not actually what you needs. With this you get a 2D array with 3 rows and 5 columns.
This however will give you 3 lines. They do not have a ending ','. Do you want that? Do you always want to print it out? Or do you want to have access to the different lines?:
var splitted = input.Split(new [] { ','}, StringSplitOptions.RemoveEmptyEntries);
var lines = from item in splitted.Select((part, i) => new { part, i })
group item by item.i / 5 into g
select string.Join(",", g.Select(a => a.part));
Or by this rather large code. But I have often needed a "Chunk" method so it may be reusable. I do not know whether there is a build-in "Chunk" method - couldn't find it.
public static class LinqExtensions
{
public static IEnumerable<IList<T>> Chunks<T>(this IEnumerable<T> xs, int size)
{
int i = 0;
var curr = new List<T>();
foreach (var x in xs)
{
curr.Add(x);
if (++i % size == 0)
{
yield return curr;
curr = new List<T>();
}
}
}
}
Usage:
var lines = input.Split(',').Chunks(5).Select(list => string.Join(",", list));
i have an application in which i have around 100 textinputs all are numbers
i want to simplify the addition ie. any other way than saying txt1.text+txt2.text.....
that would increase my code a lot
is it possible to have (n+=txt*.text) or some thing like that
any help would be appreciated have to get the application done in two days thank you
If txt1, txt2 etc are public properties of the class representing this, you can use the following code to get the sum of the numbers in the text inputs.
var n:Number = 0;
for(i = 1; i <= total; i++)
n += Number(this["txt" + i].text);
To get a concatenated string:
var s:String = "";
for(i = 1; i <= total; i++)
s += this["txt" + i].text;
If the text inputs are properties of a different class, use the instance name of the object instead of this. For example:
instanceName["txt" + i].text;
Another solution that is more clean is to store them in an array and loop through them. But that might require changes in other parts of your code.