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

Two beginners mistakes...need some input.

Here is a program I have to write for an assignment. I cannot get it to exit when I click cancel and for some reason I get an error when you enter 3 as your option. I am sure it is a beginners mistake but I am stuck. Any assistance would be greatly appreciated. Just be easy...I am still new. Thanks.

************************************************** *******************
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.swing.JOptionPane;
  3. import java.text.DecimalFormat;
  4.  
  5. public class MyType1
  6. {
  7.     public static void main (String[] args)
  8.     {
  9.         //Declaring Class Variables
  10.         String strChoice, strTryString, strTryInt, strTryDouble;
  11.         int choice, tryInt;
  12.         double tryDouble;
  13.         boolean done = false;
  14.             while (!done)
  15.             //while (!done= null)
  16.             {
  17.                 strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
  18.                 strTryDouble = (null);
  19.                 //strTryDouble = dataIn.readLine();
  20.             try
  21.             {
  22.                 choice = Integer.parseInt (strChoice);
  23.  
  24.                 //if (choice == null) finish ();
  25.                 if (choice == 4) exit ();
  26.  
  27.                 switch (choice)
  28.                 {
  29.                     case 1:
  30.                     JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  31.                     break;
  32.  
  33.                     case 2:
  34.                     tryInt = Integer.parseInt (strChoice);
  35.                     JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  36.                     break;
  37.  
  38.                     case 3:
  39.                     tryDouble = Double.parseDouble (strTryDouble);
  40.                     JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  41.                     break;
  42.  
  43.                     case 4:
  44.  
  45.  
  46.                     default:
  47.                     throw new NumberFormatException();
  48.                     //break;
  49.                 }
  50.             }
  51.             catch (NumberFormatException e)
  52.             {
  53.                 JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
  54.             }
  55.             }
  56.             exit ();
  57.     }
  58.     public static void exit()
  59.     {
  60.         System.exit(0);
  61.     }
  62. }
Feb 10 '07 #1
9 1849
showInputDialog() returns null when you click cancel so just check if strChoice is null before continuing. You also need to call exit() in case 4. The way you have it now because there is no break in case 4 it continues on to the default case and displays the "Wrong!!", "WAAAY OFF!!!" message. You get a NullPointerException when you enter 3 because you set strTryDouble to null then tried to parse a double from it. I'm not sure exactly what you're trying to do but I think you want to parse strChoice instead of strTryDouble on line 34.
Feb 10 '07 #2
Sorry if I sould inexperienced on this but to be completely honest...I am. Now I know you said I need to place exit() in case 4 but when you run the program...if you type in 4 it ends the program so I am pretty sure that part is fine...I just need to add an exit message as I have done in my recent code but it errors out. But again if I click cancel...it just displays the "Wrong!!", "WAAAY OFF!!!" message and starts back over. Also as for the

tryDouble = Double.parseDouble (strTryDouble);

I would think that you would just parse it to strChoice but the assignment says I have to Double.parseDouble it to strTryDouble and I cannot figure out how to get it to work. Thanks for the help so far but any further suggestions? The recent code is as follows:

************************************************** ******************************************

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.swing.JOptionPane;
  3. import java.text.DecimalFormat;
  4.  
  5. public class MyType1
  6. {
  7.     public static void main (String[] args)
  8.     {
  9.         //Declaring Class Variables
  10.         String strChoice, strTryString, strTryInt, strTryDouble;
  11.         int choice, tryInt;
  12.         double tryDouble;
  13.         boolean done = false;
  14.             while (!done)
  15.             //while (!done= null)
  16.             {
  17.                 strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
  18.                 strTryDouble = (null);
  19.             try
  20.             {
  21.                 choice = Integer.parseInt (strChoice);
  22.  
  23.                 //if (choice == null) finish ();
  24.                 if (choice == 4) exit ();
  25.  
  26.                 switch (choice)
  27.                 {
  28.                     case 1:
  29.                     JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  30.                     break;
  31.  
  32.                     case 2:
  33.                     tryInt = Integer.parseInt (strChoice);
  34.                     JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  35.                     break;
  36.  
  37.                     case 3:
  38.                     tryDouble = Double.parseDouble (strTryDouble);
  39.                     JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  40.                     break;
  41.  
  42.                     case 4:
  43.                     JOptionPane.showMessageDialog (null, "Thanks for playing...have a nice day.", JOptionPane.INFORMATION_MESSAGE);
  44.                     break;
  45.  
  46.  
  47.                     default:
  48.                     throw new NumberFormatException();
  49.                     //break;
  50.                 }
  51.             //throw new NumberFormatException();
  52.             }
  53.             catch (NumberFormatException e)
  54.             {
  55.                 JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
  56.             }
  57.             }
  58.             exit ();
  59.     }
  60.     public static void exit()
  61.  
  62.     {
  63.         System.exit(0);
  64.     }
  65. } //end
