1

I'm doing a program which has two different questions, ohm and ampere to be exact.

I know how to make this work but the problem is that in the end one more question should pop up asking "do u want to make a new calculation?" this question should work like if I press "Y" the program should continue from the beginning and if I press "N" the program should exit.

I hope anyone got some ideas that could help me finish this. And the program has to be in JOptionpane.dialog/message format.

//delajz

this is what i got atm.

//Declaring Variables

Ampere = Integer.parseInt(FragaAm);
Ohm = Integer.parseInt(FragaOhm);

//koden
do
{
    FragaAm =JOptionPane.showInputDialog("Hur många Ampere? ");
    FragaOhm = JOptionPane.showInputDialog("hur hög resistans? ");
    svar = Ohm*Ampere; //calculate
    slutsvar = svar + "Volt"; //answer
    JOptionPane.showMessageDialog(null,slutsvar);    

    borjaom =JOptionPane.showInputDialog("Igen? J eller N " ); // the question which ask for new calc
} while ( borjaom == "j" ||borjaom == "J" );
7
  • 1
    If you could add some of your code, that might help us determine exactly what you need. Just make sure to indent lines of code by 4 spaces when you post it so that it gets formatted properly on SO. Commented Dec 15, 2009 at 15:03
  • Do you have an embryo of the code or do you wan't someone two write the code for you? Commented Dec 15, 2009 at 15:03
  • Is this homework? And are you sure you're using JavaScript, not Java Swing? Commented Dec 15, 2009 at 15:05
  • Sounds like a mis-categorized homework assignment. JOptionPane is a Java/Swing class. Commented Dec 15, 2009 at 15:05
  • JOptionPane is in the Java API. Are you sure you don't mean Java? Commented Dec 15, 2009 at 15:06

5 Answers 5

3

You can start by looking the Sun article How to Make Dialogs.
It mentions a JOptionPane configured like the one you are looking to make:

alt text

final JOptionPane optionPane = new JOptionPane(
    "The only way to close this dialog is by\n"
    + "pressing one of the following buttons.\n"
    + "Do you understand?",
    JOptionPane.QUESTION_MESSAGE,
    JOptionPane.YES_NO_OPTION);
Sign up to request clarification or add additional context in comments.

2 Comments

The problem calls for pressing "Y" or "N" not a button labeled Yes or No.
yea brandon, from the beginning that was my problem. but it was just a way of explaining that i needed something to call a restart of the program or a exit. this works just as good
2

Try this:

borjaom =JOptionPane.showInputDialog(null, "Igen? J eller N " );

Also, JOptionPane can only be used to input Strings. So all of your variables that you set to receive the input from the JOptionPane should be declared as Strings. Ampere and Ohm should be declared as integers while FragaAm, FragaOhm and borjaom should be declared as Strings.

Comments

2

Your while condition:

} while ( borjaom == "j" ||borjaom == "J" );

assumes strings can be compared on object basis, while this is true in case the strings are interned, it is better to either switch to an enum integer that represents the Yes/No result or use the equals methods from String like:

} while ("J".equalsIgnorecase(borjaom));

(using this format instead of borjaom.equalsIgnorecase("J") has the advantage that it handles the situation where borjoam == null correctly.)

Comments

0

As an alternative to what VonC said, in JOptionPane.dialog/message format:

//Declaring Variables
int borjaom = JOptionPane.NO_OPTION;

// omitted other code, but it should still be here

    // Note that borjaom is an int now
    borjaom = JOptionPane.showConfirmDialog(null, "Igen?", "Title goes here", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE ); // the question which ask for new calc

  } while ( borjaom == JOptionPane.YES_OPTION );

borjaom will be set to one of JOptionPane.NO_OPTION or JOptionPane.YES_OPTION.

Oh, and if you want a title-less dialog (or with the default title), change "Title goes here" to null

Comments

0

This part will not work correctly:

    Ampere = Integer.parseInt(FragaAm);
    Ohm = Integer.parseInt(FragaOhm);

    do
    {
        FragaAm =JOptionPane.showInputDialog("Hur många Ampere? ");
        FragaOhm = JOptionPane.showInputDialog("hur hög resistans? ");

You are converting FragaAm to an integer before reading it.
you must first read it and then convert:

    do
    {
        FragaAm =JOptionPane.showInputDialog("Hur många Ampere? ");
        Ampere = Integer.parseInt(FragaAm);
        FragaOhm = JOptionPane.showInputDialog("hur hög resistans? ");
        Ohm = Integer.parseInt(FragaOhm);

and please have a look at the Code Conventions for the Java Programming Language ,
in your case: variables starts with lowercase (only convention, but used around the whole world)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.