4 Replies - 8724 Views - Last Post: 18 May 2009 - 07:42 AM Rate Topic: -----

#1 Lowrie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 timosh  Icon User is offline

  • New D.I.C Head

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

Re: String/Number manipulation

Post icon  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() );
}

}

}
Was This Post Helpful? 0
  • +
  • -

#4 Lowrie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 christophermark  Icon User is offline

  • New D.I.C Head

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

Re: String/Number manipulation

Posted 18 May 2009 - 07:42 AM

please help ^_^
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.

please reply asap.. tnx in advance!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1