Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions