Substitute for String.valueOf() in Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
I encountered this problem recently.
public static void main(String args[]) {
String string = null;
System.out.println(String.valueOf(string)); // This will print a String equal to "null"
System.out.println(string.toString()); // This will throw a NullPointerException
}
Is there any other by which if I invoke that method(pre-defined method in Java) I will get null as output for null String instead of "null" (null as String) or NullPointerException?

Related

I want to return back a Arraylist of string which contains userName in the response with the response code 200 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to return back a Arraylist of string which contains userName in the response with the response code 200. How to implement this.Thanks in advance.
ResponseEntity<List<String>> someMethod() {
List<String> userNames = getvalues();
return new ResponseEntity<>(userNames, OK);
}

Need to add a default value in a string inside a method in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Suppose we have a string BE231231.
I need an output like this : BE0231231
need to append zero at a this fixed location in java inside a method
public String appendAtIndex(String base, String toAppend, int index){
return base.substring(0,index) + toAppend + base.substring(index);
}
You can implement the logic for restrictions on index accordingly so that base.substring does not give errors.

Masking some value inside Json string in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Suppose i have below Json String:
{
"name":"noor",
"pass":"12345"
}
I want to mask pass value using Regex, like below
{
"name":"noor",
"pass":"*****"
}
How i can do it, using Java regex?
Try this:
"pass":"(.*?)"
As seen on: https://regex101.com/r/cK4bD0/1
try this
String jsonString = "{ \"name\":\"noor\", \"pass\":\"12345\" }";
String result = jsonString.replaceAll("(?<=pass\":\")(.*?)(?=\")", "*****");
System.out.println(result);
{[^}]*"pass"\s*:\s*"(.*?)"[^}]*}
Here is the DEMO: https://regex101.com/r/nL3tP2/2

how i can make my variable equal any element of my Array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have
String[] country={"USA","ks a","UK","France"};
I have a variable String z.
I want to say
if(z.equals(any element of my array (USA or ks or France)
but randomly:
if(z.equals(country[i]){
Doing action any code
}
Can anybody help me?
you can use
if ( Arrays.asList(country).contains(z)) {
// your code
}

I get an error when I try to finish an arguement in Android Studio [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
When I try to debug the app it gives me an error at the of the arguement saying expression expected
public void onClick(View view) {
Contact contact = new Contact(dbHandler.getContactsCount(),
String.valueOf(nameTxt.getText()), String.valueOf(phoneTxt.getText()),
String.valueOf(emailTxt.getText()), String.valueOf(addressTxt.getText()),);
You have a comma at the end of the line:
new Contact(........., String.valueOf(addressTxt.getText()),);
^
You have to remove it

Categories

Resources