Question: In JAVA Write a program that prompts the user to enter two positive whole numbers as strings! Create a method that validates the input and

In JAVA

Write a program that prompts the user to enter two positive whole numbers as strings!

  • Create a method that validates the input and returns true if all chars in the string are digits. Call the method from the main and prompt the user to re-enter as long as invalid input was entered.
  • Create a second method that converts the string to an int and returns it. Use a loop to iterate over each char in the string. You can convert a char c to an int by subtracting the char 0 from it: int num = (int)ch - '0'; Add the digit to a result variable and multiply the result with each iteration by 10, for example: 4127 can be split up as 4 * 1000 + 1 * 100 + 2 * 10 + 7 * 1

The program then converts both strings to integers and computes and prints out the sum.

When you are printing the sum. Make another method named usingAPI that does convert "5555" to int 5555 using java API.

WARNING: If you're program does not execute due to syntax errors, you will lose at least 50% of total points.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!