Feb 12 '07 #3
r035198x
13,262 8TB
Sorry if I sould inexperienced on this but to be completely honest...I am. Now I know you said I need to place exit() in case 4 but when you run the program...if you type in 4 it ends the program so I am pretty sure that part is fine...I just need to add an exit message as I have done in my recent code but it errors out. But again if I click cancel...it just displays the "Wrong!!", "WAAAY OFF!!!" message and starts back over. Also as for the

tryDouble = Double.parseDouble (strTryDouble);

I would think that you would just parse it to strChoice but the assignment says I have to Double.parseDouble it to strTryDouble and I cannot figure out how to get it to work. Thanks for the help so far but any further suggestions? The recent code is as follows:

************************************************** ******************************************

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.swing.JOptionPane;
  3. import java.text.DecimalFormat;
  4.  
  5. public class MyType1
  6. {
  7.     public static void main (String[] args)
  8.     {
  9.         //Declaring Class Variables
  10.         String strChoice, strTryString, strTryInt, strTryDouble;
  11.         int choice, tryInt;
  12.         double tryDouble;
  13.         boolean done = false;
  14.             while (!done)
  15.             //while (!done= null)
  16.             {
  17.                 strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
  18.                 strTryDouble = (null);
  19.             try
  20.             {
  21.                 choice = Integer.parseInt (strChoice);
  22.  
  23.                 //if (choice == null) finish ();
  24.                 if (choice == 4) exit ();
  25.  
  26.                 switch (choice)
  27.                 {
  28.                     case 1:
  29.                     JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  30.                     break;
  31.  
  32.                     case 2:
  33.                     tryInt = Integer.parseInt (strChoice);
  34.                     JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  35.                     break;
  36.  
  37.                     case 3:
  38.                     tryDouble = Double.parseDouble (strTryDouble);
  39.                     JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  40.                     break;
  41.  
  42.                     case 4:
  43.                     JOptionPane.showMessageDialog (null, "Thanks for playing...have a nice day.", JOptionPane.INFORMATION_MESSAGE);
  44.                     break;
  45.  
  46.  
  47.                     default:
  48.                     throw new NumberFormatException();
  49.                     //break;
  50.                 }
  51.             //throw new NumberFormatException();
  52.             }
  53.             catch (NumberFormatException e)
  54.             {
  55.                 JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
  56.             }
  57.             }
  58.             exit ();
  59.     }
  60.     public static void exit()
  61.  
  62.     {
  63.         System.exit(0);
  64.     }
  65. } //end
What is this program supposed to do?
Feb 13 '07 #4
What is this program supposed to do?
It is supposed to open a window with four options. You are supposed to enter a selection 1-4. Upon entering the selections 1-3, you will receive the "CORRECT" response. If you enter anything besides 1-4, you will get the "WRONG" response.

The problem I am running into is the double parsing of case 3. The assignment states I have to parse it to strTryDouble. I am also having a problem with the cancel button. If I click the close "X" or the cancel button, I get the "WRONG" response and it just continues until I enter 4.
Feb 13 '07 #5
r035198x
13,262 8TB
It is supposed to open a window with four options. You are supposed to enter a selection 1-4. Upon entering the selections 1-3, you will receive the "CORRECT" response. If you enter anything besides 1-4, you will get the "WRONG" response.

