views:

162

answers:

2

Hi,

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 assign 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?

+2  A: 

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

astander
(+1) "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. "
Bozho
A: 

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;
}
Jordan