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
How to check the beginning of a string contains a number in Java and convert the same into a number?
String s="-495asadad";
String s="-495asadad";
ParsePosition pp = new ParsePosition(0);
Number num = NumberFormat.getInstance().parse(s, pp);
if (pp.getIndex() == 0) {
System.out.println("No number there");
} else {
System.out.println("Number found: " + num);
}
Output:
Number found: -495
You will probably want to consider more precisely what constitutes a number and from that, how your NumberFormat should work.
The two-arg parse method tries to parse a number from the specified position. In this case position 0, the start of the string. If successful, it will update the position to after the number. If not, it will remain 0, so this is what I test in the if statement.
Related
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
in the exercise they told if string contain 00 or 0 remove it
String rse="12+00+9+88";
String re="[0]{1,2}\\+";
String oper[]=chaine.split("[0-9]{1,2}\\+");
for(int i=1;i<oper.length;i++) {
if (oper.equals(re)) {
}
}
You may juse use String.replaceAll and use 2 regexes
(0+)\+ to remove zeros followed by a +
\+(0+), specific case when zeros are the last one, the + is before it
List<String> values = Arrays.asList(
"12+00+9+88",
"12+0+9+88",
"00+9+88",
"0+9+88",
"12+00",
"12+0"
);
for (String s : values) {
String r = s.replaceAll("(0+)\\+", "").replaceAll("\\+(0+)", "");
System.out.println(r);
}
12+9+88
12+9+88
9+88
9+88
12
12
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
For event handling how can you check a string that has a combination of integers and a character?
For example - 1234p
If the user enters the example above, how can you check if the user enters integers first and then a character at the end? What kind of exception will be thrown if the data type input is not an integer or char?
You can use REGEX [0-9]+[a-zA-Z] to match if the string contains chars and integers otherwise throw an IllegalArgumentException
public void check(String input) {
if (!input.matches("[0-9]+[a-zA-Z]")) {
throw IllegalArgumentException("Not valid string");
}
// do other logic
}
So from your question it sounds like you expect a string that has all numbers except the last character that has to be an alphabet. You can check if the given string matches this condition the following way too:
String string = "1234p";
int length = string.length();
boolean numsFirst = string.substring(0, length - 1).chars().allMatch(x -> Character.isDigit(x));
boolean lastChar = Character.isDigit(string.charAt(length - 1));
if(numsFirst && lastChar)
return true;
else
return false;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
"Sales Docket successfully saved and sent for approval. Please note your document number. JBHL/39/16-17"
i want only the number 39 in the string should be increased by +1 when we run the method
Use regular expression to find the number, then build new string:
private static String increment(String input) {
Matcher m = Pattern.compile("/(\\d+)/").matcher(input);
if (! m.find())
throw new IllegalArgumentException("Invalid document number: " + input);
int newNumber = Integer.parseInt(m.group(1)) + 1;
return input.substring(0, m.start(1)) + newNumber + input.substring(m.end(1));
}
Test
System.out.println(increment("JBHL/39/16-17"));
Output
JBHL/40/16-17
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 8 years ago.
Improve this question
It is asked in an interview to write the code in Java to display the string which doesn't have consecutive repeated characters.
E.g.: Google, Apple, Amazon;
It should display "Amazon"
I wrote code to find continues repeating char. Is there any algorithm or efficient way to find it?
class replace
{
public static void main(String args[])
{
String arr[]=new String[3];
arr[0]="Google";
arr[1]="Apple";
arr[2]="Amazon";
for(int i=0;i<arr.length;i++)
{
int j;
for(j=1;j<arr[i].length();j++)
{
if(arr[i].charAt(j) == arr[i].charAt(j-1))
{
break;
}
}
if(j==arr[i].length())
System.out.println(arr[i]);
}
}
}
Logic : Match the characters in a String with the previous character.
If you find string[i]==string[i-1]. Break the loop. Choose the next string.
If you have reached till the end of the string with no match having continuous repeated character, then print the string.
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 years ago.
Improve this question
I have searched nearly all pages on Stackoverflow but still have not found out how to do this. I have the following string (which is being parsed from file):
f: (matchQuan
(Recipe
(Unique FoodType "slurpee" "xxx-xxx-eee-ddd"))
(Unique IngredType "slurpee" "qqq-rrr-sss") "slurpee"
(Cup-Vol 12)).
Now, I want to parse this string again and extract the string matchQuan, the slurpee, the string Unique and FoodType and Recipe and the ID numbers i.e. xxx-xxx-eee-ddd.
How would I do something like this with multiple extractions from a single string? I can't use Scanner.next() I don't believe because it advances to the next token in string.
Thanks!
Will this help for you?
public static void getString() {
String str = "f: (matchQuan " +
"(Recipe "+
"(Unique FoodType" + " slurpee"+ " xxx-xxx-eee-ddd"+
"(Unique IngredType"+ " slurpee"+ " qqq-rrr-sss"+" slurpee" +
" (Cup - Vol 12)).";
String newStr=str.replaceAll("\\(","").replaceAll("\\)","");
String[] arrStr=newStr.split(" ");
for (int i=0;i<arrStr.length;i++){
System.out.println(arrStr[i]);
}
}