My problem is when I enter a decimal it breaks but it works fine when not entering one it also breaks when I insert a letter if anyone could help that would be great
CODE
/**
* @(#)formulagame.java
*
* formulagame application
*
* @author
* @version 1.00 2008/9/5
*/
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class formulagame {
public static void main(String[] args) {
// Declared Strings
String input, calc;
// Declared integers
int again, begin, num, instruct;
// Asks the user if they would like to play the game
begin = JOptionPane.showConfirmDialog (null, "Would you like to play the formula game? İ");
if (begin == JOptionPane.NO_OPTION || begin == JOptionPane.CANCEL_OPTION )
JOptionPane.showMessageDialog (null, "Thank you have a nice day!!");
if (begin == JOptionPane.YES_OPTION)
{
do
{
instruct = JOptionPane.showConfirmDialog (null, "Would you like to see the instructions?");
// Tells the user the instructions and shows an example
if (instruct == JOptionPane.YES_OPTION)
JOptionPane.showMessageDialog (null, "This program tells you what number you have inserted,\nthen calculates the square of the number inserted,\n"
+ "then the square root,\n then if it is even or odd\nand last if it"
+ " is a vowel or composite."
+"EXAMPLE:\n"
+"ŻŻŻŻŻŻŻŻ\n"
+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+ "You chose the number \"number inserted\"\n"
+ "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+ "The square of \"number inserted\" is: \"square of number\"\n"
+ "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+ "\nThe square root of \"number inserted\" is: \"Square root of the number\"\n"
+ "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+ "\nThe number \"number inserted\" is \"even\" or \"odd\"\n"
+ "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+ "\nThe number \"number inserted\" is a \"vowel\" or \"composite\" \n");
// Tells the user to enter a number between 1 and 100
input = JOptionPane.showInputDialog ("Enter a number between 1 and 100: ");
// Declares the num as int
num = Integer.parseInt(input);
while (num>100 || num<1)
{
JOptionPane.showMessageDialog (null, "Follow Directions!!");
input = JOptionPane.showInputDialog ("Please try again: ");
num = Integer.parseInt(input);
}
DecimalFormat fmt = new DecimalFormat ("0.###");
// Does the calculations
calc = ("Here is your results:\n"
+"ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ\n"
+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+"You chose the number " + num + "\n"
+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+"The square of " + num + " is: " + (int)Math.pow(num,2) + "\n"
+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+ "The square root of " + num + " is: " + Math.sqrt(num) + "\n"
+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+ "The number " + num + " is" + ((num%2 == 0) ? " even" : " odd") + "\n"
+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
// Prints out the results
JOptionPane.showMessageDialog (null, calc);
// Asks the user if they would like to do another
again = JOptionPane.showConfirmDialog (null, "Do Another?");
}
// Repeats the program as long as yes is clicked
while (again == JOptionPane.YES_OPTION);
// Thanks the user for playing
JOptionPane.showMessageDialog (null, "Thank you have a nice day!!");
}
}
}
Edited to remove some useless blank lines and add the correct [ code] tags
Please
This post has been edited by pbl: 14 Sep, 2008 - 06:56 PM