JDivide Java Program

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1415 Views - Last Post: 14 August 2010 - 11:55 AM Rate Topic: -----

#1 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

JDivide Java Program

Posted 14 August 2010 - 08:39 AM

I am having a problem with this program.
I get this error:
JDivide.java:40: incompatible types
found: java.lang.string
required: int
int FieldOne = firstNumField.getText();

JDivide.java:40: incompatible types
found: java.lang.string
required: int
int FieldTwo = secondNumField.getText();
2 Errors.

The program suppose to display corresponding labels for display and textfields for input.
The program is to divide two numbers, if the number is divided by zero a message suppose to invoke.
This how far i got with the program so far.

I appreciate for your assistance.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JDivide extends JApplet implements ActionListener
{
	Container con = getContentPane();
	JLabel greeting = new JLabel("Welcome to JDivide!");
	Font bigFont = new Font("Times Roman", Font.ITALIC, 24);
	JLabel firstNum = new JLabel("Please enter your first number:");
	JLabel secondNum = new JLabel("Please enter your second number:");
	JLabel answer = new JLabel("");
	JTextField firstNumField = new JTextField("", 10);
	JTextField secondNumField = new JTextField("", 10);
	JButton calculate = new JButton("Calcualate");
        
        //layout 
	FlowLayout flow = new FlowLayout();

        //initialize fields and labels
	public void init()
	{
	   greeting.setFont(bigFont);	
	   con.add(greeting);
	   con.setLayout(flow);
	   con.add(firstNum);
	   con.add(firstNumField);
	   con.add(secondNum);
           con.add(secondNumField);
	   con.add(calculate);
	   con.add(answer);
	   calculate.addActionListener(this);
	   firstNum.requestFocus();
	
	}
	
	//action to be performed
	public void actionPerformed(ActionEvent e){
        
        int FieldOne = firstNumField.getText();
        int FieldTwo = secondNumField.getText();
        int solution = (FieldOne / FieldTwo);
        answer.setText("The solution is : " + solution);
        answer.setFont(bigFont);
        
    }
}



This is the html file

<html>
<object code = "JDivide.class" width = 450 height = 200>
</object>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: JDivide Java Program

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: JDivide Java Program

Posted 14 August 2010 - 08:50 AM

The getText() method returns a String. You need to parse it to an int if you want to assign it to an int variable.

Also, not really good practice to mess with the ContentPane. Use a Container like JPanel to organize your GUI, then add that to your JApplet. Let your top-level Container worry about dealing with the ContentPane, as it is an AWT Container.
Was This Post Helpful? 1
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: JDivide Java Program

Posted 14 August 2010 - 09:06 AM

In Java, if you divide by 0, you get infinity :)

As Mac said, need to parse your input:
fieldOne = Integer.parseInt(firstNumField.getText().trim());



Also, ALL your variables should start with lowercase. That is Java coding convention, makes it much more readable. So fix those two ints with uppercase :D
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: JDivide Java Program

Posted 14 August 2010 - 09:18 AM

Late to the party again! This is what you get for making a pot of coffee


int FieldOne = Integer.parseInt(firstNumField.getText());


Was This Post Helpful? 0
  • +
  • -

#5 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JDivide Java Program

Posted 14 August 2010 - 09:49 AM

View Postmacosxnerd101, on 14 August 2010 - 07:50 AM, said:

The getText() method returns a String. You need to parse it to an int if you want to assign it to an int variable.

Also, not really good practice to mess with the ContentPane. Use a Container like JPanel to organize your GUI, then add that to your JApplet. Let your top-level Container worry about dealing with the ContentPane, as it is an AWT Container.


Wow everyone hit on this post LOL
thanks for helping me out. I also agree in using the JPanel but I am in the intermediate class.
Ok you did mentioned about dividing by zero how can I implement that it is something like this?

