• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with out of bounds  RSS feed

 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.







 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you elaborate more?
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually had it all along, I just misused a method and put something I should not have, this is the final code I came up with:

 
Indigo Montes
Greenhorn
Posts: 22
1
C++ IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Carey tried to tell you, is that it is probably not allowed to use a double.

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

Only methods of String.
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Indigo Montes
Greenhorn
Posts: 22
1
C++ IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!