views:

433

answers:

5

im 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" );

}

}

+3  A: 

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);
VonC
The problem calls for pressing "Y" or "N" not a button labeled Yes or No.
Brandon
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
Fredrik
+2  A: 

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.

Brandon
Good remark. +1
VonC
A: 

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

R. Bemrose
working just as intended, yea i changed the title ;)
Fredrik
+2  A: 

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.)

rsp
A: 

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)

Carlos Heuberger