6 Replies - 429 Views - Last Post: 04 August 2017 - 05:21 AM Rate Topic: -----

#1 Jblaq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-August 17

this is the calculation i'm trying to make (input/750)*6

Posted 04 August 2017 - 02:48 AM

this is the calculation i'm trying to make (input/750)*6
this is the code i wrote to make the calculation,but its not working like i want it to work

[font="Lucida Console"]JButton btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
 
   String r=textFieldInput.getText();
   int u=Integer.parseInt("750");
   String z1 = r / u *6;  /*on this line, i'm getting this error "The operator/is undefined for the argument type(s) String, int" */
   textField.setText(z1);
}



please i need an assistance
thanks.....



Is This A Good Question/Topic? 0
  • +

Replies To: this is the calculation i'm trying to make (input/750)*6

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,410
  • Joined: 20-September 08

Re: this is the calculation i'm trying to make (input/750)*6

Posted 04 August 2017 - 02:53 AM

 String z1 = "" + (r / u * 6);  

Was This Post Helpful? 0
  • +
  • -

#3 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 541
  • View blog
  • Posts: 2,198
  • Joined: 13-June 14

Re: this is the calculation i'm trying to make (input/750)*6

Posted 04 August 2017 - 03:04 AM

It doesn't make sense to divide a string by an integer, does it? So, you need to convert your string to a numeric type so you can do the calculation - a string representing a number is still a string. Note that if you're dividing two integers, you'll lose anything after the decimal point because the result will also be an integer.
Was This Post Helpful? 1
  • +
  • -

#4 IOExceptional  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 122
  • Joined: 15-September 16

Re: this is the calculation i'm trying to make (input/750)*6

Posted 04 August 2017 - 03:14 AM

The problem is that you're performing mathematical operations on Strings that represent integers, not actual integers.


View PostJblaq, on 04 August 2017 - 11:48 AM, said:

   String r=textFieldInput.getText();


This needs to be parsed and stored as an int, like you've done in the next line.


View PostJblaq, on 04 August 2017 - 11:48 AM, said:

   int u=Integer.parseInt("750");


Why are you parsing a String literal? Just use the int value of 750.


View PostJblaq, on 04 August 2017 - 11:48 AM, said:

   String z1 = r / u *6;


As g00se demonstrated, you can convert that to a String but appending an empty String to it. Keep in mind, though, that you run the risk of getting some pretty inaccurate results when dividing integers like that. Depending on your situation, you might want to think about casting r or u to double in your formula.
Was This Post Helpful? 1
  • +
  • -

#5 Jblaq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-August 17

Re: this is the calculation i'm trying to make (input/750)*6

Posted 04 August 2017 - 04:17 AM

I appreciate your reply warmly, but
the point is that i'm pretty new with java. from my own understanding, iv'e done what you said, the error has disappeared, but the calculated result is still not correct, and its not even close.
so please, can you show me some sample of what you mean, or how i can do it.
Thanks G00se....
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,410
  • Joined: 20-September 08

Re: this is the calculation i'm trying to make (input/750)*6

Posted 04 August 2017 - 04:34 AM

As was said earlier, use

double u = 750.0;


You need to research how integer division works in Java
Was This Post Helpful? 0
  • +
  • -

#7 IOExceptional  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 122
  • Joined: 15-September 16

Re: this is the calculation i'm trying to make (input/750)*6

Posted 04 August 2017 - 05:21 AM

Could you show us the code you have now, so that we may give you advice on how to fix it?

I can assume, though, the problem comes from dividing integers the way you are. Consider this line:

(1 / 2) * 2


Mathematically, this would equate to 1. However, printing it in Java would give you 0. This is because, in Java, the result of (1 / 2) -- two ints -- is an int, and ints aren't stored with a decimal point. Thus, 0.5 becomes 0, which, multiplied by 2, gives a final result of 0.

To avoid this problem, you can make at least one of the operands a double or a float.

(1.0 / 2) * 2

(1 / 2.0) * 2

(1.0 / 2.0) * 2

All of the above would give the mathematically correct result of 1.

In your case, you can store either r or u as a double, or explicitly cast one of them when calculating z1.

((double)r / u)


I should also mention that there are much better sources on integer division than me. My explanation was just to serve as a basic introduction. I suggest you do some further research on the topic once you get your programme working (or before).

This post has been edited by IOExceptional: 04 August 2017 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1