Click here to Skip to main content
15,888,984 members
Home / Discussions / Java
   

Java

 
GeneralRe: What type of check is made in an ArrayList generic? Pin
Afzaal Ahmad Zeeshan22-Feb-15 8:47
professionalAfzaal Ahmad Zeeshan22-Feb-15 8:47 
AnswerRe: What type of check is made in an ArrayList generic? Pin
jschell23-Feb-15 10:21
jschell23-Feb-15 10:21 
GeneralRe: What type of check is made in an ArrayList generic? Pin
Afzaal Ahmad Zeeshan23-Feb-15 20:26
professionalAfzaal Ahmad Zeeshan23-Feb-15 20:26 
GeneralRe: What type of check is made in an ArrayList generic? Pin
jschell26-Feb-15 9:50
jschell26-Feb-15 9:50 
Questionproject Pin
Member 1146518219-Feb-15 10:35
Member 1146518219-Feb-15 10:35 
AnswerRe: project Pin
Richard Andrew x6419-Feb-15 12:25
professionalRichard Andrew x6419-Feb-15 12:25 
AnswerRe: project Pin
Afzaal Ahmad Zeeshan22-Feb-15 6:55
professionalAfzaal Ahmad Zeeshan22-Feb-15 6:55 
Questionwhy calculation(BMI) is not working.... please help me... Pin
Member 1141203417-Feb-15 9:41
Member 1141203417-Feb-15 9:41 
Greetings, everything in this code works except when the i input the weight and height the result is not shown in the textbox (v_text).. please help me

Java
package bmicalculator;


import static com.sun.corba.se.impl.orbutil.CorbaResourceUtil.getText;
import java.lang.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import java.awt.Container;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;


public class BMICalculator extends JFrame implements ActionListener, PropertyChangeListener
{
    
    /**
     * Set up my GUI
     * 
     */
    JPanel p_format;
    JPanel p_text;
    JLabel f_name;
    JLabel height;
    JLabel weight;
    
    JTextField n_text;
    JFormattedTextField h_text;
    JFormattedTextField w_text;
    JFormattedTextField v_text;
    JTextField r_text;
    JTextArea a_text;
    
    JButton v_bmi;
    JButton result;
    JButton r_done;
    JButton r_save;
    JButton r_reset;
    JButton f_open;
    JFileChooser chooser = new JFileChooser();
    
    String Height, Weight;
    double BMI;
    static String output = "Results";
    static int jopIcon = JOptionPane.QUESTION_MESSAGE;
    boolean bFlag = true; //state, true means no exception
    
    Font font;
    static int f_size = 16; 
    static int style = Font.PLAIN;
    static String font_name = " Harrington ";
    
    double number1;
    double number2;
   
    
    public BMICalculator (String s)
    {
        super(s);
		font = new Font(font_name, style, f_size);
		getContentPane().setLayout(new GridLayout(1,2));
		p_format = new JPanel();
		p_text = new JPanel();
                
                
                //First Panel////////////////////////////////////
                
                p_text.setLayout(null);
                
                f_name = new JLabel(" Name: ");
                f_name.setSize(120,60);
                f_name.setLocation(30, 30);
                f_name.setForeground(Color.DARK_GRAY);
                
                n_text = new JTextField(40);
		n_text.setSize(120,60);
		n_text.setLocation(70,30);
                
                height = new JLabel(" Height(meter) : ");
                height.setSize(90, 60);
                height.setLocation(30,60 );
                height.setForeground(Color.RED);
                
                h_text = new JFormattedTextField();
                h_text.setColumns(10);
                h_text.setLocation(70, 60);
                h_text.setValue(new Double(number1));
                h_text.addPropertyChangeListener("value", this);
                
                weight = new JLabel(" Weight(kilogram): ");
                weight.setSize(90, 60);
                weight.setLocation(30, 90);
                weight.setForeground(Color.RED);
                
                w_text = new JFormattedTextField( );
                w_text.setColumns(10);
                w_text.setLocation(70, 90);
                w_text.setValue(new Double(number2));
                w_text.addPropertyChangeListener("value", this);
                
                
                v_bmi = new JButton("Your BMI: ");
                v_bmi.setSize(90, 60);
                v_bmi.setLocation(30,190);
                v_bmi.setForeground(Color.BLUE);
                v_bmi.addPropertyChangeListener("value", this);
                v_bmi.addActionListener(this);
                
                v_text = new JFormattedTextField( );
                v_text.setColumns(10);
                v_text.setLocation(70, 190);
                v_text.setValue(BMI);
                v_text.addPropertyChangeListener("value", this);
                
                result = new JButton("Your level: ");
                result.setSize(60, 60);
                result.setLocation(30,250);
                result.setForeground(Color.BLUE);
                result.addActionListener(this);
                
                r_text = new JTextField(45);
                r_text.setSize(90, 60);
                r_text.setLocation(70, 250);
                r_text.addActionListener(this);
                
                a_text = new JTextArea(30,45);
		a_text.setSize(200,200);
		a_text.setLocation(30, 300);
                
                r_done = new JButton(" Done! ");
		r_done.setSize(120,30);
		r_done.setLocation(30, 350);
                r_done.setForeground(Color.BLACK);
                r_done.addActionListener(this);
                
                r_save = new JButton(" Save ");
		r_save.setSize(120,30);
		r_save.setLocation(60, 350);
                r_save.setForeground(Color.MAGENTA);
                r_save.addActionListener(this);

		r_reset = new JButton(" Reset ");
		r_reset.setSize(120,30);
		r_reset.setLocation(90, 350);
                r_reset.setForeground(Color.RED);
                r_reset.addActionListener(this);
                
                f_open = new JButton(" Open ");
		f_open.setSize(120,30);
		f_open.setLocation(120, 350);
                f_open.setForeground(Color.BLUE);
                f_open.addActionListener(this);
                
                JScrollPane textScrollPane=new JScrollPane(a_text);
                textScrollPane.setSize(300,300);
		textScrollPane.setLocation(30,300);
                
                p_format.setBackground(Color.PINK);
                p_format.add(f_name);
                p_format.add(n_text);
                p_format.add(height);
                p_format.add(h_text);
                p_format.add(weight);
                p_format.add(w_text);
                p_format.add(v_bmi);
                p_format.add(v_text);
                p_format.add(result);
                p_format.add(r_text);
                p_format.add(a_text);
                p_format.add(r_done);
                p_format.add(r_save);
                p_format.add(r_reset);
                p_format.add(f_open);
                p_format.add(textScrollPane);
                
                
                getContentPane().add(p_format);
              
    }
    
