检查字符串对象中的数字是否在Java 8范围内。

huangapple go评论58阅读模式
英文:

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&lt;Channel&gt; cList= Arrays.asList(
    new Channel(1,&quot;BBC&quot;,&quot;0300&quot;,&quot;0500&quot;),
    new Channel(2,&quot;TR&quot;,&quot;0400&quot;,&quot;0509&quot;),
    new Channel(3,&quot;NEWS&quot;,&quot;0700&quot;,&quot;0800&quot;)); 
/*logic to identifyif the value is in between given rabge and add 100 to it.*/
List&lt;Channel&gt; 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 + &quot;&quot; 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 &gt; 100 &amp;&amp; x &lt; 500) {
            return new Channel(
                    c.getIndex(),
                    c.getName(),
                    // update the int value of the startTime and transform it back to String
                    &quot;&quot; + (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&lt;Channel&gt; cList = Arrays.asList(
            new Channel(1, &quot;BBC&quot;, &quot;0300&quot;, &quot;0500&quot;),
            new Channel(2, &quot;TR&quot;, &quot;0400&quot;, &quot;0509&quot;),
            new Channel(3, &quot;NEWS&quot;, &quot;0700&quot;, &quot;0800&quot;)
    );
    List&lt;Channel&gt; cNewList = cList.stream()
                    .map(Main::getNewStartTimeChannel)
                    .collect(Collectors.toList());
}

huangapple
  • 本文由 发表于 2020年7月23日 14:03:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63047850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定