If 
{
FieldTwo / FieldOne 
        answer.setText = (Cannot divide by zero")
}


Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: JDivide Java Program

Posted 14 August 2010 - 09:51 AM

if(FieldOne != 0){
    ans =  FieldTwo / FieldOne;
}
else{
    //throw an error or w/e
}


Was This Post Helpful? 0
  • +
  • -

#7 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JDivide Java Program

Posted 14 August 2010 - 10:03 AM

View PostKYA, on 14 August 2010 - 08:51 AM, said:

if(FieldOne != 0){
    ans =  FieldTwo / FieldOne;
}
else{
    //throw an error or w/e
}



I get an error:

JDivide.java:48 incompatible types
found: int
required: java.swing.JLabel
.swing.JLabel
answer = FieldTwo / FieldOne;
^

//action to be performed
	public void actionPerformed(ActionEvent e){
        
        int FieldOne = Integer.parseInt(firstNumField.getText().trim());
        int FieldTwo = Integer.parseInt(secondNumField.getText().trim());
        int solution = (FieldOne / FieldTwo);
        answer.setText("The solution is : " + solution);
        answer.setFont(bigFont);
        
	if(FieldOne != 0)
	{
    		answer =  FieldTwo / FieldOne;
	}
	else{
    		//throw an error or w/e
            }
}


This post has been edited by bravo659: 14 August 2010 - 10:04 AM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JDivide Java Program

Posted 14 August 2010 - 10:13 AM

answer is a JLabel you cannot assign to it an int which is the result of your division
you will have to

int ans - FiledTwo / FieldOne;
answr.setText("" + ans);
Was This Post Helpful? 0
  • +
  • -

#9 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JDivide Java Program

Posted 14 August 2010 - 10:25 AM

View Postpbl, on 14 August 2010 - 09:13 AM, said:

answer is a JLabel you cannot assign to it an int which is the result of your division
you will have to

int ans - FiledTwo / FieldOne;
answr.setText("" + ans);


this is what i have now i need to throw an exception if user try to divide by zero
I still get the solution to be 0

//action to be performed
	public void actionPerformed(ActionEvent e){
        
        int FieldOne = Integer.parseInt(firstNumField.getText().trim());
        int FieldTwo = Integer.parseInt(secondNumField.getText().trim());
        int solution = (FieldOne / FieldTwo);
        answer.setText("The solution is : " + solution);
        answer.setFont(bigFont);
        
	if(FieldOne != 0)
	{
    		int answer =  FieldTwo / FieldOne;
		
	}
	

	
    }


Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: JDivide Java Program

Posted 14 August 2010 - 10:30 AM

Not really. The conditional eliminated the possibility of diving by zero. If you wanted to inform the user that it isn't allowed add an else block to the if and put the message there.
Was This Post Helpful? 0
  • +
  • -

#11 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JDivide Java Program

Posted 14 August 2010 - 10:33 AM

View PostKYA, on 14 August 2010 - 09:30 AM, said:

Not really. The conditional eliminated the possibility of diving by zero. If you wanted to inform the user that it isn't allowed add an else block to the if and put the message there.


so the message is like this?
if(FieldOne != 0)
	{
    		int answer =  FieldTwo / FieldOne;
		
	}
        else
        {
          messagebox.show("Error...cannot divide by zero");
        }


Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: JDivide Java Program

Posted 14 August 2010 - 10:35 AM

Yes. It can be to the console or a component, your choice.
Was This Post Helpful? 0
  • +
  • -

#13 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JDivide Java Program

Posted 14 August 2010 - 11:50 AM

View PostKYA, on 14 August 2010 - 09:35 AM, said:

Yes. It can be to the console or a component, your choice.


I do it that way it gives me an error.
cannot do it messagebox.show.

do you know a way to invoke this message to the user?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: JDivide Java Program

Posted 14 August 2010 - 11:51 AM

Don't you mean to use a JOptionPane? Take a look at some of the methods in the JOptionPane class to use.
Was This Post Helpful? 0
  • +
  • -

#15 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: JDivide Java Program

Posted 14 August 2010 - 11:52 AM

In console, you use System.out.print()..

In GUI, you do it with a JOptionPane:
// parameters (parent,message,title,type)
JOptionPane.showMessageDialog(this,"Dividing by 0!","ERROR",JOptionPane.ERROR_MESSAGE);


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2