开发者

Java: string tokenizer and assign to 2 variables?

Let's say I have a time hh:mm (eg. 11:22) and I want to use a string tokenizer to split. However, after it's split I am able to get for example: 11 and next line 22. But how do I assi开发者_如何学Gogn 11 to a variable name "hour" and another variable name "min"?

Also another question. How do I round up a number? Even if it's 2.1 I want it to round up to 3?


Have a look at Split a string using String.split()

Spmething like

String s[] = "11:22".split(":");;
String s1 = s[0];
String s2 = s[1];

And ceil for rounding up

Find ceiling value of a number using Math.ceil


Rounding a number up isn't too hard. First you need to determine whether it's a whole number or not, by comparing it cast as both an int and a double. If they don't match, the number is not whole, so you can add 1 to the int value to round it up.


// num is type double, but will work with floats too
if ((int)num != (double)num) {
    int roundedNum = (int)num + 1;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