Question: Can someone help me with this error in my recursive program? What I need help with is dealing with the negatives. Here is my code
Can someone help me with this error in my recursive program?
What I need help with is dealing with the negatives. Here is my code below:
import java.util.Scanner; import javax.swing.JOptionPane;
public class PrintDigits {
private static String output = "";
private void printDigits2Num (int num) { if (num == 0) { } else { int lastDigit = num % 10;
printDigits2Num (num / 10);
if (lastDigit < 0) { lastDigit = -lastDigit; output += "Negative "; }
if (lastDigit == 0) output += "Zero "; else if (lastDigit == 1) output += "One "; else if (lastDigit == 2) output += "Two "; else if (lastDigit == 3) output += "Three "; else if (lastDigit == 4) output += "Four "; else if (lastDigit == 5) output += "Five "; else if (lastDigit == 6) output += "Six "; else if (lastDigit == 7) output += "Seven "; else if (lastDigit == 8) output += "Eight "; else if (lastDigit == 9) output += "Nine "; } }
public static void main (String [] args) { boolean running = true; while (running == true) { try { String input = JOptionPane.showInputDialog (null, "Input: ");
String [] numbers; numbers = input.trim ().split ("\\s+"); for (int i = 0 ; i < numbers.length ; i++ ) { try { int num; num = Integer.parseInt (numbers[i]); PrintDigits c = new PrintDigits (); c.printDigits2Num (num); output = output.trim () + ". "; } catch (NumberFormatException e) { output += "Error: " + numbers[i] + " must " + "be a valid " + "integer! "; } } JOptionPane.showMessageDialog (null, output); output = ""; } catch (NullPointerException npe) { JOptionPane.showMessageDialog (null, "Error Message: num1[, num2, ...:] " + "must be valid integers."); running = false; }
} } }
What I have bolded is my base case, and right now it does nothing. I want the base case to handle the negative numbers that I input.
Here is a sample input/output with my code:
Input:
2340 -98
Output:
Two Three Zero Four.
Negative Nine Negative Eight
What It Should Be:
Input:
2304 -98
Output:
Two Three Zero Four
Negative Nine Eight
ASSIGNMENT BELOW:
In this program you will be taking numbers (as digits) entered by the user and printing them out in English one digit at a time. You will be applying your knowledge of recursion to make this program.
You will need to check if the user entered a valid number (or numbers) in a JOptionPane dialog box, if not, display an error message in a JOptionPane dialog box, and re-prompt the user.
Then go through each number given by the user, one at a time (a loop will be helpful here). Try to convert the string to an integer, if there is an error, display an error message and continue to the next number if there is one (you might need to try and catch something here).
If the number parses successfully, you should check if the number is negative, and print out the word "Negative" before any of the digits if it is.
After successfully retrieving the number, call printDigits2Num () with the number.
After finishing with the current number, loop again and complete the rest of the user entered numbers if there are any.
private void printDigits2Num (int num);
This is the recursive method to display all the digits in their English format. Note: this method must be recursive, and you may not add any extra parameters to the method. If the function is not recursive, or you change the parameters, you will receive 0 points for this program.
With each recursive call to printDigits2Num () print out the rightmost digit of num in its English format.
Determine if you need to make another recursive call, this will happen when num contains more than one digit. After the recursive call isolate the rightmost digit of num (there is a simple math operation to use here). Since num might be negative, you should check to see if the isolated digit is negative and convert it to positive (do not use multiplication to do this).
Display the final resulting English number in a JOptionPane dialog box.
There are a few things to deal with that have not already been mentioned:
? Separate each English digit by a space, but there should not be a space before the first digit or after the last digit. This will require you to do some logic to make this work properly.
? Each number needs to be on its own line (see the sample output below to understand this).
? There must be a period printed after the last digit for each number.
? Notice in sample output #8 leading zeros are ignored, Integer.parseInt () will take care of this, you do not need to do anything special
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