    public static void main(String args[])
	{
		BMICalculator k=new BMICalculator("SHARIMAH SAKKA Project");
		k.setSize(550, 700);
		k.show();
		k.setVisible(true);
                k.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
    
    
    
    void BMIResult()
    {
            Height = height.getText();
            Weight = weight.getText();//declare the Height string with Jtext height
            
             number1 = Double.parseDouble(Height);
             number2 = Double.parseDouble(Weight);//exception may come out

             BMICalculate(number1, number2);
          
            
            
            //if (BMI >= 29) 
               // {
                 //   output += "\n\n High Risk zone(UnHealthy). Please start losing weight! It's a MUST!!!";
                //} 
            //else if (BMI <= 25 || BMI < 28.9) 
                //{
                    //output += "\n\n Moderate Risk zone. Please start going on diet and lose some weight!";
                //} 
            //else if (BMI <= 18.5 || BMI < 24.9) 
                //{
                    //output += "\n\n Low Risk zone(Healthy). Hopefully you can maintain this way! ^^";
                //} 
            //else if (BMI < 18.4) 
                //{
                    //output += "\n\n Need to start eating more. Too skinny and unhealthy for your body :'(";
                //}
     }
    
    public void BMICalculate(double number1, double number2)
        {
            try 
            {
                number1 = (double) ((Number)h_text.getValue());
                number2 = (double) ((Number)w_text.getValue());
                BMI = (number2 / ((number1) * (number1)));
            } 
            catch (NumberFormatException nfe) 
            {
                output += "\n\n Whoa! Input error: must enter valid integers";//if exception comes out, prepare error message
                jopIcon = JOptionPane.ERROR_MESSAGE;
            }
        }
     
    void saveToFile()
        {
            if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) 
            {
                File saveFile = chooser.getSelectedFile();
                    try 
                        {
                            PrintWriter out = new PrintWriter (new FileWriter(saveFile));
                            out.print (a_text.getText());
                            out.close();
			} 
                    catch (IOException e) 
                        {
			// Happens if the file cannot be written to for any reason, such as:
			//    - the file might already exist
			//    - the disk might be full
			//    - the user might not have write permission in the directory
                            JOptionPane.showMessageDialog(null, "Could not save the file " + e.getMessage());
			}
            }
        }
    void loadFromFile() 
        {
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
            {
		File loadFile = chooser.getSelectedFile();
                    try 
                        {
                            BufferedReader in = new BufferedReader (new FileReader(loadFile));
                            a_text.setText("");
                            String nextLine = in.readLine();
                            while (nextLine != null) 
                                {
                                    a_text.append(nextLine + "\n");
                                    nextLine = in.readLine();
				}
                            in.close();
                        }
                    catch (IOException ie) 
                        {
                            JOptionPane.showMessageDialog(null, "Could not load the file " + ie.getMessage());
                        }
            }
         }
    
    

