英文:
Check if the number in string object is between range java 8
问题
我想检查字符串中的数字是否在给定范围内。如果是,则在字符串中的数字上加100并返回字符串。
例如,频道具有id名称以及开始和结束时间。
// 创建频道对象列表
List<Channel> cList = Arrays.asList(
new Channel(1, "BBC", "0300", "0500"),
new Channel(2, "TR", "0400", "0509"),
new Channel(3, "NEWS", "0700", "0800")
);
/* 识别值是否在给定范围内并在其中添加100的逻辑 */
List<Channel> cNewList = cList.forEach(channel -> {
int startTime = Integer.parseInt(channel.getStartTime());
if (startTime >= 100 && startTime <= 500) {
int newStartTime = startTime + 100;
channel.setStartTime(String.format("%04d", newStartTime));
}
});
我知道我们可以使用Integer.parseInt(String)
方法将其转换为整数值,但我想将输出返回为字符串。
英文:
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.
答案1
得分: 1
你需要解析字符串为整数以进行比较,所以你可以使用parseInt
函数。
之后,你总是可以将你的整数与空字符串连接起来,以获得一个字符串(例如,1 + ""
会给你一个字符串)。
英文:
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 (e.g. 1 + ""
will give you a string).
答案2
得分: 1
假设你的Channel
类拥有以下成员字段:
class Channel {
private int index;
private String name;
private String startTime;
private String endTime;
// ...
}
在Main
类中,你定义了一个静态辅助方法:
public class Main {
private static Channel getNewStartTimeChannel(Channel c) {
// 将字符串解析为整数
int x = Integer.parseInt(c.getStartTime());
if (x > 100 && x < 500) {
return new Channel(
c.getIndex(),
c.getName(),
// 更新startTime的整数值并将其转换回字符串
"" + (x + 100),
c.getEndTime());
}
return c;
}
}
你可以轻松地将原始列表中的Channel
转换为新列表:
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());
}
英文:
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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论