Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

What's wrong with this program


  • Please log in to reply

#1
skrtel_williams

skrtel_williams

    New Member

  • Member
  • Pip
  • 1 posts
Hi everyone
for this program


import javax.swing.JOptionPane;
public class TEMPO
{
public static void main (String args[])
{
int temp;
String tempString;
String contString;
char charAns;

do
{

tempString = JOptionPane.showInputDialog(null, "Enter the temperature:",
"Input",JOptionPane.QUESTION_MESSAGE);
temp = Integer.parseInt(tempString);

if (temp < 10 )
JOptionPane.showMessageDialog(null,"Too Cold", "Output",
JOptionPane.INFORMATION_MESSAGE);

else if (temp >= 10 && temp <= 20)
JOptionPane.showMessageDialog(null,"Average", "Output",
JOptionPane.INFORMATION_MESSAGE);

else if (temp >= 21 && temp <= 30)
JOptionPane.showMessageDialog(null, "Hot", "Output",
JOptionPane.INFORMATION_MESSAGE);

else
JOptionPane.showMessageDialog(null, "Too Hot", "Output",
JOptionPane.INFORMATION_MESSAGE);

contString =
JOptionPane.showInputDialog(null, "Would you like to continue (y/n):",
"Input",JOptionPane.QUESTION_MESSAGE);

charAns = contString.charAt(0);

}while (charAns !='n');

System.exit(0);
}
}

the problem is, when i enter other that 'y', the program accepts it.how to make it only accept 'y' as the answer.

pls help...
tq & rgds
  • 0

Advertisements


#2
coyne20

coyne20

    Member

  • Member
  • PipPip
  • 35 posts
I dont think your no option works whatsoever. Try the following below:

import javax.swing.JOptionPane;
import javax.swing.*;

public class TEMPO
{
//private static final JOptionPane = null;

public static void main (String args[])
{
int temp;
String tempString;
int contString;
char charAns;

int Names;
do
{

tempString = JOptionPane.showInputDialog(null, "Enter the temperature:",
"Input",JOptionPane.QUESTION_MESSAGE);
temp = Integer.parseInt(tempString);

if (temp < 10 )
JOptionPane.showMessageDialog(null,"Too Cold", "Output",
JOptionPane.INFORMATION_MESSAGE);

else if (temp >= 10 && temp <= 20)
JOptionPane.showMessageDialog(null,"Average", "Output",
JOptionPane.INFORMATION_MESSAGE);

else if (temp >= 21 && temp <= 30)
JOptionPane.showMessageDialog(null, "Hot", "Output",
JOptionPane.INFORMATION_MESSAGE);

else
JOptionPane.showMessageDialog(null, "Too Hot", "Output",
JOptionPane.INFORMATION_MESSAGE);

Names = JOptionPane.showOptionDialog(null,
"Would you like to continue?",
"Confirmation",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,null,null,null);

}while(Names != 1);

if(Names == 1)
{
System.exit(0);
}

}

}
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP