Need to convert strings representing an integer


We often need to convert Strings representing an integer to an integer value - for example when accepting input from the user.
Open the editor for Q8. Write a method called toInt that takes a String parameter, and returns the integer that it represents.
The method should first check the String to make sure that it is not null, is not empty, and contains only valid characters for an integer. The first character may be a minus sign, to indicate a negative integer. All subsequent characters should be digits. If the String is not valid, then an exception should be thrown.
Ensure the tests in Q8Test pass. Half of the marks for this question are for passing the tests.
Be sure to include helpful messages in your exceptions. Some of the marks for this question will be awarded for helpful exception messages.
Your method should use at least one loop. Use the most appropriate loop for your purpose. Some of the marks for this question will be awarded for effective use of loops.
Keep your code as readable as possible. Some of the marks for this question are awarded for using simple expressions, methods to reduce code repetition, and consistent formatting.
Hints:
? Converting a single character to an integer can be done quite simply using character arithmetic. For example, if char c = '0'; then what is the value of the expression c - '0'?
? Once you have figured out one digit numbers try to get your algorithm working for 2 digit number next. Remember the place value system? Now the first digit is worth 10 times as much as the second digit.
? What is the relationship between the place values of numbers with lots of digits? For example, in a four digit number the place values are 1000, 100, 10, 1.

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Need to convert strings representing an integer
Reference No:- TGS092522

Expected delivery within 24 Hours