I want to check if a number in string is in a given range or not. if yes then add 100 to the number present in the string and return string.
For example channel has id name and start and end time
// created list of channel object
List<Channel> cList= Arrays.asList(
new Channel(1,"BBC","0300","0500"),
new Channel(2,"TR","0400","0509"),
new Channel(3,"NEWS","0700","0800"));
/*logic to identifyif the value is in between given rabge and add 100 to it.*/
List<Channel> cNewList=cList.forEach(
// perform operation to find if c.getstartTime() between
// range(100,500).then add 100 in it.
);
I know we can use Integer.parseInt(String) method to convert to integer value but I want the output back to a string.
You have to parse the string to integer to do the comparison, so you're good with the parseInt
.
Afterwards can always concat your integers with an empty string to get back a string (eg 1 + ""
will give you a string).
Assuming that you class Channel
has these member fields:
class Channel {
private int index;
private String name;
private String startTime;
private String endTime;
...
}
and in the Main
class you define a static helper method:
public class Main {
private static Channel getNewStartTimeChannel(Channel c) {
// parse the string to int
int x = Integer.parseInt(c.getStartTime());
if (x > 100 && x < 500) {
return new Channel(
c.getIndex(),
c.getName(),
// update the int value of the startTime and transform it back to String
"" + (x + 100),
c.getEndTime());
}
return c;
}
you can easily transform the Channels
in the original list into the new one:
public static void main(String[] args) {
List<Channel> cList = Arrays.asList(
new Channel(1, "BBC", "0300", "0500"),
new Channel(2, "TR", "0400", "0509"),
new Channel(3, "NEWS", "0700", "0800")
);
List<Channel> cNewList = cList.stream()
.map(Main::getNewStartTimeChannel)
.collect(Collectors.toList());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.