473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

due in 45 mins help! check input for double or integer

Okay, I finally got this program to run according to what the book had us build it as. Now prof wants case 2 and case 3 to prompt again for input, check input to see if it is the correct type, then display a "correct" message if is correct.
So, If you choose 2 from the original pane, you would be prompted to enter an integer. The program would check to see if you input an integer and if it was then you would get a "correct" message. If not you would get an error message.
I have tried and tried to get this and I am out of time here. It does not go over this in my book and I have only had Java a few weeks now. PLEASE HELP if you can. I really really appreciate it!
Thanks

Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.JOptionPane;
  3. import java.io.*;
  4.  
  5. public class MyTypeafter
  6. {
  7.     public static void main(String[] args) throws IOException
  8.     {
  9.         // declare and construct variables
  10.         String strChoice, strTryString, strTryInt, strTryDouble;
  11.         int choice, tryInt;
  12.         double tryDouble;
  13.         boolean done = false;
  14.  
  15.         // loop while user does not click cancel button
  16.         while(!done)
  17.         {
  18.         try
  19.             {
  20.                 String myMsg = "What's my Type?\n";
  21.                 myMsg = myMsg + "1) String\n";
  22.                 myMsg += "2) Integer\n";
  23.                 myMsg += "3) Double\n";
  24.                 myMsg += "4) Quit the Program\n";
  25.  
  26.                 strChoice = JOptionPane.showInputDialog(null, myMsg, "Input", JOptionPane.QUESTION_MESSAGE);
  27.                 if(strChoice == null) break;
  28.                 choice = Integer.parseInt(strChoice);
  29.  
  30.             switch(choice)
  31.                 {
  32.                 case 1:
  33.                     JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
  34.                     break;
  35.  
  36.                 case 2:
  37.                     JOptionPane.showMessageDialog(null, "Please enter an integer");
  38.                     tryInt = Integer.parseInt(strChoice);
  39.                     if (strChoice == integer)
  40.                     {
  41.                         JOptionPane.showMessageDialog("correct");
  42.                     }//end if
  43.                     break;
  44.  
  45.                 case 3:
  46.                     JOptionPane.showMessageDialog(null, "Correct Double Value");
  47.                     tryDouble = Double.parseDouble(strChoice);
  48.                     break;
  49.  
  50.                 case 4:
  51.                     done = true;
  52.                     JOptionPane.showMessageDialog(null, "Now Closing");
  53.                     break;
  54.  
  55.                 default:
  56.                 throw new NumberFormatException();
  57.  
  58.                 }//end switch
  59.  
  60.               }//end try
  61.  
  62.         catch(NumberFormatException e)
  63.             {
  64.             String myMsg2 = "Error, problem with number format\n" + e;
  65.             JOptionPane.showMessageDialog(null, myMsg2, "Error", JOptionPane.INFORMATION_MESSAGE);
  66.             }//end catch
  67.  
  68.         }//end while
  69.  
  70.     }//end main method
  71.  
  72. }//end class
  73.  
  74.  
Oct 18 '06 #1
1 7404
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class MyType
  4. {
  5.  
  6.     public static void main(String[] args)
  7.     {
  8.         // declare and construct variables
  9.  
  10.  
  11.  
  12.         int choice = 0;
  13.         int tryInt = 0;
  14.         double tryDouble = 0.0;
  15.         boolean done = false;
  16.  
  17.         // loop while user does not click cancel button
  18.         String entered = "";
  19.         while(!done) {
  20.             try    {
  21.                 String strChoice = JOptionPane.showInputDialog(null,"What's My Type?\n1)String\n2)integer\n3)double\n4Quit the program\n");
  22.                 if(strChoice == null) {
  23.                     strChoice = ""+4;
  24.                 }
  25.                 choice = Integer.parseInt(strChoice);
  26.                 switch(choice) {
  27.                     case 1: {
  28.                         entered = JOptionPane.showInputDialog(null,"Enter The String\n");
  29.                         JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
  30.                         break;
  31.                     }
  32.                     case 2: {
  33.                         entered = JOptionPane.showInputDialog(null,"Enter The integer\n");
  34.                         tryInt = Integer.parseInt(entered);
  35.                         JOptionPane.showMessageDialog(null, "Correct");
  36.                         break;
  37.                     }
  38.                     case 3: {
  39.                         entered = JOptionPane.showInputDialog(null,"Enter The Double\n");
  40.                         tryDouble = Integer.parseInt(entered);
  41.                         JOptionPane.showMessageDialog(null, "Correct");
  42.  
  43.                         break;
  44.                     }
  45.                     case 4:
  46.                         done = true;
  47.                         JOptionPane.showMessageDialog(null, "Now Closing");
  48.                         break;
  49.                     default:
  50.                         throw new NumberFormatException();
  51.  
  52.                 }
  53.             }
  54.             catch(NumberFormatException e) {
  55.                 JOptionPane.showMessageDialog(null, "Please try again");
  56.             }
  57.         }
  58.  
  59.         System.exit(0);
  60.  
  61.     }
  62.  
  63. }
oops hope this is not too late!
Oct 18 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
1
by: Manjesh Gowda | last post by:
Hi all, I am maintaing a persistent HTTP connection using the "HTTP CONNECT" method , this works fine for many proxies like Squid, ISA , etc. But the iPlanet proxy disconnects the conneciton...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
3
by: Funky | last post by:
Hi, I have developed an ASP.NET application which has been running in production for around 3 months without any major glitches. Recently, a user was attempting to upload a rather large CSV file...
1
by: switzerland qunatium computer | last post by:
HERE I BUILT A QUICK MATRIX TOOOK 5 MINS Body: HERE I BUILD ONE FOR YOU 1.http://en.wikipedia.org/wiki/Real-time_computing 2.http://en.wikipedia.org/wiki/Quantum_computing NOW ALL YOU NEED...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
3
by: cbsGEM | last post by:
I have a start and end date columns (date) and start and end time columns (varchar2). I need to calculate the days, hours and minute between these 4 columns, I also have to exclude weekends and...
3
by: ashes | last post by:
Hi all, i am creating a website and i want to log out the user after 30 mins of inactivity. how do i do this using vb.net. Also, if the user tries to login and not getting through. after 3...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.