Good day!

I just need a little help on how to create this program the precise way. Example if the user will enter 123450, it should output the sum of all individual integers, in the example the result must be 15. If the user will enter a character or a negative number, an error msg will be displayed.

Thank you!

Here is my code below. but when I enter 123450 it displays 21 which is suppose to be 15 only. And feel free to correct the code to be more precise.

import java.io.*;
import java.text.*;

public class Adding_Integer {
static BufferedReader inputNum =  new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException {

int sum=0; 

System.out.print("Enter a positive integer: ");
String intNumbers = inputNum.readLine();
    for (int i=0; i <= intNumbers.length(); i++)
        {
            sum += Integer.parseInt(intNumbers.valueOf(i)); 
        }
            System.out.println("Sum of digits: " + sum);
  }  
}

Thank you!

Edited 4 Years Ago by blocker

Problem's on line 15. Obviously misunderstood what valueOf does.
You need to use either substring or charAt there to pick out each char of the user's input string, then you can use parseInt to convert that one character to a number.
There are better ways to handle each single character, but maybe it's best to get this working with what you already know, then later we can move on to discuss better ways to do it.

I treid this but it gives me an error.

    sum += Integer.parseInt(intNumbers.charAt(i));

Never say "it gives an error". Always post the complete exact text of your error messages.

Anyway, charAt returns a char, so that's not a valid parameter for parseInt. YOu can keep the parseInt but use substring instead of charAt, or you can keep the charAt and use the fact that char is a numeric type, so, for example
myChar - '0' returns 0 if myChar is '0', 9 if myChar is '9' etc

What I am missing here? Ive modified it to substring and I got this result:

Enter a positive integer: 123450
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:470)
    at java.lang.Integer.parseInt(Integer.java:499)
    at Adding_Integer.main(Adding_Integer.java:15)

Process completed.

Now here is my code:

import java.io.*;
import java.text.*;

public class Adding_Integer {
static BufferedReader inputNum =  new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException {

int sum=0; 

System.out.print("Enter a positive integer: ");
String intNumbers = inputNum.readLine();
    for (int i=0; i <= intNumbers.length(); i++)
        {
            sum += Integer.parseInt(intNumbers.substring(i,i)); 
        }
            System.out.println("Sum of digits: " + sum);
  }  
}

Thank you!

public String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

So substring(i,i) will return a string of zero characters length. substring(i, i+1) would be better.

Also line 13 <= intNumbers.length() - that will give you values from 0 to length, which is one value too many.

Thank you richieking, JamesCherrill!

I didnt notice the 1 + 1 of the substring.

Now its working!.. One more thing, can I add exception error if the user enters a character or a negative number? How to to in that way? I believe Ive added throws IOException.

Thank you once again!

Edited 4 Years Ago by blocker

Check the API doc for Integer.parseInt
It tells you it throws a NumberFormatException if the string isn't a valid representation of an integer value

This question has already been answered. Start a new discussion instead.