Abad Ashraf

Greenhorn

Posts: 27

posted 3 weeks ago

This is a practice problem and not a homework problem of any sort. I am having troubles with the round method, it goes out of bounds when I do not want it to. This is what I need to do:

The round method should round the num instance variable to n decimal places by using the (n + 1)th

digit. Return the rounded value. If there are not at least (n + 1) digits following the decimal, return the original value of num. As a reminder, rounding works as follows: If the (n + 1)th digit is 5 or greater, round the nth digit up by one. Otherwise, leave the nth digit as it is. You may not use the Math class.

I can only use the string method: indexOf, charAt, contains, substring, replace, length() and the regex methods split, replaceAll and matches.

I was trying to get the number after the decimal point. For example if I had 3 as a input for the method I would start at the number after the decimal point and go up to 6 to get the numbers 257. After that I would use 6 in the original string to get the number next to 7 in 257 and see if its greater than 5 or not. If it is I can make 257 to 258 via count, if not I leave it as is. If it went out of bounds I print the original number. The problem I get is that I go out of bounds from the get-go.

The round method should round the num instance variable to n decimal places by using the (n + 1)th

digit. Return the rounded value. If there are not at least (n + 1) digits following the decimal, return the original value of num. As a reminder, rounding works as follows: If the (n + 1)th digit is 5 or greater, round the nth digit up by one. Otherwise, leave the nth digit as it is. You may not use the Math class.

I can only use the string method: indexOf, charAt, contains, substring, replace, length() and the regex methods split, replaceAll and matches.

I was trying to get the number after the decimal point. For example if I had 3 as a input for the method I would start at the number after the decimal point and go up to 6 to get the numbers 257. After that I would use 6 in the original string to get the number next to 7 in 257 and see if its greater than 5 or not. If it is I can make 257 to 258 via count, if not I leave it as is. If it went out of bounds I print the original number. The problem I get is that I go out of bounds from the get-go.

posted 3 weeks ago

I have a different interpretation of the requirements: I don't think you're allowed to use Double.parseDouble. In fact, I don't think you are allowed to use the double type at all and that it must be done entirely with string manipulation.

For instance, if a character is one of '5', '6', '7', '8', or '9' it will need to be rounded up and set to '0'.

For instance, if a character is one of '5', '6', '7', '8', or '9' it will need to be rounded up and set to '0'.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

posted 3 weeks ago

That pretty much says that all you can use is string manipulation. It ends up just like if you tried to figure this out with paper and pencil. In fact, I would suggest working out an example with paper and pencil, being hyper-aware of all the decisions that you are making in the process. For example, finding which character contains (if any) the decimal point.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

I can only use the string method: indexOf, charAt, contains, substring, replace, length() and the regex methods split, replaceAll and matches.

That pretty much says that all you can use is string manipulation. It ends up just like if you tried to figure this out with paper and pencil. In fact, I would suggest working out an example with paper and pencil, being hyper-aware of all the decisions that you are making in the process. For example, finding which character contains (if any) the decimal point.

Abad Ashraf

Greenhorn

Posts: 27

posted 3 weeks ago

What Carey tried to tell you, is that it is probably not allowed to use a double.

Only methods of String.

I canonlyuse the string method: indexOf, charAt, contains, substring, replace, length() and the regex methods split, replaceAll and matches.

Only methods of String.

If you spend too much time thinking about a thing, you'll never get it done. Make at least one definite move daily toward your goal. - Bruce Lee

Abad Ashraf

Greenhorn

Posts: 27

posted 3 weeks ago

Oh no I meant that out of all the other String methods in Java I am only limited to the ones I was taught in class. So I can use other things like double and so in Java along with the strings I was taught. Like I cannot use trim or something like that.

posted 3 weeks ago
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

I suggest a better test program. I'm sure it will show weaknesses in your approach.

I get this:

The answer is wrong but also truncates the second decimal place.

I get this:

The answer is wrong but also truncates the second decimal place.

posted 3 weeks ago

You may be correct but I'm a little suspicious because over the last 2 years here at Code Ranch there have been numerous Java problems posted that constrain what can be used to solve an otherwise medium difficulty problem and all of them have been very specific as to what can be used to solve the problem.

P.S. It is possible to solve without converting the number to a double.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Abad Ashraf wrote:Oh no I meant that out of all the other String methods in Java I am only limited to the ones I was taught in class. So I can use other things like double and so in Java along with the strings I was taught. Like I cannot use trim or something like that.

You may be correct but I'm a little suspicious because over the last 2 years here at Code Ranch there have been numerous Java problems posted that constrain what can be used to solve an otherwise medium difficulty problem and all of them have been very specific as to what can be used to solve the problem.

P.S. It is possible to solve without converting the number to a double.

It is sorta covered in the JavaRanch Style Guide. |