How can I edit the resulting String?

John

If the exception occurs I need to cut the resulting String by a value obtained in another method.

I have a resulting method :

private String generateSomeName() {
        String basicProjectName = "Sth_" + method1();
        return basicProjectName + "_" + "X" ;
    }

    private String method1() {
        try {
            Class.getNumber();  //returns String1 
        } catch (Exception e) {
            log.warn("Unable to get this Number", e);
        }
        return "";            
    }

My problem is that when everything works I will get the proper format of the String Sth_String1_X

But if there is an exception and I will get the empty String "" (without String1) my result is:

Sth__X (with double _)

And I need it to be:

Sth_X with only one _

Any tips how to handle this?

user10386912

Assuming java 8 is used, an option would be to use Optional to add the _ before the result returned by the method1 method only if the result is not null and non empty

private String generateSomeName() {
    String basicProjectName = "Sth" + Optional.ofNullable(method1())
                                              .filter(s -> s.length() > 0)
                                              .map(s -> "_" + s)
                                              .orElse("");
    return basicProjectName + "_X" ;
}

private String method1() {
    try {
        Class.getNumber();  //returns String1 
    } catch (Exception e) {
        log.warn("Unable to get this Number", e);
    }
    return "";            
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I split a string on separators, and keep the separators as elements in the resulting array?

How can I insert a variable into the resulting url parameter string after submitting my GET form?

How can I run a shell command in vim and return the resulting string into a VimScript variable?

How can I edit this TextField?

How can I skip chained promises resulting in exceptions?

Cytoscape: How can I control the size of the resulting PDF in inch or cm?

How can I catch the resulting page of a post request using requests?

How can I edit specific characters from a string within a separate method?

javafx: How can I make TableCell Edit return double instead of string and the font changes color based on a condition?

How can I edit a non-string data value in a data frame?

How can I format a String number to have commas in android Edit Field

How can I format a String number to have space in android Edit Field

How can I edit javascript in a browser

How can I edit the content of .properties file?

How can I edit a Cell in JavaFX with IntegerProperty?

How can I write an edit controller

How can I edit the output of my dataframe?

How can I edit video tags?

How can I edit my dates with a DatePicker?

How can I edit text in "column mode"?

How can I edit a custom dialog background?

How can I edit a keyword search in Chrome?

How can I edit multiple files in VIM?

How can I edit a variable in a running shell?

How can I edit the source of HTML in the clipboard?

How can I update the selection after edit?

How can I edit an inputed text in python

How can I edit a GitHub release date?

Java - How can I edit a class file

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive