How to code complex conditions

Hello world

First of all, please forgive if I don't use the programming terminology correctly, I'm a beginner with no computer training.

My question is a general: what is the right approach for complicated code conditions?

I start with a good example that is actually something concrete I should do in my little application:

TODO: shorten/shorten a complete person by name to fit a character 16
For example: Johnathan-> J necromancer necromancer

There are many possibilities how to make based on the length of the first, model and other names and there is an order of preference for these solutions.
So if possible shorten only the name as above; If it is too long and then leave some space and so on. These would be the preferences demonstrated by examples:
1 J L Smith
2 JL Smith
3 JLSmith
4 John Languely S
5 John Lang B S
6 John L S
7 JohnLS
8 J L S
9 JLS

How to encode? With the nested If if if-else blocks or in some other way?

First of all I did with the nested If - s, but it was sort of a mess after some time, so the easiest way for me was to do the following:
(Just 'calculate' names abbreviated in order of preference, and return if it fits in the 16 characters.)
if (displayName.length() > 16) {

               String[] dn = displayName.split("\\s+");
               String lName = dn[dn.length - 1];
               StringBuffer dName16 = new StringBuffer();
               
               // J L Smith
               dName16 = new StringBuffer();
               for (int i = 0; i < dn.length - 1; i++) {
                    dName16.append(dn.charAt(0) + " ");
               }
               dName16.append(lName);
               if (dName16.length() <= 16){
                    return new String(dName16);
               }
               
               // JL Smith
               dName16 = new StringBuffer();
               for (int i = 0; i < dn.length - 1; i++) {
                    dName16.append(dn[i].charAt(0));
               }
               dName16.append(" " + lName);
               if (dName16.length() <= 16){
                    return new String(dName16);
               }
               
               // JLSmith
               dName16 = new StringBuffer();
               for (int i = 0; i < dn.length - 1; i++) {
                    dName16.append(dn[i].charAt(0));
               }
               dName16.append(lName);
               if (dName16.length() <= 16){
                    return new String(dName16);
               }

//more code..
               
I would like to know how to approach these kind of situations effectively and clearly in the least error prone way. How to code complex conditions? Is there any book or theory about it?
I am not sure it is clear what I am asking, but I will follow up with more description on what I mean later. But please comment, ask, advice anything in the meantime - I would really appreciate any help with this.

Thank you in advance!
lemonboston                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

Hello lemonboston,

If you compare that with my proposal there are only a few more steps to get there.
The iteration on your list looks almost exactly like mine...

Right now, your convertDisplayNameToFitIn method has two responsibilities: building the list and selecting the abbreviation. You will have to hunt in the methods that they own.

Beside that, you need to declare types interface references where possible. I got this abbreviatedList thing should be reported as list rather than ArrayList:

List abbreviatedList = new ArrayList();

Good bye
DPT

Tags: Java

Similar Questions

  • How to code a batch file to put in the form of z: drive?

    I would like to drive Z: quick format and xcopy in D: drive in the Z: drive for backup.

    I would like to pass on any message entry also.

    Anyone have any suggestions on how to code a batch file for this?

    Thanks for all the suggestions

    Hi oem7110,

    Your question of Windows is more complex than what is generally answered in the Microsoft Answers forums. It is best suited for on TechNet. Please post your question in the TechNet forum. Link: http://social.technet.microsoft.com/Forums/en-US/ITCG/threads

    With regard to:

    Samhrutha G S - Microsoft technical support.

    Visit our Microsoft answers feedback Forum and let us know what you think.

  • SWIFT playgrounds, how to Code 2, build a loop, I can not find isOnGem

    Swift playgrounds, how to Code 2, build a loop, I can not all isOnGem. What Miss me? If I am not mistaken, is it a bug? How should I declare?

    I can't figure out how to solve the puzzle without this.

    isOnGem is not available as an option in the box to scroll so just type in from the table instead. If you enter exactly the same case, it will be recognized as legal code when you run your program.

  • How to define the condition in the analytic function

    Oracle 10g version

    Hi all

    I have the following data samples:

    Examples of data

    WITH DATA AS

    (

    SELECT 100 case_id, next_date, to_date('01-feb-2015','dd-mon-yyyy') UNION double, ALL crt_date to_date('01-jan-2015','dd-mon-yyyy')

    SELECT 100 case_id, next_date, to_date('01-mar-2015','dd-mon-yyyy') UNION double, ALL crt_date to_date('01-feb-2015','dd-mon-yyyy')

    SELECT 100 case_id, next_date, to_date('01-apr-2015','dd-mon-yyyy') UNION double, ALL crt_date to_date('01-may-2015','dd-mon-yyyy')

    SELECT 100 case_id, to_date('01-jun-2015','dd-mon-yyyy') next_date, to_date('01-apr-2015','dd-mon-yyyy') crt_date FROM dual

    )

    SELECT wagneur, MIN (next_date) OVER (PARTITION BY case_id) min_dt_analytical

    ,(

    SELECT MIN (next_date) DATA dd

    WHERE dd.case_id = d.case_id

    AND dd.next_date > crt_date

    ) min_dt_sub_query

    DATA d

    ;

    My question is that I get min_dt_sub_query using sub query but I want to use the analytical instead of query sub function so I created min_dt_analytical column, but I do not have how to give the condition that is AND dd.next_date > crt_date analytical so that I can get the same result as min_dt_sub_query data accordingly. Thanks in advance

    Concerning

    MIT

    Do not know if I understood your needs... but... something like that?

    WITH DATA AS (SELECT 100 case_id, next_date, to_date('01-feb-2015','dd-mon-yyyy') UNION double, ALL crt_date to_date('01-jan-2015','dd-mon-yyyy')

    SELECT the 100 case_id, next_date, to_date('01-mar-2015','dd-mon-yyyy') UNION double, ALL crt_date to_date('01-feb-2015','dd-mon-yyyy')

    SELECT the 100 case_id, next_date, to_date('01-apr-2015','dd-mon-yyyy') UNION double, ALL crt_date to_date('01-may-2015','dd-mon-yyyy')

    SELECT 100 case_id, next_date, to_date('01-apr-2015','dd-mon-yyyy') double crt_date to_date('01-jun-2015','dd-mon-yyyy'))

    SELECT d.

    MIN (next_date) OVER (PARTITION BY case_id) min_dt_analytical

    MIN(CASE WHEN next_date > crt_date THEN next_date ELSE NULL END) OVER (PARTITION BY case_id) AS min_dt_sub_query2

    DATA d;

    HTH

  • How to code a default widget - FORMS in Adobe Muse

    How the code of the widget by default FORMS in Adobe Muse? Please write the code of the widget.

    Thank you!

    Hi Tony,.

    Widgets by default, including the form, are native widgets of Muse which have different features than MuCOWs. Currently, it is not possible to create something that mimics the behaviour of widget native Muse, just what is shown in the MuCOW feature.

    Hope that clarifies things,

    Andrew

  • How to code a button to play and pause sound using its loading?

    How to code a button to play and pause sound using its loading? I used the sound load methos to fix the sound. Can someone help me?

    I can't determine where the sound is defined and where you try to control it.  This will work but is sloppy coding:

    Stop();

    adaDrut1.onRelease = function () {}

    _root.mySoundC = new Sound (this);

    _root.mySoundC.loadSound ("F #/ Bhajni1.ark", true);

    _root.mySoundC.onSoundComplete = function() {}

    This.Start (); }

    gotoAndPlay ("play2");

    _Global.myTaalLaya = 'entries Low1. "

    }

    {play_btn.onRelease = Function ()}

    _root.mySoundC.start ();

    }

    {pause_btn.onRelease = Function ()}

    _root.mySoundC.stop ();

    }

  • How to show/hide conditional text on the text selected with the FDK API

    Hello!

    Do you know how to show/hide conditional text in a selected text using the FDK API? I managed to do it on the whole document, but I need to do on specific selected text...

    Thanks in advance,

    Stefano

    Hi Stefano,

    This is not possible, the conditions are the properties of the document and cannot be changed from visible to hidden or v.v. for the full document.

    If you really need a more detailed method, you must create a separate condition for the selected text.

    -Michael

  • How to create a condition on a "news of the calculated items" of a PivotTable

    Hello

    Would you know how to set a condition on a new calculated item in a pivot table dynamic.
    Example to show what < 0 in red for the new calculated item 'Différence' obtained under $1-$2
    The link shows an example of an existing report, but I don't understand how this has been achieved.

    http://a.imageshack.us/img838/9851/conditioncolour.jpg

    Thank you

    Vincent,

    Yes, I know what you're saying. But please look over the page clearly.

    I said the conditional format is for revenue, Adr column, room, this three columns are numeric columns. So if we value conditional formatting for these columns, it will use the same State of PivotTable

  • How this code deletes the record from the database company?

    Hello, I'm at the beginner level and I would like to learn more about this code of a book. I have several questions below, I hope I can get any help
    Thank you

    · How this code deletes the record from the database company?
    · Where is the connection to the db?

    · What part or command is that deletes the record?
    · What does that mean?
    < link rel = "stylesheet" href = "styles.css" >

    < html >
    < head >
    < /title > < title > test code
    < link rel = "stylesheet" href = "styles.css" >
    < / head >

    < body >

    < h1 > delete a record from the company < / h1 >

    < table >
    < action = "CompanyDeleteForm.cfm cfform" method = "POST" > "
    < b >
    < td > < table > C_ID
    < td >
    < cfinput type = "Text".
    name = "C_ID".
    message = 'Please enter the C_ID '.
    validate = "integer".
    required = "Yes".
    size = "22".
    MaxLength = "20" >
    < table >
    < /tr >
    < b >
    < td > < table >
    < td >
    < input type = "submit" value = "Delete a rec of the company" >
    < table >
    < /tr >
    < / cfform >
    < /table >

    < / body >
    < / html >
    -----------------------------------------------

    This isn't. He sends the value of C_ID in a form variable to an action (CompanyDeleteForm.cfm) page that would delete code.

    Phil

  • How this code would work?

    Hello

    I have this code in a form and it works correctly, I do not know how? because I tried after you modify, delete the quotation marks and parentheses, it does not.

    SET_BLOCK_PROPERTY (NVL ' of the 'TTO_TICKETTRAVELORDERDETAILS, DEFAULT_WHERE,' (STATUS, "$$ # & & & &") = NVL ("' |: CONTROL.)) STATUS | " (', NVL (STATUS, "$$ # & & & &"))

    AND IS_TOUR = "N"

    AND EXISTS (SELECT "X"

    OF TTO_ORDERDETAILSROUTES T

    WHERE T.ORDERDETAILID = TTO_TICKETTRAVELORDERDETAILS. ORDERDETAILID

    AND NVL(CARRIER,''$$##'') = NVL ("' |: CONTROL.) CARRIER: "', NVL(CARRIER,''$$##''))

    AND NVL (CLASS, "$$ & &") = NVL ("' |: CONTROL.) CLASS | " ((', NVL (CLASS, "$$ & &")))

    AND EXISTS (SELECT "X"

    OF TTO_TICKETTRAVELORDERHEADER H

    WHERE H.ORDERID = TTO_TICKETTRAVELORDERDETAILS. ORDERID

    AND STATUS = "Post")

    AND EXISTS (SELECT "X"

    OF TTO_TICKETTRAVELORDERHEADER H

    WHERE H.ORDERID = TTO_TICKETTRAVELORDERDETAILS. ORDERID

    AND TO_CHAR(ORDERDATE,''DD-MM-YYYY'') BETWEEN NVL ("' |: CONTROL.) FROM_DATE | " (', ORDERDATE) AND NVL ("' |: CONTROL.) TO_DATE | " (', ORDERDATE)

    AND NVL(H.CUSTOMER_NO,''$$##@@@'') = NVL ("' |: CONTROL.) CUSTOMER_NO: "', NVL(H.CUSTOMER_NO,''$$##@@@'')))');

    After editing it and try in the pl/sql developer, he could not identify the TTO_TICKETTRAVELORDERDETAILS. ORDERID

    and TTO_TICKETTRAVELORDERDETAILS. ORDERDETAILID

    in the condition "Exists".

    SELECT

    t.*,

    t.ROWID

    Of

    TTO_TICKETTRAVELORDERDETAILS t

    WHERE

    NVL (STATUS,' $$ # & & & &') = NVL (", NVL (STATUS,' $$ # & & & &'))

    AND IS_TOUR = N

    AND THERE ARE

    (

    SELECT

    « X »

    Of

    TTO_ORDERDETAILSROUTES T

    WHERE

    T.ORDERDETAILID = TTO_TICKETTRAVELORDERDETAILS. ORDERID

    AND NVL(CARRIER,'$$##') = NVL (", NVL(CARRIER,'$$##'))

    AND NVL (CLASS,' $$ & &') = NVL (", NVL (CLASS,' $$ & &'))

    )

    AND THERE ARE

    (

    SELECT

    « X »

    Of

    TTO_TICKETTRAVELORDERHEADER H

    WHERE

    H.ORDERID = TTO_TICKETTRAVELORDERDETAILS. ORDERID

    AND STATE = 'Post'

    )

    AND THERE ARE

    (

    SELECT

    « X »

    Of

    TTO_TICKETTRAVELORDERHEADER H

    WHERE

    H.ORDERID = T.TTO_TICKETTRAVELORDERDETAILS. ORDERID

    AND ORDERDATE BETWEEN NVL('',ORDERDATE) AND NVL('',ORDERDATE)

    AND NVL(H.CUSTOMER_NO,'$$##@@@') = NVL (", NVL(H.CUSTOMER_NO,'$$##@@@')));

    Why?

    If you do not give the table in the outer query an alias then you reference with the full name.

    3. ??

  • How to set up conditional mapping

    Hello

    I wonder if the VBscript code is the only way to create a conditional mapping...

    We need to create a large number of condition like this:

    If Custom4 = YYY and Soruce account = XXX and then target account = Alfa to other target = Beta account

    Anyone of you experienced a way of more 'user-friendly' to work with such conditions?

    As the mapping change very often, one of the requirements that we are trying to achieve is to leave the possibility to change the mapping to functional users. But they do not know what that VBscript and they would feel more comfortable with simple tables.

    Help, please...

    Is it always the combination of Source Custom4 and account Source that determines the account target? If so, you could merge these two fields in the account of the Source on the ground through your import Format and perform one of the following two methods of mapping.

    For example, your import Format would give something like:

    Source account = XXX_YYY
    (underscore makes it easier to visually separate the account of origin of the C4, but you could eliminate it)

    * 1. Explicit mapping on all the potential account + Custom4 combinations *.

    For example, you have the following explicit maps:

    XXX_YYY--> Alfa
    XXX_YYA--> Beta
    XXX_YYB--> Beta
    XXX_YYC--> Beta
    ... etc.

    This method is extremely effective and would require no script, however, may not require a mapping table explicit very grand. If there are too many to keep them in an explicit mapping table combinations, see the following alternative method.

    * 2. A combination of Explicit and as (and/or between and/or in) cards *.

    This takes advantage of the processing order of the Types of cards, which is explicit, followed between, followed by, as in last. In this case, the explicit plan "replaces" the If part of your original conditional statement, while the plan as "replaces" the Else clause:

    Explicit card: XXX_YYY--> Alfa
    Similar card: account rule definition = XXX *, target account = Beta

    Notes *.

    Implementation or the other of these changes would naturally influence your existing cards in a meaningful way and it may be the case that is neither the optimal method. If these suggestions do not work for you, there are still other ways to accomplish what you're trying to do. Focus on combining or dividing or generally manipulate the fields from the source with Formats of import and import Scripts and examine the various Types of mapping or combinations of mapping Types to reduce the complexity of script and the effort of maintenance of your users. In the end, you will find the combination of manipulation of data that requires the least amount of scripts while reaching a decent or better performance.

  • How to use if condition to determine the passage of sequence not

    I have a sequence where I need determine his pass fail condition by comparing the two values. The values are returned by different screws LabVIEW, say. Locals.ValueReturnedByVI_1 and a value other than Locals.ValueReturnedByVI_2. Now, I intend to compare these values using an If-Else Condition and determine if this sequence has passed or failed.

    If (Locals.ValueReturnedByVI_1 is Locals.ValueReturnedByVI_2)

    / * This sequence has failed * /.

    on the other

    / * This sequence passed * /.

    End

    I don't know what needs to be done inside the blocks conditon said English of the sequence.

    Or is there a better way to do it?

    Another option would be to use a Test pass/fail without adapter step. The value of the Expression of Locals.ValueReturnedByVI_1 Data Source is Locals.ValueReturnedByVI_2.

    The biggest difference I see, this is how the result will appear in a report. I'll keep that in mind as you choose how the issue.

  • How to access a conditional expression IF-step via API?

    If we have a flow control stage IF so we can access its conditional expression via:

    "" 'RunState.SequenceFile.Data.Seq ["MainSequence']." Hand ['IF']. ConditionExpr

    How to access this term if we do not know the type of step beforehand? I create a sort of auditor of static expressions. I have access to a step through

    RunState.InitialSelection.SelectedFile.Data.Seq [Locals.seqIndex]. GetStep (Locals.stepIndex, Locals.groupIndex)

    What if

    RunState.InitialSelection.SelectedFile.Data.Seq [Locals.seqIndex]. GetStep (Locals.stepIndex, Locals.groupIndex). StepType.Name == "NI_Flow_If".

    then I want to use the Engine.CheckExpression method to the conditional expression IF-step

    Hey maksya,

    Attached is a file of sequence with a flow control if statement that checks to see if the step is a 'NI_Flow_If' using:

    "" 'RunState.SequenceFile.Data.Seq ["MainSequence']." GetStep (0, StepGroup_Main). StepType.Name == "NI_Flow_If".

    If this is the case, it will then call check expression on the current ConditionExpr:

    RunState.Engine.CheckExpression (ThisContext, RunState.SequenceFile.Data.Seq ["MainSequence"]. GetStep(0,StepGroup_Main). ConditionExpr, 0, Locals.errorString, NULL, null)

    You will notice that this expression is not valid, and that's because "ConditionExpr" is not valid for all types of step.  However, because this step will not work UNLESS the stage is a NI_Flow_If, the expression will be valid at run time.

    Also is that I changed to using InitialSelection.SelectedFile to SequenceFile just to facilitate the testing of the installation.  However, it will be the same for your SelectedFile.

  • How to avoid race conditions?

    Hi, ive done Labview for a dozen years. And this is the first time ive met or its made my code. I have a lot of code that runs at the same time. And for years, good practice in order to avoid it never repeated?

    Stu

    I spent an entire course on the race conditions and concurency in higher education, and to avoid race conditions, understand why they occur.

    Race conditions occur when several parallel processes or competitor access to a shared resource.  Furthurmore, this is especially annoying when 2 or more threads or processes are seen change a resource.  Then all first identify candidates for this when the architecture of your application.  If you only have one thread, you must be sure (although it gets more blurry since it has the ability to run in parallel).  Is it then all variables/resources that are needed in the 2 wires at the same time, and if so, you change the value in one or more threads.  If so, you have to deal with synchronization.

    Traditionally, you talk semaphors, locks and mutexes.  All methods of locking critical code (ie the resources that are sources of possible race condition).

    BUT WE ARE IN LABVIEW.  LabVIEW has a simple method which could be easier to use, functional overall.

    screws that are not marked for reentrancy is essentially a great way for the protection of data and your locking critacal sections.  That only one thread can access and or modify the data stored in the vi, at the same time.  Other threads will be blocked and wait on the resource.

    It was much more difficult to implement in c.

    Anyway the way you deal with racing conditions is to

    1. identify critical sections and resources which can cause race conditions (usually shared resources with multiple writers)

    2 lock the resource until cooked (avoid deadlocks by per se - but that is another debate)

    Unlike traditional bugs, bugs competitor are more random and very difficult to reproduce/test for so you will have to deal with them in the design of architecture.

  • How can code 643 Windows update has encountered an unknown error on Vista, I fix

    When I went to windows update, everything went well except that I got a message, Code 643 Windows update encountered an unknown error. This happened on Vista Home premium. I still shows that an update is ready to be installed, but whenever I try, I get the same message, how can I solve this problem?

    Hello

    1. don't you make changes to your system?

    Method 1: Look for the update history.

    (a) click Start, click Control Panel.

    (b) click System and security, click Windows update.

    (c) click view update history.

    (d) check the number of KB.

    Method 2:

    Try to reset the windows update components.

    To reset the update Windows components automatically follow the steps mentioned below.

    (a) click the button to resolve this in the article mentioned below.

    (b) click run in the file download dialog box, and then follow the steps in the fix it Wizard.

    Follow the article mentioned below to get help.

    How to reset the Windows Update components?

    http://support.Microsoft.com/kb/971058

    You receive the error 0 x 80070643, error code 0 x 643 or updates may be offered again when you try to install the .NET Framework updates when you use Windows Update or Microsoft Updates
    http://support.Microsoft.com/kb/976982

    I hope this helps.

Maybe you are looking for