Reputation: 413
I have a String output like 12345678910111213 from a method (numbers from 1 to 13 without any space in between) I want to get the last number from this series, In this case i need number 13 as output in order to use that with another method.
Can anyone help me in writing java code for the above logic?
Thanks in Adavance!!
Upvotes: 0
Views: 543
Reputation: 92326
If you known the input is always well-formed with 1
, 2
, ..., 9
, 10
, 11
, 12
, …, 99
concatenated you can derive the last value simply by looking at the string length:
public static void main(String[] args) {
System.out.println(getLastNumber("123")); // 3
System.out.println(getLastNumber("12345678910111213")); // 13
}
public static int getLastNumber(String input) {
if (input == null) {
return 0;
} else if (input.length() > 9) {
return 9 + ((input.length() - 9) / 2);
} else {
return input.length();
}
}
For values larger than 99, the algorithm would need to be expanded (there surely is a generic solution to this problem that deals with arbitrarily large numbers…).
Upvotes: 0
Reputation: 33655
Here is an outline of an algorithm, the assumption is that the last number you want is a valid number up to and including 13
.
1
, then (potentially could be 10/11/12/13), go to 2.0|1|2|3
, then track the value of character in 1 as the last_value
, then go to 4, else go to 3last_value
(so for example, characters 1
& 1
, would end up saving 11
in last_value
.last_value
.Upvotes: 0
Reputation: 1
Do you always want to read the number using the last 2 characters? If not how can you know how many characters you want in your number?
int desiredNumberLength = 2;
String str = "12345678910111213";
int number = Integer.parseInt( str.substring( str.length - desiredNumberLength, str.length ) );
if ( number > 13 ) number = number % 10;
Upvotes: 0
Reputation: 5423
just split the last two numbers if greater than 13 then return last char otherwise return last two characters. e.g.:
public int getLastNum(String numbers) throws Exception{
if(numbers.length()==0) return 0;
if(numbers.length()==1) return Integer.parseInt(numbers);
int num=Integer.parseInt(numbers.subString(numbers.length-2,numbers.length));
if(num>13) return num%10;
else return num;
}
Upvotes: 0
Reputation: 2715
If the numbers will always be ordered integers starting from 1 then you can try the following (highly inefficient) approach
public int getLast(String in) {
int current = 0;
while (in.length() != 0) {
current++;
in = in.replaceFirst(String.valueOf(current), "");
}
return current;
}
Upvotes: 2
Reputation: 824
I believe the simple answer is: you can't.
What you MIGHT want to do, if you have control on the string structure, is to pad each number with a zero (or zeros, if relevant), and then you can split the string into pairs (or trios, again, if relevant), and get the last group.
For example, instead of 12345678910111213
you would have "010203040506070809010111213"
, and then you can break it into "01 02 03 04 05 06 07 08 09 10 11 12 13"
- which is then easy to get your "13"
out of.
Upvotes: 0