-
June 5th, 2011, 05:41 PM
#1
Splitting number
Helo,
I am currently trying to figure out a way to create a number checker and one of the steps involves splitting up a number into it's separate digits. Here is an example of what I'm trying to do:
given the number 56197245 take every other digit starting with the 5 on the left and double it. So that would give you 10, 2, 14, 8. You then have to add up all the digits involved so you would have 1+0+2+1+4+8.
Any advice?
As a side note I am very new to programming so if I could get an explanation with steps involved it would be most appreciated.
Thanks
-
June 5th, 2011, 07:42 PM
#2
Re: Splitting number
When you say 'given the number', how is this number represented, as a String (i.e. text), or a numeric type (int, Integer, etc) ?
For a String, you can use the charAt(..) method to get alternate digits by their position, then get their numeric values using Integer.parseInt(..), then add them.
For a numeric type, you could use use String.valueOf(..) to get a String representation and use the method above.
Alternatively you could use a fancy maths algorithm...
The fastest algorithm can frequently be replaced by one that is almost as fast and much easier to understand...
D. Jones
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
June 5th, 2011, 08:24 PM
#3
Re: Splitting number
Norm
-
June 8th, 2011, 03:19 PM
#4
Re: Splitting number
Hi, below is my code. I hope it will help.
public int SplitAndCalculate(String number){
int finalResult=0;
String resultofFirstStep="";
for(int i=0;i<number.length();i=i+2){
resultofFirstStep += Integer.toString((number.charAt(i)-48)*2);
}
for(int j=0;j<resultofFirstStep.length();j++){
finalResult+=(resultofFirstStep.charAt(j)-48);
}
return finalResult;
}
-
June 13th, 2011, 05:39 PM
#5
Re: Splitting number
Originally Posted by chaosbeaver
Hi, below is my code. I hope it will help.
public int SplitAndCalculate(String number){
int finalResult=0;
String resultofFirstStep="";
for(int i=0;i<number.length();i=i+2){
resultofFirstStep += Integer.toString((number.charAt(i)-48)*2);
}
for(int j=0;j<resultofFirstStep.length();j++){
finalResult+=(resultofFirstStep.charAt(j)-48);
}
return finalResult;
}
You're generating lots of unnecessary intermediate String objects. Since String is immutable each time you add to a String object a new String object must be created. The Java compiler is allowed to put in an optimization here (for example by substituting the String with a StringBuilder) but that's not guaranteed. Regardless, in my view one should select the most appropriate data structure for each situation and in this case it would be a StringBuilder.
Looping twice is not necessary. It can all be done in one go (avoiding the String intermediates) like this,
Code:
public int SplitAndCalculate(String number){
int finalResult=0;
for(int i=0; i<number.length(); i=i+2) {
int d = number.charAt(i) - '0'; // int (0 - 9) corresponding to char ('0' - '9')
finalResult += (d<5) ? d+d : d+d-9; // double d and add the sum of the digits
}
return finalResult;
}
Last edited by nuzzle; June 15th, 2011 at 05:19 AM.
-
June 8th, 2011, 03:31 PM
#6
Re: Splitting number
@chaosbeaver - what is this junky code you posted?
How is that helping the OP learn how to solve problems and write programs?
In your code, what is the magic number 48? Why do you subtract that from the other?
If you do tricks in your code, you should document what you are doing.
This code can confuse people.
Norm
-
June 8th, 2011, 03:45 PM
#7
Re: Splitting number
Originally Posted by Norm
@chaosbeaver - what is this junky code you posted?
How is that helping the OP learn how to solve problems and write programs?
Well, if it works it should be fine with you. Or as you so succinctly put it in another thread,
"You only know something if you can write working code. "
-
June 8th, 2011, 04:34 PM
#8
Re: Splitting number
Originally Posted by Norm
@chaosbeaver - what is this junky code you posted?
How is that helping the OP learn how to solve problems and write programs?
In your code, what is the magic number 48? Why do you subtract that from the other?
If you do tricks in your code, you should document what you are doing.
This code can confuse people.
Well, I am new to java and also new to this forum. That is actually my first post here. I think you are right. Next time I will post code together with annotations.
@cshoya
//charAt() will get the ASCII value of the character. For numbers 0~9, their actual value= their ASCII value subtract 48.
-
June 9th, 2011, 02:20 AM
#9
Re: Splitting number
Originally Posted by chaosbeaver
//charAt() will get the ASCII value of the character. For numbers 0~9, their actual value= their ASCII value subtract 48.
The standard way to do this is to use '0' instead of 48, like
number.charAt(i) - '0';
But you're still assuming a specific order among the digits and you're assuming ASCII encoding (while Java is using Unicode characters).
So a much safer bet is to use the isDigit() and digit() methods of Character for the char to int conversion. These can also be used to incorporate an error check into the code, namely whether the String really holds digits only.
-
June 9th, 2011, 12:22 PM
#10
Re: Splitting number
You can do this without converting to String.
Hints:
(num/10) >10 Checks if number is greater than 10.
num%10 Get the last digit of a number.
-
June 29th, 2011, 03:24 PM
#11
Re: Splitting number
First of all you have to find the length of each. If length is greater than 1 than you have to split it other wise put same in array.And splitted number also put in array after wards you just add all separated numbers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|