0

The string postcode at the end is used as an input later on in my code and as you can see, it's hard-coded. What I want to do is make it more dynamic. So boolean pc4 is set to the countries that accept 4-digit postcodes. pc5 is 5-digit and pc6 6-digits.

What I want to do is something like:

if(pc4==true){String postcode="1234"}

As you guys already know, this doesn't work outside the if-statement.

So my question is: can I update a string outside an if-statement or is there more efficient way of getting to where I need to be?

String state1 = "state";

 boolean pc4 = (bString.equals("Bahrain") || bString.equals("Afghanistan") 
             || bString.equals("Albania") || bString.equals("Armenia") 
             || bString.contains("Australia")
             ... 
             || bString.equals("Tunisia") ||bString.equals("Venezuela") );
 boolean pc5 = (bString.equals("Alan Islands") || bString.equals("Algeria")
             || bString.equals("American Samoa") || bString.equals("Wallis and Futuna") 
             ...
             || bString.equals("Zambia"));
 boolean pc6 = (bString.equals("Belarus") || bString.equals("China")
             || bString.equals("Colombia") || bString.equals("India")
             ...
             || bString.equals("Turkmenistan") || bString.equals("Viet Nam"));

 String postcode = "123456";
2
  • This is clearly an abuse of the inline if statement. Commented Aug 30, 2012 at 14:22
  • 1
    I don't think it's necessary to downvote someone for having poor code when they're posting a question on how to improve their code. Commented Aug 30, 2012 at 14:30

4 Answers 4

6

Define postcode outside the block and assign value to it based on condition.

Something like below:

  String postcode="";


 if(pc4){postcode="1234"}
Sign up to request clarification or add additional context in comments.

5 Comments

@ThomSmith: Agree. Updated answer.
this is the code i've just used which hasn't worked: String postcode = ""; if(pc4){postcode="1234";} else if (pc5){postcode="12345";} else if (pc6){postcode="123456";}
I println postcode and it gives me blank. bString equals china so pc6 should be true. Maybe I should try bString.contains("China")
Are you sure you are exactly passing China? no case mises? No need of contains.
It worked when I used contains but its not working for Australia even with a contains, strange, thanks Nambari - your answer solved my issue
2

While tangential to your original question, rather than having gigantic one-line if statements, it might be easier to use a Map to define the length of your postal codes.

HashMap<String, Integer> countries = new HashMap<>();
countries.add("Bahrain", 4);

switch(countries.get(myCountry)) {
  case 4:
    // Stuff!
}

6 Comments

Recent Java will box ints automatically, so you wouldn't need new Integer().
Also, I agree that a simple map is probably the best way to do this.
+1, as it's a lot more helpful to provide means of fixing this pile of || trees.
@ThomSmith Thank you, I have changed my answer to make it simpler.
I dont know how to HashMap yet but it's onmy list of things to do. Thank you, I will deffinately use this
|
1

Use the ternary conditional operator.

String postcode = pc4 ? "1234" : "";

Comments

0
String postcode = null; // or ""
if (pc4)
  postcode = "1234";
else if (pc5)
  postcode = "12345";
else if (pc6)
  postcode = "123456";
else
  postcode = "default value";

Comments

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.