Maarten Vaartjes
Maarten Vaartjes

Reputation: 51

Java switch statement not working, even 'default' isn't

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");
        }
    }
}

Edit

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

Answers (4)

Alhaitham
Alhaitham

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

Joop Eggen
Joop Eggen

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

Vasanth Manne
Vasanth Manne

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.

  1. one-char String and this character is not one of the RegEx's meta characters ".$|()[{^?*+\"
    1. two-char String and the first char is the backslash and the second is not the ascii digit or ascii letter

Upvotes: 0

Bhumi Nandan Chettri
Bhumi Nandan Chettri

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

Related Questions