473,395 Members | 2,443 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,395 software developers and data experts.

Need Help With Try, Catch -new Student

I am in my first year of Java programming and I am having a hard time. I have been working on a small program and probably have more than one mistake. My instructor told me to check where the try clause ends. I have no idea.

I am new posting here so I am not sure if this will work. I hope you can copy and paste into a textpad file.
Please please help.
Thanks so much
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class MyType
  6. {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         // declare and construct variables
  11.         String strChoice, strTryString, strTryInt, strTryDouble;
  12.         int choice, tryInt;
  13.         double tryDouble;
  14.         boolean done = false;
  15.  
  16.         // loop while user does not click cancel button
  17.         while(!done)
  18.         {
  19.         try
  20.                 {
  21.                 JOptionPane.showInputDialog(null,"What's My Type?\n1)String\n2)integer\n3)double\n4Quit the program\n");
  22.                 choice = Integer.parseInt(strChoice);
  23.             }
  24.             switch(choice)
  25.                 {
  26.                 case 1:
  27.                     JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
  28.                     break;
  29.  
  30.                 case 2:
  31.                     JOptionPane.showMessageDialog(null, "Correct");
  32.                     tryInt = Integer.parseInt(strChoice);
  33.                     break;
  34.  
  35.                 case 3:
  36.                     JOptionPane.showMessageDialog(null, "Correct");
  37.                     tryDouble = Integer.parseInt(strChoice);
  38.                     break;
  39.  
  40.                 case 4:
  41.                     done = true;
  42.                     JOptionPane.showMessageDialog(null, "Now Closing");
  43.                     break;
  44.  
  45.                 default:
  46.                 throw new NumberFormatException();
  47.  
  48.                 }
  49.  
  50.         }
  51.             }
  52.         catch(NumberFormatException e);
  53.                 {
  54.                 JOptionPane.showMessageDialog(null, "Please try again");
  55.                 }
  56.         }
  57.  
  58.  
  59.  
  60.         System.exit(0);
  61.  
  62.     }
  63.  
  64. }
  65.  
  66.  
Oct 16 '06 #1
5 2444
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;
  15.         boolean done = false;
  16.  
  17.         // loop while user does not click cancel button
  18.         while(!done) {
  19.             try    {
  20.                 String strChoice = JOptionPane.showInputDialog(null,"What's My Type?\n1)String\n2)integer\n3)double\n4Quit the program\n");
  21.                 choice = Integer.parseInt(strChoice);
  22.                 switch(choice) {
  23.                     case 1:
  24.                         JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
  25.                         break;
  26.                     case 2:
  27.                         JOptionPane.showMessageDialog(null, "Correct");
  28.                         //tryInt = Integer.parseInt(strChoice);
  29.                         break;
  30.                     case 3:
  31.                         JOptionPane.showMessageDialog(null, "Correct");
  32.                         //tryDouble = Integer.parseInt(strChoice);
  33.                         break;
  34.                     case 4:
  35.                         done = true;
  36.                         JOptionPane.showMessageDialog(null, "Now Closing");
  37.                         break;
  38.                     default:
  39.                         throw new NumberFormatException();
  40.  
  41.                 }
  42.             }
  43.             catch(NumberFormatException e) {
  44.                 JOptionPane.showMessageDialog(null, "Please try again");
  45.             }
  46.         }
  47.  
  48.         System.exit(0);
  49.  
  50.     }
  51.  
  52. }
Is there anything else that you wanted the program to do?
Oct 17 '06 #2
No, but I still can't get it to compile. It says class or interface expected. I appreciate your help. This stuff is so hard for me.
Thanks,
Kendra
Oct 17 '06 #3
r035198x
13,262 8TB
No, but I still can't get it to compile. It says class or interface expected. I appreciate your help. This stuff is so hard for me.
Thanks,
Kendra
Make sure there are no unintentional characters (dots, commas etc in your code). It does not give an error here
Oct 17 '06 #4
Ahh, There was a colon at the top. Thanks, In my instructions in the book, it tells me begin a while loop to repeat as long as the user does not hit the cancel button but does not tell how to end if they do. Would that be an action listener? It said to import the io package and I noticed you took it out. I didn't know why it said to import that either.
Oct 17 '06 #5
r035198x
13,262 8TB
Ahh, There was a colon at the top. Thanks, In my instructions in the book, it tells me begin a while loop to repeat as long as the user does not hit the cancel button but does not tell how to end if they do. Would that be an action listener? It said to import the io package and I noticed you took it out. I didn't know why it said to import that either.
To make the program exit if the user clicks cancel, add this

Expand|Select|Wrap|Line Numbers
  1. String strChoice = JOptionPane.showInputDialog(null,"What's My Type?\n1)String\n2)integer\n3)double\n4Quit the program\n");
  2.         if(strChoice == null) {
  3.             strChoice = ""+4;
  4.         }
If cancel is pressed, strChoice will be null, so set it to the exit option. The io thing is the reason I asked if you wanted to do something else because as you can see package io has nothing to do with this.
Oct 18 '06 #6

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

Similar topics

5
by: Michael S. Kolias | last post by:
I have a stored procedure that I call from an aspx page but for some weird reason i get this error message: Procedure 'sp_insert_customer' expects parameter '@username', which was not supplied. ...
4
by: Johannes Hansen | last post by:
What are the best practice on handling an exception caused by a Dispose method when its called from inside a loop? Wrap the entire loop in a try-catch or do the try-catch on each iteration to get...
1
by: jmev7 | last post by:
Greetings Data Gurus! I've been asked to help find and delete junk records from a database table that comes from online data request forms: you know, those annoying forms you have to fill out to...
8
by: John Bowman | last post by:
Hello, Does anyone have a good/reliable approach to implementing an IsNumeric() method that accepts a string that may represent a numerical value (eg. such as some text retrieved from an XML...
5
by: Chua Wen Ching | last post by:
Hi, I read from this tutorial at codeproject Question A: http://www.codeproject.com/csharp/GsXPathTutorial.asp regarding xpath.. but i try to apply in my situation, and can't get it...
0
by: muntyanu | last post by:
Hi All, I need to catch windows general exception with code 8001010D "An outgoing call cannot be made since the application is dispatching an input-synchronous call." form C# code. Is it...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
6
by: bill salkin | last post by:
I setup a "Try..." block and attempted to open a non- existant database. It went to the second "catch" not the first. What is the proper "catch" clause for this specific case? TIA, Bill
3
by: Gunnar Syren | last post by:
I'm trying to implement a macro feature in my application by recording and playing back keystrokes. At first I thought it would be enough to catch KeyDown in my main form, but I soon realized that...
11
by: darrel | last post by:
Trying to get back into .net again after being out of it for a while. I'm trying to figure out the proper way to handle multiple events via a try catch. What I'm confused about is the proper...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.