    @Override
    public void actionPerformed(ActionEvent x) 
    {
        
        if(x.getSource()==v_bmi)
            {
                //BMICalculate();
                v_text.setValue(new Double(BMI));
                 
            }
         if(x.getSource()==result)
            {
              //  BMILevel();
                r_text.getText();
                
                  if (BMI >= 29) 
                {
                    r_text.setText(" High Risk zone(UnHealthy).Please start losing weight!It's a MUST!!! ");
                } 
            else if (BMI <= 25 || BMI < 28.9) 
                {
                    r_text.setText(" Moderate Risk zone.Please start going on diet and lose some weight! ");
                } 
            else if (BMI <= 18.5 || BMI < 24.9) 
                {
                    r_text.setText(" Low Risk zone(Healthy).Hopefully you can maintain this way! ");
                } 
            else if (BMI < 18.4) 
                {
                    r_text.setText(" Need to start eating more. Too skinny and unhealthy for your body. ");
                }
            }
        if(x.getSource()==r_done)
            {
                String s= "Name: " + n_text.getText()+ "\n"+"Height: " + h_text.getText()+"\n"+"Weight: " + w_text.getText()+"\n"+"BMI: " + v_text.getText()+"\n"+"Result: " + r_text.getText();
                a_text.setText(s);
            }
        
        if(x.getSource()==r_reset)
            {
                String s=n_text.getText() +h_text.getText()+w_text.getText()+ v_text.getText()+r_text.getText();
                n_text.setText(null);
                h_text.setText(null);
                w_text.setText(null);
                v_text.setText(null);
                r_text.setText(null);
                a_text.setText(null);
            } 
        if(x.getSource()==r_save)
            {
                saveToFile();
            }
        
        if(x.getSource()== f_open)
            {
                 loadFromFile();
            } 
        
    }

    @Override
    public void propertyChange(PropertyChangeEvent x) 
    {
        Object source = x.getSource();
        
        if(source==v_bmi)
            {
                String b;
                b = Double.valueOf(BMI).toString();
                v_text.setValue(new Double(b));
                
            }
    }
    
    
}

GeneralRe: why calculation(BMI) is not working.... please help me... Pin
harold aptroot17-Feb-15 10:41
harold aptroot17-Feb-15 10:41 
Questionjava Pin
Member 1145378615-Feb-15 5:48
Member 1145378615-Feb-15 5:48 
AnswerRe: java Pin
Richard MacCutchan15-Feb-15 6:11
mveRichard MacCutchan15-Feb-15 6:11 
AnswerRe: java Pin
Richard Andrew x6419-Feb-15 12:26
professionalRichard Andrew x6419-Feb-15 12:26 
Question[SendingMultipartDataPostRequest] Issues with Sending a Post Request with Multi-part form data Pin
B.Sudhir14-Feb-15 12:27
B.Sudhir14-Feb-15 12:27 
Questionwhy java is called as architectural neutral? Pin
Member 1144945112-Feb-15 22:31
Member 1144945112-Feb-15 22:31 
AnswerRe: why java is called as architectural neutral? Pin
Richard MacCutchan12-Feb-15 22:36
mveRichard MacCutchan12-Feb-15 22:36 
Questionjava netbeans Pin
Member 1144139511-Feb-15 3:23
Member 1144139511-Feb-15 3:23 
QuestionRe: java netbeans Pin
Richard MacCutchan11-Feb-15 21:44
mveRichard MacCutchan11-Feb-15 21:44 
AnswerRe: webbase chat program Pin
Member 1144139512-Feb-15 0:54
Member 1144139512-Feb-15 0:54 
GeneralRe: webbase chat program Pin
Richard MacCutchan12-Feb-15 1:02
mveRichard MacCutchan12-Feb-15 1:02 
AnswerRe: java netbeans Pin
Member 1144139513-Feb-15 4:15
Member 1144139513-Feb-15 4:15 
GeneralRe: java netbeans Pin
Richard MacCutchan13-Feb-15 4:55
mveRichard MacCutchan13-Feb-15 4:55 
QuestionSelenium WebDriver Pin
Member 114387579-Feb-15 1:55
Member 114387579-Feb-15 1:55 
QuestionRe: Selenium WebDriver Pin
Richard MacCutchan9-Feb-15 2:48
mveRichard MacCutchan9-Feb-15 2:48 
QuestionNullPointerException in java Pin
Member 114349697-Feb-15 6:03
Member 114349697-Feb-15 6:03 
AnswerRe: NullPointerException in java Pin
Richard MacCutchan7-Feb-15 22:11
mveRichard MacCutchan7-Feb-15 22:11 

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.