0

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.

8
  • 2
    Define what you mean by not working. The wrong case is taken? Multiple? None of them? If you think its the if, have you done basic debugging like stepping through in a debugger to see if that's the case? Commented Aug 24, 2018 at 6:46
  • 1
    What is the value of pair.length at that point? ` Commented Aug 24, 2018 at 6:46
  • 1
    @MaartenVaartjes I didn't say system logs. Do you know what a debugger and a breakpoint is? If not, you need to learn what they are and how to use them. Commented Aug 24, 2018 at 6:50
  • 2
    Your switch statement works fine in IdeOne: ideone.com/7Cxxm7 . It's probably the value of pair.length. Step through it with the debugger, or print its value in the log ( log.i, see here ). Then you'll know for certain. Commented Aug 24, 2018 at 6:50
  • 1
    Hm... using a debugger is an essential part of programming. If you didn't understand the guide, you'll have to find another guide that explains it better. And, of course, experiment - keep trying things until you understand how the thing works. You'll get there! Commented Aug 24, 2018 at 7:01

4 Answers 4

2

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("\\|\\|")
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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 |).

1 Comment

bedankt joop ! .
0

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

3 Comments

What ? , " \\ " is needed to use | , to avoid complications , i have been told
why dont you write your own split function for this. Use CharAt function to check the char which you want from split.
i did that in my old function , but we need to be able to recycle the split with every aspect of what could be in the qr , so this is the recommended way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.