## Java || Compute The Sum From A String Of Integers & Display Each Number Individually Here is a simple program, which was presented in a Java course. This program was used to introduce the input/output mechanisms which are available in Java. This assignment was modeled after Exercise 2.30, taken from the textbook “Java How to Program” (early objects) (9th Edition) (Deitel). It is the same exercise in both the 8th and 9th editions.

Our class was asked to make a program which prompts the user to enter a non-negative integer into the system. The program was supposed to then extract the digits from the inputted number, displaying them each individually, separated by a white space (” “). After the digits are displayed, the program was then supposed to display the sum of those digits to the screen. So for example, if the user inputted the number “39465,” the program would output the numbers 3 9 4 6 5 individually, and then the sum of 27.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

```How To Get String Input If/Else Statements Do/While Loops For Loops Methods (A.K.A "Functions") - What Are They? ParseInt - Convert String To Integer Substring Try/Catch ```

``` Compute The Sum From A String Of Numbers Java import java.util.Scanner; import java.lang.Math; public class SumFromString { public static void main(String[] args) { // declare variables char question = 'n'; String userInput= " "; Scanner cin = new Scanner(System.in); // display message to screen System.out.println("nWelcome to My Programming Notes' Java Program."); do{// this is the start of the do/while loop int sum = 0; // get data from the user System.out.print("nEnter a non negative integer: "); userInput = cin.nextLine(); System.out.println(""); // if the string isnt a number, dont compute the sum if(!IsNumeric(userInput)) { System.out.println(userInput+" is not a number..."); System.out.println("Please enter digits only!"); } // if the string is a negative number, dont compute the sum else if(IsNegative(userInput)) { System.out.println(userInput+" is a negative number..."); System.out.println("Please enter positive digits only!"); } // if the string is a decimal number (i.e 19.87), dont compute the sum else if(IsDecimal(userInput)) { System.out.println(userInput+" is a decimal number..."); System.out.println("Please enter positive whole numbers only!"); } // if everything else checks out OK, then compute the sum else { System.out.print("The digits are: "); sum = ComputeSum(userInput); System.out.println("and the sum is " + sum); } // ask user if they want to enter more data System.out.print("nDo you have more data for input? (Y/N): "); String response = cin.nextLine(); response=response.toLowerCase(); question = response.charAt(0); System.out.println("n------------------------------------------------"); }while(question == 'y'); System.out.print("nBYE!n"); }// end of main public static boolean IsNumeric(String str) {// checks to see if a string is a number try { Double.parseDouble(str); return true; } catch(NumberFormatException nfe) { return false; } }// end of IsNumeric public static boolean IsNegative(String str) {// checks to see if string is a negative number return Double.parseDouble(str) < 0; }// end of IsNegative public static boolean IsDecimal(String str) {// checks to see if string is a decimal number return (Math.floor(Double.parseDouble(str))) != Double.parseDouble(str); }// end of IsDecimal public static int ComputeSum(String userInput) {// displays each number individually & computes the sum of the string int sum = 0; String[] temp = new String[userInput.length()]; for(int counter=0; counter < userInput.length(); ++counter) { temp[counter] = userInput.substring(counter,counter+1); System.out.print(temp[counter] + " "); sum = sum + Integer.parseInt(temp[counter]); } return sum; }// end of ComputeSum }// http://programmingnotes.org/ 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 import java.util.Scanner;import java.lang.Math; public class SumFromString{   public static void main(String[] args)   {    // declare variables    char question = 'n';    String userInput= " ";    Scanner cin = new Scanner(System.in);       // display message to screen    System.out.println("nWelcome to My Programming Notes' Java Program.");       do{// this is the start of the do/while loop       int sum = 0;       // get data from the user    System.out.print("nEnter a non negative integer: ");    userInput = cin.nextLine();       System.out.println("");       // if the string isnt a number, dont compute the sum    if(!IsNumeric(userInput))    {    System.out.println(userInput+" is not a number...");    System.out.println("Please enter digits only!");    }       // if the string is a negative number, dont compute the sum    else if(IsNegative(userInput))    {    System.out.println(userInput+" is a negative number...");    System.out.println("Please enter positive digits only!");    }       // if the string is a decimal number (i.e 19.87), dont compute the sum    else if(IsDecimal(userInput))    {    System.out.println(userInput+" is a decimal number...");    System.out.println("Please enter positive whole numbers only!");       }       // if everything else checks out OK, then compute the sum    else    {    System.out.print("The digits are: ");    sum = ComputeSum(userInput);    System.out.println("and the sum is " + sum);    }       // ask user if they want to enter more data    System.out.print("nDo you have more data for input? (Y/N): ");    String response = cin.nextLine();    response=response.toLowerCase();    question = response.charAt(0);       System.out.println("n------------------------------------------------");       }while(question == 'y');       System.out.print("nBYE!n");   }// end of main      public static boolean IsNumeric(String str)     {// checks to see if a string is a number      try    {    Double.parseDouble(str);    return true;       }    catch(NumberFormatException nfe)    {    return false;    }   }// end of IsNumeric      public static boolean IsNegative(String str)     {// checks to see if string is a negative number      return Double.parseDouble(str) < 0;   }// end of IsNegative         public static boolean IsDecimal(String str)     {// checks to see if string is a decimal number      return (Math.floor(Double.parseDouble(str))) != Double.parseDouble(str);   }// end of IsDecimal      public static int ComputeSum(String userInput)   {// displays each number individually & computes the sum of the string    int sum = 0;    String[] temp = new String[userInput.length()];       for(int counter=0; counter < userInput.length(); ++counter)    {    temp[counter] = userInput.substring(counter,counter+1);    System.out.print(temp[counter] + " ");    sum = sum + Integer.parseInt(temp[counter]);    }    return sum;   }// end of ComputeSum}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

`Welcome to My Programming Notes' Java Program.`

``` Enter a non negative integer: 0 The digits are: 0 and the sum is 0 Do you have more data for input? (Y/N): y ------------------------------------------------ Enter a non negative integer: 39465 The digits are: 3 9 4 6 5 and the sum is 27 Do you have more data for input? (Y/N): y ------------------------------------------------ Enter a non negative integer: H3ll0 W0rld H3ll0 W0rld is not a number... Please enter digits only! Do you have more data for input? (Y/N): y ------------------------------------------------ Enter a non negative integer: -98 -98 is a negative number... Please enter positive digits only! Do you have more data for input? (Y/N): y ------------------------------------------------ Enter a non negative integer: 19.87 19.87 is a decimal number... Please enter positive whole numbers only! Do you have more data for input? (Y/N): n ------------------------------------------------ ```

```BYE! ```