Reputation: 175
I have a semicolon separated text file. The idea is to read the text file line by line. Every line will be splitted to an array element.
Now I want to do some checks like is the ID (first element called "Referenz") unique, are all mandatory "fields" filled, etc...
I guess I have to take the ID and put it to an list. And for the next line I have to compare the ID with the IDs from the list?
So question is that the right way and what / how to realise that.
Here is my code so far:
public class Test_Line2Array {
public static void main(String[] args) {
String strLine = "Referenz;field2;field3;field4;field5;field6;field7;Titel;Name1;Name2;Name3;field8;field9;field10;field11;field12;field13;field14;Street;field15;ZIP;field16;city;field17;dob;field18;field19;field20;field21;field22;field23;field24;field25;field26;field27;field28;field29;field30;field31;field32;field33;field34;field35;field36;field37;field38;field39;phone;mobile;CustomField1;CustomField2;CustomField3;CustomField4;CustomField5;CustomField6;CustomField7;CustomField8;CustomField9;CustomField10";
//declaration
String[] stringArray;
String delimiter = ";";
// allocates memory for 59 strings
stringArray = new String[59];
// split the String after separator ";"
stringArray = strLine.split(";", -1);
// print array
for(int j = 0; j < stringArray.length; j++) {
System.out.println(j + " " + stringArray[j]);
}
}
Upvotes: 1
Views: 99
Reputation: 44368
I recommend you to split the string with delimiter ;
and add separated Strings to a List
, where you can easily validate with the Collections.frequency()
static method returning the number as int
of the occurence.
String[] values = strLine.split(";");
List<String> list = Arrays.asList(values);
if (Collections.frequency(list, list.get(0) > 1) {
System.out.println("The first value is not unique in the list");
}
Since Java 8 feel free to use Stream:
if (list.stream().filter(a -> a.equals(list.get(0))).count() > 1) {
System.out.println("The first value is not unique in the list");
}
Upvotes: 1
Reputation: 40298
// allocates memory for 59 strings
stringArray = new String[59];
// split the String after separator ";"
stringArray = strLine.split(";", -1);
Initializing the String[59] isn't helping you; the split method is just returning something that overwrites it immediately afterwards.
If you needed to check for any duplicates, using a HashSet would help here.
If you only need to make sure the first element isn't duplicated, you can just do it in a loop. You've already got one, so...
// print array
for(int j = 0; j < stringArray.length; j++) {
if (stringArray[0].equals(stringArray(j)) {
System.out.println("Duplicate!");
}
System.out.println(j + " " + stringArray[j]);
}
}
Upvotes: 1
Reputation: 4014
For each line, put its Referenz
in a HashSet
. Then checking if a subsequent Referenz
is unique would be as simple as referenzSet.contains(theNewReferenz)
Upvotes: 0
Reputation: 29680
To check if the first element is unique, you can use the following:
Collections.frequency(Arrays.asList(stringArray), stringArray[0]) == 1
This returns a boolean
that is true
if the first element of stringArray
is unique, otherwise false
.
Upvotes: 0