# String/Number manipulation

Page 1 of 1

## 4 Replies - 9544 Views - Last Post: 18 May 2009 - 07:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=13009&amp;s=2750f63ae195f6e292d178a8ec23d7d4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Lowrie

Reputation: 2
• Posts: 15
• Joined: 14-July 05

# String/Number manipulation

Posted 29 October 2005 - 10:57 AM

i need help with this problem for school.
write a program that will ask the user to enter a number, and then print out the individual defits of that number and the sum of those digits. use a methof for calculating the sum of the digits.
eg enter a number: 3146
the digits of 3145 are 6, 4, 1, 3
the sum of these digits are 14

i can do the sum, i just need help to separate the digits. i am not allowed to use an array, because we havnt lernt them yet, but i cant find any other way to do it

This post has been edited by Lowrie: 29 October 2005 - 10:59 AM

Is This A Good Question/Topic? 0

## Replies To: String/Number manipulation

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: String/Number manipulation

Posted 29 October 2005 - 01:04 PM

Well assuming you are taking the input from the user as a string, and are not allowed to use an array, perhaps you could take the input, use the string.length() to determine how many characters are in it, then for however many characters there are, create a new string object made up of the various String.substring()s. You could then loop through those variables, convert them to integers using the parseInt method and add them up.

### #3 timosh

Reputation: 0
• Posts: 4
• Joined: 06-October 05

## Re: String/Number manipulation

Posted 31 October 2005 - 09:27 AM

Not very nice solution.
But it works..........

// ----------------------------------- CODE --------------------
/**
* @author timosh
*/
public class DigitCalc {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Start app");
int res = calcDigits (12345678);
System.out.println("Result = " + res );

res = calcDigits (3146);
System.out.println("Result = " + res );

}

private static int calcDigits (long val)
{
int sum =0;
String str = ""+val;
prn(" Inpit Value: " + val );

int lenght = str.length();
for (int i=0;i<lenght;i++)
{
String ch = "" +str.charAt(i);
prn ("digit [" +i + "] = " + ch);
int digit = Integer.parseInt(ch);
sum = sum +digit;
}
return sum;
}

private static void prn(Object obj)
{
if(true && (obj!= null))
{
System.out.println(obj.toString() );
}

}

}

### #4 Lowrie

Reputation: 2
• Posts: 15
• Joined: 14-July 05

## Re: String/Number manipulation

Posted 31 October 2005 - 01:28 PM

yeah i did it with an array, thanks for the help guys

### #5 christophermark

Reputation: 0
• Posts: 1
• Joined: 18-May 09

## Re: String/Number manipulation

Posted 18 May 2009 - 07:42 AM

using string manipulation:
create a program that will accept a string value in the form First Name Middle Initial Surname and will display it in the form SurName, FirstName MI.
sample Output:
Enter full name [Firstname MI Surname]: Michael S. Jordan

You entered: Michael S. Jordan
Rearranged Name [Surname, Firstname MI]: Jordan, Michael S.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }