-1

I have a requirement in which I am reading from a config file using Java function :

My config file contain these data:

PRE|X8AR01013001010000149U
PRE|Y90BV0001100002000049U
POS|1ABCDEFRI0002000789GHJK
POS|00A0000110000200000082U

I wrote this function :

After returning this value I am comparing with another String. If it is matching -- pass the flow otherwise reject

But After if I have to return 'something' , here, i return "0".

With this even if condition is not matching , it is passing the flow.

What supposed to Pass in return

1- return st -- even if bit is 0 , it is not allowing the flow

2- return 0 - passing flow in all cases

3- return null - not allowing the flow

Here is the code :

public String sux_getSpMobilePostPaidVasValue(ExecutionContext ectx, Object[] args) throws IOException {
    final String AG_HOME = System.getProperty("AG_HOME");
    String resource = AG_HOME +
            "conf/subscriber_content_restriction.conf";

    File file = new File(resource);
    BufferedReader br = new BufferedReader(new FileReader(file));

    String st;
    while ((st = br.readLine()) != null)
        if (st.startsWith("POS")) {
            String stPre = st.substring(4, 5);
            if (stPre == "0") {
                return "0";
            }

        }
    return "0";  // When I am not returning it is giving me compile time error
}

Is there any way to achieve this ?

Kaan
  • 5,434
  • 3
  • 19
  • 41
Ritika Saxena
  • 43
  • 1
  • 7
  • Your curly braces don't seems balanced. – Yoshikage Kira Oct 07 '19 at 16:35
  • 1
    See: [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jacob G. Oct 07 '19 at 16:36
  • 2
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – almac777 Oct 07 '19 at 16:36
  • First of all compare strings like `stPre.equals("0")` and not with == – fzn Oct 07 '19 at 16:37
  • What you return for an invalid condition is completely up to you. – OldProgrammer Oct 07 '19 at 16:37
  • There is no single answer to "what should I return". All the options you've suggested are valid. You may also consider throwing a custom exception if whatever your method was looking for couldn't be found. And handle the exception from where your function is called. – fzn Oct 07 '19 at 16:40
  • Is it mandatory to return After if block ? I try these ways but nothing is working properly 1- return st -- even if bit is 0 , it is not allowing the flow 2- return 0 - passing flow in all cases 3- return null - not allowing the flow – Ritika Saxena Oct 07 '19 at 16:41

3 Answers3

1

// When I am not returning it is giving me compile-time error

Compile-time error is because your method definition expects a string type return value. So you would have to return something.

I do not know what your method is being used for, but it looks like it's always going to return either 0 if some bit is found in the config file. So why not make the function return a boolean value instead?

public boolean sux_getSpMobilePostPaidVasValue(ExecutionContext ectx, Object[] args)
throws IOException {
    final String AG_HOME = System.getProperty("AG_HOME");
    String resource = AG_HOME + "conf/subscriber_content_restriction.conf";

    File file = new File(resource);
    BufferedReader br = new BufferedReader(new FileReader(file));

    String st;
    while ((st = br.readLine()) != null) {
        if (st.startsWith("POS")) {
            String stPre = st.substring(4, 5);
            if (stPre.equals("0")) {
                return true;
            }
        }
    }
    return false;
}

And for some reason your function really needs to send a string, you can use a custom exception.

import com.myproject.utils.exceptions.InvalidConfigError;

public String sux_getSpMobilePostPaidVasValue(ExecutionContext ectx, Object[] args)
throws IOException {
    final String AG_HOME = System.getProperty("AG_HOME");
    String resource = AG_HOME + "conf/subscriber_content_restriction.conf";

    File file = new File(resource);
    BufferedReader br = new BufferedReader(new FileReader(file));

    String st;
    while ((st = br.readLine()) != null) {
        if (st.startsWith("POS")) {
            String stPre = st.substring(4, 5);
            if (stPre.equals("0")) {
                return "0";
            }
        }
    }
    throw new InvalidConfigError("Does not contain 0.");
}

And you may define a custom exception in a separate .java file like this.

package com.myproject.utils.exceptions;

public class InvalidConfigError extends Exception {
    public InvalidConfigError(String message) {
        super(message);
    }
}
fzn
  • 514
  • 3
  • 10
0

stPre == "0" is wrong. It should be: stPre.equals("0"). Compare strings with the String#equals() method.

public String sux_getSpMobilePostPaidVasValue(ExecutionContext ectx, Object[] args) throws IOException {
    final String AG_HOME = System.getProperty("AG_HOME");
    String resource = AG_HOME
            + "conf/subscriber_content_restriction.conf";

    File file = new File(resource);
    String stPre = "";
    // Try With Resourses to auto-close reader.
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String st;
        while ((st = br.readLine()) != null) {
            // Skip blank lines (if any).
            if (st.trim().equals("")) {
                continue;
            }

            if (st.startsWith("POS")) {
                stPre = st.substring(4, 5);
                if (stPre.equals("0")) {
                    break;
                }
            }
        }
    }
    return stPre; 
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
0

I really don't understand everything I'd like to about your problem. But @Goion is probably the answer you want. You have not put an open bracket "{" after the while. Thus the next to last bracket closes the method and the last one in your sample would actually end the class.

On a side note this idea of passing special nonsensical values to indicate an error is a bad practice. You should utilize the Java Exception features.

You could create your own Exception class and throw that which is probably the right-est answer. Personally I'd probably throw an InvalidArgumentException. You can even specify the cause in the constructor.

I would NOT return a string of zero if the method fails.

Terry
  • 911
  • 10
  • 26