Jump to content

Using varibles to calculate

Angy

Hello,

I am doing some coding for a project in Java.

and I have multiple variables, is there an easy way to test if a variable is not entered into a JTextField, and calculate it?

 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

i havent worked with Java and JTextFIelds in months, but i believe when nothing is entered, calling the getText() method will either return an empty string or null. so just have a test

 

String tField = textField.getText();
if(textField.getText() == null || tField.equals("")) {
	//do your computations
} else if(//next text field test, etc.) {
}

i may be completely wrong. 

Rigs I've Built

The Striker i5 4590 @ 3.7 ||  MSI GTX 980 Armor X2 || Corsair RMX 750 || Team Elite Plus 8 GB || Define S || MSI Z97S SLI Krait

The Office PC i3 4160 @ 3.6 || Intel 4600 || EVGA 500B || G.Skill 8 GB || Cooler Master N200 || ASRock H97M Pro4

The Friend PC G3258 @ 4.3 || Sapphire R9 280X Tri-X || EVGA 600B || 8 GB Dell Ram || Cooler Master N200 || ASRock H97M- iTX/ac

The Mom Gaming PC A10-7890K @ 4.4 || iGPU + ASUS R7 250 ||  8 GB Klevv DDR3-2800 Mhz

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

This is not working  

 

 

 

ActionListener actionListener = new ActionListener() { 

        public void actionPerformed(ActionEvent e) {

        if (window == BASICINTEREST);      // basic interest calculator   

        Pval = Double.parseDouble(card1P.getText());  // these retrieve the values out of the JTextFields 

            Rval = Double.parseDouble(card1R.getText());

            Tval = Double.parseDouble(card1T.getText());

            Ival = Double.parseDouble(card1I.getText()); 

            Rval = Rval/100;

            

            ans =Double.parseDouble(new DecimalFormat("##.####").format(ans));  // formats the output to be 3 decimal places

           

            

            if(card1I.getText() == null || card1I.equals(""));     // tests if The I value is unknown and so on

            ans =  Pval * Rval * Tval; 

            answerTxt.setText(String.valueOf(new Double(ans)));

            

            if(card1P.getText() == null || card1P.equals("")); 

            ans = Ival/(Rval * Tval); 

            answerTxt.setText(String.valueOf(new Double(ans)));

            

            if(card1R.getText() == null || card1R.equals("")); 

            ans = Ival/(Pval * Tval);

            answerTxt.setText(String.valueOf(new Double(ans)));

            

            if(card1T.getText() == null || card1T.equals("")); 

            ans = Ival/(Pval * Rval);

            answerTxt.setText(String.valueOf(new Double(ans)));

            }                      

    }; 

    calculate.addActionListener(actionListener);

       

Link to comment
Share on other sites

Link to post
Share on other sites

@Angy please you [ code] tags.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

ActionListener actionListener = new ActionListener() { 
        public void actionPerformed(ActionEvent e) {
        if (window == BASICINTEREST);      // basic interest calculator   
        Pval = Double.parseDouble(card1P.getText());  // these retrieve the values out of the JTextFields 
            Rval = Double.parseDouble(card1R.getText());
            Tval = Double.parseDouble(card1T.getText());
            Ival = Double.parseDouble(card1I.getText()); 
            Rval = Rval/100;
            
            ans =Double.parseDouble(new DecimalFormat("##.####").format(ans));  // formats the output to be 3 decimal places
           
            
            if(card1I.getText() == null || card1I.equals(""));     // tests if The I value is unknown and so on
            ans =  Pval * Rval * Tval; 
            answerTxt.setText(String.valueOf(new Double(ans)));
            
            if(card1P.getText() == null || card1P.equals("")); 
            ans = Ival/(Rval * Tval); 
            answerTxt.setText(String.valueOf(new Double(ans)));
            
            if(card1R.getText() == null || card1R.equals("")); 
            ans = Ival/(Pval * Tval);
            answerTxt.setText(String.valueOf(new Double(ans)));
            
            if(card1T.getText() == null || card1T.equals("")); 
            ans = Ival/(Pval * Rval);
            answerTxt.setText(String.valueOf(new Double(ans)));
            }                      
    }; 
    calculate.addActionListener(actionListener);

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Angy said:

ActionListener actionListener = new ActionListener() { 
        public void actionPerformed(ActionEvent e) {
        if (window == BASICINTEREST);      // basic interest calculator   
        Pval = Double.parseDouble(card1P.getText());  // these retrieve the values out of the JTextFields 
            Rval = Double.parseDouble(card1R.getText());
            Tval = Double.parseDouble(card1T.getText());
            Ival = Double.parseDouble(card1I.getText()); 
            Rval = Rval/100;
            
            ans =Double.parseDouble(new DecimalFormat("##.####").format(ans));  // formats the output to be 3 decimal places
           
            
            if(card1I.getText() == null || card1I.equals(""));     // tests if The I value is unknown and so on
            ans =  Pval * Rval * Tval; 
            answerTxt.setText(String.valueOf(new Double(ans)));
            
            if(card1P.getText() == null || card1P.equals("")); 
            ans = Ival/(Rval * Tval); 
            answerTxt.setText(String.valueOf(new Double(ans)));
            
            if(card1R.getText() == null || card1R.equals("")); 
            ans = Ival/(Pval * Tval);
            answerTxt.setText(String.valueOf(new Double(ans)));
            
            if(card1T.getText() == null || card1T.equals("")); 
            ans = Ival/(Pval * Rval);
            answerTxt.setText(String.valueOf(new Double(ans)));
            }                      
    }; 
    calculate.addActionListener(actionListener);

 

you're asking if the varible is empty then do your code. You need to ask if it is not empty

 

if i != null

run code.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×