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.
************************************************** ******************* -
import java.io.*;
-
import javax.swing.JOptionPane;
-
import java.text.DecimalFormat;
-
-
public class MyType1
-
{
-
public static void main (String[] args)
-
{
-
//Declaring Class Variables
-
String strChoice, strTryString, strTryInt, strTryDouble;
-
int choice, tryInt;
-
double tryDouble;
-
boolean done = false;
-
while (!done)
-
//while (!done= null)
-
{
-
strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
-
strTryDouble = (null);
-
//strTryDouble = dataIn.readLine();
-
try
-
{
-
choice = Integer.parseInt (strChoice);
-
-
//if (choice == null) finish ();
-
if (choice == 4) exit ();
-
-
switch (choice)
-
{
-
case 1:
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
case 2:
-
tryInt = Integer.parseInt (strChoice);
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
case 3:
-
tryDouble = Double.parseDouble (strTryDouble);
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
case 4:
-
-
-
default:
-
throw new NumberFormatException();
-
//break;
-
}
-
}
-
catch (NumberFormatException e)
-
{
-
JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
-
}
-
}
-
exit ();
-
}
-
public static void exit()
-
{
-
System.exit(0);
-
}
-
}
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.
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:
************************************************** ****************************************** - import java.io.*;
-
import javax.swing.JOptionPane;
-
import java.text.DecimalFormat;
-
-
public class MyType1
-
{
-
public static void main (String[] args)
-
{
-
//Declaring Class Variables
-
String strChoice, strTryString, strTryInt, strTryDouble;
-
int choice, tryInt;
-
double tryDouble;
-
boolean done = false;
-
while (!done)
-
//while (!done= null)
-
{
-
strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
-
strTryDouble = (null);
-
try
-
{
-
choice = Integer.parseInt (strChoice);
-
-
//if (choice == null) finish ();
-
if (choice == 4) exit ();
-
-
switch (choice)
-
{
-
case 1:
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
case 2:
-
tryInt = Integer.parseInt (strChoice);
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
case 3:
-
tryDouble = Double.parseDouble (strTryDouble);
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
case 4:
-
JOptionPane.showMessageDialog (null, "Thanks for playing...have a nice day.", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
-
default:
-
throw new NumberFormatException();
-
//break;
-
}
-
//throw new NumberFormatException();
-
}
-
catch (NumberFormatException e)
-
{
-
JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
-
}
-
}
-
exit ();
-
}
-
public static void exit()
-
-
{
-
System.exit(0);
-
}
-
} //end
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:
************************************************** ****************************************** - import java.io.*;
-
import javax.swing.JOptionPane;
-
import java.text.DecimalFormat;
-
-
public class MyType1
-
{
-
public static void main (String[] args)
-
{
-
//Declaring Class Variables
-
String strChoice, strTryString, strTryInt, strTryDouble;
-
int choice, tryInt;
-
double tryDouble;
-
boolean done = false;
-
while (!done)
-
//while (!done= null)
-
{
-
strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
-
strTryDouble = (null);
-
try
-
{
-
choice = Integer.parseInt (strChoice);
-
-
//if (choice == null) finish ();
-
if (choice == 4) exit ();
-
-
switch (choice)
-
{
-
case 1:
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
case 2:
-
tryInt = Integer.parseInt (strChoice);
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
case 3:
-
tryDouble = Double.parseDouble (strTryDouble);
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
case 4:
-
JOptionPane.showMessageDialog (null, "Thanks for playing...have a nice day.", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
-
-
default:
-
throw new NumberFormatException();
-
//break;
-
}
-
//throw new NumberFormatException();
-
}
-
catch (NumberFormatException e)
-
{
-
JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
-
}
-
}
-
exit ();
-
}
-
public static void exit()
-
-
{
-
System.exit(0);
-
}
-
} //end
What is this program supposed to do?
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.
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?
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 -
-
-
import java.io.*;
-
import javax.swing.JOptionPane;
-
import java.text.DecimalFormat;
-
-
public class MyType1 {
-
public static void main (String[] args) {
-
//Declaring Class Variables
-
boolean done = false;
-
String strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
-
if(strChoice == null || strChoice.equals("4")) {
-
done = true;
-
}
-
while (!done) {
-
try {
-
int choice = Integer.parseInt (strChoice);
-
switch (choice) {
-
case 1: {
-
String tryString = JOptionPane.showInputDialog (null,"Enter a String");
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
}
-
case 2: {
-
String tryStrInt = JOptionPane.showInputDialog (null,"Enter an integer");
-
int tryInt = Integer.parseInt(tryStrInt);
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
}
-
case 3: {
-
String tryStrDouble = JOptionPane.showInputDialog (null,"Enter a double");
-
double tryDouble = Double.parseDouble(tryStrDouble);
-
JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
-
break;
-
}
-
case 4: {
-
JOptionPane.showMessageDialog (null, "Thanks for playing...have a nice day.");
-
break;
-
}
-
default: throw new NumberFormatException();
-
-
}
-
}
-
catch (NumberFormatException e) {
-
JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
-
}
-
strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
-
if(strChoice == null || strChoice.equals("4")) {
-
done = true;
-
}
-
}
-
}
-
}
-
-
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
Got it...thanks for the help...you definitely gave me some ideas.
-ICEMAN
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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>
|
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...
|
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...
|
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;...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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: 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,...
| |