Reputation: 51
My switch
statement isn't working as whole.
I have never used switch in Java, and I dont know what I did wrong. It is also not executing default
. I looked some info up about switch
statements, and I think maybe it is because of this line:
if (pair.length == 2) {
// Voorbeeld van het gebruik van de key/value pairs
switch (pair[0]) {
because what I looked up it looked like everybody was using a variable on the pair[0]
spot.
Thanks in advance!
String scanString = result.getText(); // result.getText();
String[] parts = scanString.split("\\||");
// Loop alle delen tussen | langs
for (String part : parts) {
String[] pair = part.split("\\|"); // Bevat de key en value pair voor en na het streepje
if (pair.length == 2) {
// Voorbeeld van het gebruik van de key/value pairs
switch (pair[0]) {
case "po":
System.out.println("Productieorder: " + pair[1]);
edt2.setText(pair[1]);
break;
case "tnr":
System.out.println("Tekeningnummer: " + pair[1]);
break;
case "ref":
System.out.println("Referentie: " + pair[1]);
break;
case "hafa":
System.out.println("Half Fabrikaat: " + pair[1]);
break;
case "art":
System.out.println("Artikel: " + pair[1]);
break;
case "atl":
System.out.println("Aantal: " + pair[1]);
break;
case "loc":
System.out.println("Locatie: " + pair[1]);
edt4.setText(pair[1]);
break;
default:
System.out.println("NIET GELUKT");
}
}
}
I Will try simply this: if (pair.length > 2)
instead of == 2
, I acually don't even know why it was == 2
, because I need to scan qr string that can exist out of more than 3000 chars.
Upvotes: 0
Views: 222
Reputation: 518
Problem is here.
String[] parts = scanString.split("\\||");
It is no difference from
String[] parts = scanString.split("");
It will split every letters of the string.
For example:
"Hello".split("\\||")
Its return value is an array like
["H","e","l","l","o"]
If you want to split a string by two | , you should write:
String[] parts = scanString.split("\\|\\|")
Upvotes: 2
Reputation: 109613
You should use split("\\|")
if wanting to split by |
; split("\\|\\|")
if wanting to split by ||
.
Otherwise the second |
without regex escape \
will be an OR, and as such the string is split on the empty string too, giving an array of strings containing just one letter (though not |
).
Upvotes: 1
Reputation: 1
If you are using split function and then you need to keep in mind below points. This function does not take as it is input to split.
Upvotes: 0
Reputation: 43
Problem is in String[] parts = scanString.split("\||"); and String[] pair = part.split("\|"); which spliting string by character. and the condition if (pair.length == 2) is checking size 2 whic returns false so the control isn't entering into the switch block. You can install a breakpoint and debug it.
Upvotes: 1