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
The underscore is not visible in integer data types:
int intHex = 0x0041;
System.out.println("intHex: " + intHex);
int intBinary = 0b01000001;
System.out.println("intBinary: " + intBinary);
int intUnderscore = 1_23_456;
System.out.println("intUnderscore: " + intUnderscore);
If you expect that
int intUnderscore = 1_23_456;
System.out.println("intUnderscore: " + intUnderscore);
prints
intUnderscore: 1_23_456
you misunderstod the purpose of the underscore. It is just syntactic sugare meant to make source code easier to read.
Related
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 months ago.
Improve this question
I need to count the number of times a letter's present in a String.
For example:
str = "/data/name/data/name"
How do we get the number of / in this string?
val count = str.count { it == '/' }
To be honest, I am not sure whether you need an answer in java or kotlin (your tags include both), so if you need an answer in java:
String input = "/data/name/data/name";
char search = '/';
long count = input.chars().filter(ch -> ch == search).count();
(and if you need a kotlin version, just take a look at #Ivo's answer)
I think you can count with this way,
val str = "/data/name/data/name"
var count = 0
str.forEach {
if(it == '/'){
count++
}
}
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 9 months ago.
Improve this question
creditTIDstatusArray=[93312263-1-09722612223, 99802001-1-09102842369, 99802002-1-09102842369];
creditTIDstatusList.addAll(Arrays.asList(creditTIDstatusArry));
searchValue="99802002-1".
int retval=creditTIDstatusList.indexOf("99802002-1");
System.out.println("The element at index is:" retval);
Output: 2
Please let me know how I can find the index of the given above(searchValue)element.
As you never have posted the reproducible code.
Assumptions
creditTIDstatusArray is a String type array.
Your search query always stays in front of each String value in the array.
Multiple indexes may start with the same search value.
String[] creditTIDstatusArray=new String[]{"93312263-1-09722612223", "99802001-1-09102842369", "99802002-1-09102842369"};
String searchValue="99802002-1";
for (int i = 0; i < creditTIDstatusArray.length; i++) {
if(creditTIDstatusArray[i].startsWith(searchValue)){
System.out.println("Index :" + I); // this will print all the indexes that starts with searchvalue
}
}
I am not sure why you have added the array into a list and then searched index of because it will never work as you are searching only part of a String rather than the whole value or object.
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 1 year ago.
Improve this question
In Python, below code can be run:
str = "i = %s"
print(str % i)
or
str = "i = {}"
print(str.format(i))
I know that Java can format string using
String str = String.format("i = %s", i);
But, I want to separate "i = %s" and i to make "i = %s" variable and use it on other codes, like below:
String pathFormat = "/v2.0/%s/search";
System.out.println(String.format(pathFormat, userId);
Is it possible in Java?
Yes, you can do that in Java just as in your last example.
String numberFormat = "i = %s";
int i = 17;
System.out.println(String.format(numberFormat, i));
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 2 years ago.
Improve this question
So basically what I did is I converted the string to an integer and now I want to make it so whatever number the user enters will be multiplied by 7.
String strName;
strName=this.txtInputDogName.getText();
String strAge;
strAge=this.txtInputDogAge.getText();
int foo;
foo = Integer.parseInt(strAge);
int strAge = (strAge*7);
this.lblOutput.setText("Hello "+strName+"'s age in human years is "+strAge);
You don't need to define and assign to a variable on separate lines.
Instead of writing
String strName;
strName=this.txtInputDogName.getText();
You can write it like this:
String strName = this.txtInputDogName.getText();
And you dont need the variable foo at all. You can just use parseInt, multiply it by 7, and convert it to string.
Now the improved and correct version of your code is:
String strName = this.txtInputDogName.getText();
String strAge = this.txtInputDogAge.getText();
strAge = Integer.toString(Integer.parseInt(strAge) * 7);
this.lblOutput.setText("Hello " + strName + "'s age in human years is " + strAge);
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 2 years ago.
Improve this question
I'm looking for a regex that is able to match numbers bigger than -328, and if it is possible to provide another solution to match the same pattern but without the zero. I tried many things but still not sure about how it works, for example, ^\-?[0-9]\d{3,}$
I'm using it with the com.jfoenix.validation.RegexValidator in order to check the pattern in a textfield.
Thanks
Try this.
String pat = "^-(32[0-7]|3[0-1]\\d|[1-2]\\d\\d|\\d{1,2})|\\d+$";
for (int i = -1000; i <= 1000; ++i) {
String s = Integer.toString(i);
boolean result = s.matches(pat);
if (result != (i > -328))
System.out.println(i + " fail!");
}