The problem I am running into is the double parsing of case 3. The assignment states I have to parse it to strTryDouble. I am also having a problem with the cancel button. If I click the close "X" or the cancel button, I get the "WRONG" response and it just continues until I enter 4.
Your explanation does not match what you are doing there. According to you explanation you only need to di Integer.parseInt fo the option entered once. But in your code you are also doing Double.parseDouble for case 3 and another Integer.parseInt for case 2 of the switch. What are those for?
Feb 13 '07 #6
r035198x
13,262 8TB
I strongly suspect, however, that you want the user to be able to enter some input and you test if the input's type matches the previously selected type. Something like this

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import java.io.*;
  4. import javax.swing.JOptionPane;
  5. import java.text.DecimalFormat;
  6.  
  7. public class MyType1 {
  8.  public static void main (String[] args) {
  9.   //Declaring Class Variables
  10.   boolean done = false;
  11.   String strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
  12.   if(strChoice == null || strChoice.equals("4")) {
  13.    done = true;
  14.   }
  15.   while (!done) {
  16.    try {
  17.     int choice = Integer.parseInt (strChoice);
  18.     switch (choice) {
  19.      case 1: {
  20.       String tryString = JOptionPane.showInputDialog (null,"Enter a String");
  21.       JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  22.       break;
  23.      }
  24.      case 2: {
  25.       String tryStrInt = JOptionPane.showInputDialog (null,"Enter an integer");
  26.       int tryInt = Integer.parseInt(tryStrInt);
  27.       JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  28.       break;
  29.      }
  30.      case 3: {
  31.       String tryStrDouble = JOptionPane.showInputDialog (null,"Enter a double");
  32.       double tryDouble = Double.parseDouble(tryStrDouble);
  33.       JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
  34.       break;
  35.      }
  36.      case 4: {
  37.       JOptionPane.showMessageDialog (null, "Thanks for playing...have a nice day.");
  38.       break;
  39.      }
  40.      default: throw new NumberFormatException();
  41.  
  42.     }
  43.    }
  44.    catch (NumberFormatException e) {
  45.     JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
  46.    }
  47.    strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
  48.    if(strChoice == null || strChoice.equals("4")) {
  49.     done = true;
  50.    }
  51.   }
  52.  }
  53. }
  54.  
  55.  
Feb 13 '07 #7
Thanks r035198x...that may do it...I need to check it when I get home where I have a compiler. I know there are easier ways to do thsi but the assignment wants me to use these specific methods. Anyway...I will let you know how it turns out. Thanks again.

-ICEMAN
Feb 13 '07 #8
Got it...thanks for the help...you definitely gave me some ideas.

-ICEMAN
Feb 14 '07 #9
r035198x
13,262 8TB
You are welcome anytime.
Feb 14 '07 #10

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

Similar topics

3
by: tjland | last post by:
This is my first version of this program, im kinda new to python and i was wondering if someone could help me with this. Their are some errors that i just cant find, and i want some advice on how...
7
by: Will | last post by:
Pardon two post in a row to the newsgroup but I want to try and expedite this, if you guys don't mind helping out... I running Windows XP Pro and wanted to download Python and any additional...
38
by: BORT | last post by:
Please forgive me if this is TOO newbie-ish. I am toying with the idea of teaching my ten year old a little about programming. I started my search with something like "best FREE programming...
0
by: Ren | last post by:
Hi I have been ask to try a create a form on my works website to allow teachers to fill it out the click button that emails to the boss I thought asp is what need so i am very new to asp and...
56
by: Kevin Torr | last post by:
I've been using C for some time now and I would like to take my first leap into C++. I should warn you that I have little or no experience with visual C++, though I do have some experience with...
4
by: ef | last post by:
Hi, I'm breaking my head about something, it's probably simple. I have the following peace of code: ======= <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
5
by: dirk | last post by:
Hello, i gave made some code in VBA for Excel. i want to make a webpage with the same examples as my excel file, so i think i will need javascript (serverside?) how do i begin? are there some...
3
by: ballpointpenthief | last post by:
Is this group the correct place to ask this question? Since no-one has answered me as yet I will post it here anyway. = = = = == = ==== = = ===== I'm trying to program sockets in C, very...
3
by: yogi_bear_79 | last post by:
I'm sure I have a few things wrong here. But I am stuck on how to do a recurring search. Also my statement cin >quote; acts weird. If I enter more than one word it blows right past cin >findMe;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.