I created a console program in jcreator (java) which asks the user to enter a number in string and converts that string value into char array and then converts each char array into an integer which I also stored each as an array and then adds all the value from integer array and prints the total on the screen. Someone says its not the solution he is looking for and that I shouldnt use string and I need to focus on the integer value. Is there any other way of doing this as unlike my program? if there is, can you show me the code you have used? thank you.

Dani AI

Generated

Both the arithmetic approach described by and your original char-array idea () are fine; which to use depends on constraints. For small, strictly numeric input the numeric (modulo) method is compact and fast. If you want a concise, modern way that avoids manual loops and is safe for very long numbers or mixed input, use Java 8+ streams to work directly on the input string.

A compact way to sum the digits from a string (skips non-digits) is:

String s = userInput.trim();

int sum = s.chars()
           .filter(Character::isDigit)
           .map(c -> c - '0')
           .sum();

If you still want the individual digits stored (you mentioned an array), produce them without explicit char arrays or manual parsing:

int[] digits = s.chars()
                .filter(Character::isDigit)
                .map(c -> c - '0')
                .toArray();

Notes and gotchas:

  • Filter with Character::isDigit (or regex) so non-digit characters and signs are ignored or handled deliberately. Without filtering, c - '0' can yield wrong values for non-digits.
  • If input may be negative and you want to include the sign, handle s.startsWith("-") separately.
  • For extremely large numeric computations that need numeric semantics beyond digit-sums (not just sums), consider BigInteger to avoid overflow.
  • Unless you need the digits later, computing the sum on the fly (no array) uses less memory and is simpler.

These stream snippets give a readable, concise alternative to manual char-to-int loops and the numeric modulo approach, while also making input validation and digit extraction straightforward.

Recommended Answers

All 4 Replies

if u want to convert string to int

String s = "2";

int x = Integer.parseInt(s);

but u could directly get the int value using nextInt

Scanner sc = new Scanner(System.in);
int x = sc.nextInt(); //edited to assign sc.nextInt() to int x

hope this helps.

give me a sample working program. does that break the string digits into every single integer value? for example, i entered 123 the output should be 6. im trying to do it in a for loop while storing the nextint value to an array and i dont get any gud result

first change the string to integer. that is

Scanner sc = new Scanner(System.in);
String st = sc.nextLine(); 
int x = Integer.parseInt(st);

//then goin to find the total of the sum of the individual digits. 
int total=0;

while(x>0){
  total += x%10;
  x = x/10;
}

its just simple maths. what happens here is, say x=123 and when x is divided by 10 the answer is gonna be 12 and 3 the reminder. so x%10 gives u the reminder. and then u r adding the reminder digits to the total and u r repeating it until the last digit.

hope this helps.

yes it does! this makes sense. i was thinking of storing it into an integer array. thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.