Question
I need help with this java programming project. I have to take an already working program and add a gui to it. The program deals
I need help with this java programming project. I have to take an already working program and add a gui to it. The program deals with recursion. You enter in a number and it's supposed to do the following. Say you entered in 2 for example, this is what you should see.
Enter an integer: 2 The digits in that number are: two If you add ten to that number, the digits in the new number are: one two
My program boots up, but when I enter in a number, the program just closes instead. I hope someone can help me fix this.
Here is my code.
import javax.swing.*;
import java.util.Scanner;
public class RecursionDemoGUI { public static void main(String[] args) { Scanner outputStream = null; String InputText = JOptionPane.showInputDialog("Enter an integer:"); Scanner keyboard = new Scanner(System.in);; int number = keyboard.nextInt( ); JOptionPane.showMessageDialog(null,"The digits in that number are:"); displayAsWords(number); System.out.println( ); JOptionPane.showMessageDialog(null,"If you add ten to that number, "); JOptionPane.showMessageDialog(null,"the digits in the new number are:"); number = number + 10; displayAsWords(number); System.out.println( ); }
/** Precondition: number >= 0 Displays the digits in number as words. */ public static void displayAsWords(int number) { if (number < 10) System.out.print(getWordFromDigit(number) + " "); else //number has two or more digits { displayAsWords(number / 10); System.out.print(getWordFromDigit(number % 10) + " "); } } /** Precondition: 0 <= digit <= 9 Returns the word for the argument digit. */ private static String getWordFromDigit(int digit) { String result = null; switch (digit) { case 0: result = "zero"; break; case 1: result = "one"; break; case 2: result = "two"; break; case 3: result = "three"; break; case 4: result = "four"; break; case 5: result = "five"; break; case 6: result = "six"; break; case 7: result = "seven"; break; case 8: result = "eight"; break; case 9: result = "nine"; break; default: System.out.println("Fatal Error."); System.exit(0); break; } return result; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started