Click here to Skip to main content
15,500,741 members
Home / Discussions / Java
   

Java

 
RantRe: Java Pin
ZurdoDev27-Apr-20 2:23
professionalZurdoDev27-Apr-20 2:23 
QuestionAndroid Studio submission Pin
Gammill18-Apr-20 11:34
MemberGammill18-Apr-20 11:34 
AnswerRe: Android Studio submission Pin
Gammill18-Apr-20 14:52
MemberGammill18-Apr-20 14:52 
QuestionIntroduction to object oriented programming Pin
Member 1479832810-Apr-20 7:59
MemberMember 1479832810-Apr-20 7:59 
AnswerRe: Introduction to object oriented programming Pin
Richard MacCutchan10-Apr-20 22:17
mveRichard MacCutchan10-Apr-20 22:17 
Questioncannot resolve Pin
MallardsReach30-Mar-20 4:32
MemberMallardsReach30-Mar-20 4:32 
AnswerRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 4:50
mveRichard MacCutchan30-Mar-20 4:50 
GeneralRe: cannot resolve Pin
MallardsReach30-Mar-20 7:29
MemberMallardsReach30-Mar-20 7:29 
Thanks Richard after many hours of head scratching and internet searching I think I have it.
Below is the class level variables and the method showAnswer.
Java
 // Variables declaration
        double decNum1, decNum2, decAnswer; // class level available to all
        String strAnswer;
   // End of variables declaration

 void showAnswer(int dPlaces){
     Double numAnswer;
     numAnswer = decNum1 * decNum2;
     strAnswer = String.format("%,." + dPlaces + "f", numAnswer);
}


And below is the 3 radiobuttons and the sum button that call the method when clicked.
Java
rbOneDecimal.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           decNum1 = Double.parseDouble(txtNumOne.getText());
           decNum2 = Double.parseDouble(txtNumTwo.getText());
            showAnswer(1); //call method
            lblDPAnswer.setText(strAnswer);
                              }
    });
rbTwoDecimal.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           decNum1 = Double.parseDouble(txtNumOne.getText());
           decNum2 = Double.parseDouble(txtNumTwo.getText());
            showAnswer(2); //call method
            lblDPAnswer.setText(strAnswer);
       }
    });

rbThreeDecimal.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           decNum1 = Double.parseDouble(txtNumOne.getText());
           decNum2 = Double.parseDouble(txtNumTwo.getText());
            showAnswer(3); //call method
            lblDPAnswer.setText(strAnswer);
       }
    });
btnSum.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           decNum1 = Double.parseDouble(txtNumOne.getText());
           decNum2 = Double.parseDouble(txtNumTwo.getText());
            showAnswer(0); //call method
            lblDPAnswer.setText(strAnswer);
           }
    });

Could it be done a better way? I seem to be repeating these 2 lines.
Java
decNum1 = Double.parseDouble(txtNumOne.getText());
decNum2 = Double.parseDouble(txtNumTwo.getText());

GeneralRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 7:45
mveRichard MacCutchan30-Mar-20 7:45 
GeneralRe: cannot resolve Pin
MallardsReach30-Mar-20 11:32
MemberMallardsReach30-Mar-20 11:32 
GeneralRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 22:34
mveRichard MacCutchan30-Mar-20 22:34 
GeneralRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 23:52
mveRichard MacCutchan30-Mar-20 23:52 
GeneralRe: cannot resolve Pin
MallardsReach31-Mar-20 0:22
MemberMallardsReach31-Mar-20 0:22 
QuestionRadio Button Selected Pin
MallardsReach25-Mar-20 10:50
MemberMallardsReach25-Mar-20 10:50 
QuestionRe: Radio Button Selected Pin
ZurdoDev25-Mar-20 11:10
professionalZurdoDev25-Mar-20 11:10 
AnswerRe: Radio Button Selected Pin
MallardsReach25-Mar-20 11:33
MemberMallardsReach25-Mar-20 11:33 
QuestionRe: Radio Button Selected Pin
ZurdoDev25-Mar-20 11:39
professionalZurdoDev25-Mar-20 11:39 
AnswerRe: Radio Button Selected Pin
MallardsReach25-Mar-20 12:11
MemberMallardsReach25-Mar-20 12:11 
QuestionRe: Radio Button Selected Pin
ZurdoDev25-Mar-20 13:19
professionalZurdoDev25-Mar-20 13:19 
AnswerRe: Radio Button Selected Pin
Richard MacCutchan26-Mar-20 1:16
mveRichard MacCutchan26-Mar-20 1:16 
GeneralRe: Radio Button Selected Pin
MallardsReach26-Mar-20 1:28
MemberMallardsReach26-Mar-20 1:28 
GeneralRe: Radio Button Selected Pin
Richard MacCutchan26-Mar-20 1:36
mveRichard MacCutchan26-Mar-20 1:36 
GeneralRe: Radio Button Selected Pin
MallardsReach26-Mar-20 3:55
MemberMallardsReach26-Mar-20 3:55 
GeneralRe: Radio Button Selected Pin
MallardsReach26-Mar-20 5:15
MemberMallardsReach26-Mar-20 5:15 
QuestionUser defined Method Pin
MallardsReach24-Mar-20 3:56
MemberMallardsReach24-Mar-20 3:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.