Welcome to Dream.In.Code
Become a Java Expert!

Join 150,194 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,038 people online right now. Registration is fast and FREE... Join Now!




JOption Pane + decimals

 
Reply to this topicStart new topic

JOption Pane + decimals

stutfly
14 Sep, 2008 - 06:18 PM
Post #1

New D.I.C Head
*

Joined: 30 Aug, 2008
Posts: 28


My Contributions
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 code.gif

This post has been edited by pbl: 14 Sep, 2008 - 06:56 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: JOption Pane + Decimals
14 Sep, 2008 - 07:05 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
You don't do any input validation
When you do:

CODE

//    Declares the num as int
    num = Integer.parseInt(input);


you are assuming the user will enter an int ... which may not be the case

You will add to validate that the input entered is a int

CODE

try {
    num = Integer.parseInt(input);
}
catch(NumberFormatException e) {
   ... the number entered is not an int (double or String)
}



User is offlineProfile CardPM
+Quote Post

stutfly
RE: JOption Pane + Decimals
15 Sep, 2008 - 01:37 PM
Post #3

New D.I.C Head
*

Joined: 30 Aug, 2008
Posts: 28


My Contributions
Ya I think I got it working I just used
num = Double.parseDouble(input);

and declared num as a double



QUOTE(pbl @ 14 Sep, 2008 - 08:05 PM) *

You don't do any input validation
When you do:

CODE

//    Declares the num as int
    num = Integer.parseInt(input);


you are assuming the user will enter an int ... which may not be the case

You will add to validate that the input entered is a int

CODE

try {
    num = Integer.parseInt(input);
}
catch(NumberFormatException e) {
   ... the number entered is not an int (double or String)
}



User is offlineProfile CardPM
+Quote Post

pbl
RE: JOption Pane + Decimals
15 Sep, 2008 - 03:23 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(stutfly @ 15 Sep, 2008 - 02:37 PM) *

Ya I think I got it working I just used
num = Double.parseDouble(input);

and declared num as a double



Will work with 10 10.5 even 10E5 but not if the user enters ABCD

User is offlineProfile CardPM
+Quote Post

stutfly
RE: JOption Pane + Decimals
15 Sep, 2008 - 06:17 PM
Post #5

New D.I.C Head
*

Joined: 30 Aug, 2008
Posts: 28


My Contributions
O ok I see what you mean


QUOTE(pbl @ 15 Sep, 2008 - 04:23 PM) *

QUOTE(stutfly @ 15 Sep, 2008 - 02:37 PM) *

Ya I think I got it working I just used
num = Double.parseDouble(input);

and declared num as a double



Will work with 10 10.5 even 10E5 but not if the user enters ABCD


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:24AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month