Author Topic: Java Calculation Help Please!  (Read 49 times)

Dr. Tox

  • In Stasis: See You In A Few Years!
  • Subordinate Wasp
  • ***
  • Posts: 145
Java Calculation Help Please!
« on: January 17, 2012, 08:47:20 AM »
I get the error that * operator is not defined but maths operators are supposed to be native, are they not? This program runs fine up until I try to multiply velocity by time to get distance.... In other simple calculator progs I've done none of the maths operators needed to be defined so WTF!??

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class strings {

   /**
    * @param args
    */
   public static void main(String[] args) {
      
    System.out.print("Enter velocity value in Mph and press Enter: ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String velocity = null;
    try {
      velocity = br.readLine();
    } catch (IOException e) {
      System.out.println("Error!");
      System.exit(1);
    }
    System.out.println("velocity is " + velocity);
   
    System.out.print("Enter time value in hours and press Enter: ");
    BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
    String time = null;
    try {
      time = br.readLine();
    } catch (IOException e) {
      System.out.println("Error!");
      System.exit(1);
    }
    System.out.println("time is " + time);
    String distance = (velocity * time);
}
}
Alimentary, dear Watson; I had a gut feeling.

Dr. Tox

  • In Stasis: See You In A Few Years!
  • Subordinate Wasp
  • ***
  • Posts: 145
Re: Java Calculation Help Please!
« Reply #1 on: January 17, 2012, 08:51:46 AM »
I get this message at: String distance = (velocity * time);


Multiple markers at this line
   - The operator * is undefined for the argument
    type(s) java.lang.String, java.lang.String
   - distance cannot be resolved to a variable
   - The operator * is undefined for the argument
    type(s) java.io.BufferedReader, java.io.BufferedReader

I get that distance can't be resolved because I can't tell the fucking thing what distance means until I can use the fucking * operator!
Alimentary, dear Watson; I had a gut feeling.

Vesp

  • Administrator
  • Foundress Queen
  • *****
  • Posts: 3,130
Re: Java Calculation Help Please!
« Reply #2 on: January 17, 2012, 03:13:30 PM »
I don't know much about Java.. just started to learn it... but shouldn't velocity be defined as something other than a string? like an int?

Also... when doing multiplication like that, does it need to have (these) around it?

No idea, literally only have had my java class like, 3 times so far. But I figured I would at least attempt to try to understand this and try to solve it.
Bitcoin address: 1FVrHdXJBr6Z9uhtiQKy4g7c7yHtGKjyLy

Dr. Tox

  • In Stasis: See You In A Few Years!
  • Subordinate Wasp
  • ***
  • Posts: 145
Re: Java Calculation Help Please!
« Reply #3 on: January 17, 2012, 07:41:00 PM »
Yeah, I've tried defining the values and integers and it laughs at me when I do. So I do it this way and it seems to work...

http://ocw.csail.mit.edu/f/1

MIT opencourseware is where I'm practicing from and i'm using eclipse just like they say to and so far so good.

The thing is, I was under the impression that mathematical operators aren't supposed to have to be defined like in C++. I thought they were natively understood by java. I mean, they have been so far. I've done stuff like:

double score = 2.0 * 5.0
System.out.println(score);

<-Run

10.0

There's no definition needed. And as far as the (parentheses) go, well, I was just fucking around with all kinds of combination to get it to work and that's where I gave up. No matter what I do it's not recognizing * as a math operator. Besides, it still recognizes the + operator so what the fuck?

This is not logical!

Now I just tried it in NetBeans 7.1
It says "Bad operand types for binary operator "*".
« Last Edit: January 17, 2012, 08:34:51 PM by Dr. Tox »
Alimentary, dear Watson; I had a gut feeling.

Dr. Tox

  • In Stasis: See You In A Few Years!
  • Subordinate Wasp
  • ***
  • Posts: 145
Re: Java Calculation Help Please!
« Reply #4 on: January 17, 2012, 11:02:55 PM »
Yeah, so, "velocity" & "time" do have to be converted from strings to integers but I'm not sure how to do that with the code here that takes a user inputs and utilizes them to do perform further mathematics functions....

I see this page
http://docs.oracle.com/javase/tutorial/java/data/converting.html

Which doesn't really seem to help me in this instance...

Amending with

    System.out.println("time is " + time);
    double a = velocity;
    double b = time;
    System.out.println(a * b);

Gives the errors: Cannot convert from strings to double

Double.parseDouble(velocity);
Double.parseDouble(time);

Gives the error "Type mismatch: Cannot convert from strings to double"

int velocity = (int) ((Double.parseDouble(velocity.getText)));

Error: -The primitive type int of velocity does not have a field getText
   - Duplicate local variable velocity
« Last Edit: January 17, 2012, 11:45:58 PM by Dr. Tox »
Alimentary, dear Watson; I had a gut feeling.

Vesp

  • Administrator
  • Foundress Queen
  • *****
  • Posts: 3,130
Re: Java Calculation Help Please!
« Reply #5 on: January 18, 2012, 12:10:27 AM »
You'll have to cast them I believe? I don't remember how to do casting.. however. Not totally sure... I know casting can be used for


Wouldn't you do something like this?
 
int velocity = 0;
int time = 0;

int distance = velocity * time;

System.out.println("distance traveled" + distance);


I'm not sure... I honestly do not even understand what it is you are doing? Is there a link to an online homework sort of thing for me to look at as well? I'm not sure... but it doesn't look like you've linked it.
Bitcoin address: 1FVrHdXJBr6Z9uhtiQKy4g7c7yHtGKjyLy

Dr. Tox

  • In Stasis: See You In A Few Years!
  • Subordinate Wasp
  • ***
  • Posts: 145
Re: Java Calculation Help Please!
« Reply #6 on: January 18, 2012, 02:55:13 AM »
Oh, this is just something I wanted to try on my own. I'm not following any guide.

This is just supposed to be a simple distance-velocity-time calculator. I just happened to choose to solve for distance.

Evidently I'm supposed to use something like this and adapt it.

public class StringToNumber {
    public static void main(String[] args) {
            // a string - could have come from user input
            // or from any other source
        String numStr = "3.14159";
         
            // the method used in the tutorial
        double num = (Double.valueOf(numStr)).doubleValue();
        System.out.println("2pi is " + (2 * num)); // multiplication is good
         
            // an alternative (which I linked to)
        double pi = Double.parseDouble(numStr);
        System.out.println("2pi is " + (2 * pi));

It's evidently doing three things according to pbrockway2 over at javaforums: (1) declaring a double variable pi, (2) parsing numStr to get a number from the string and (3) assigning the number to the double variable.

So I'm guessing I should define numStr in my code as the input from the user and then I can perform multiplication on it?
Alimentary, dear Watson; I had a gut feeling.

Dr. Tox

  • In Stasis: See You In A Few Years!
  • Subordinate Wasp
  • ***
  • Posts: 145
Re: Java Calculation Help Please!
« Reply #7 on: January 18, 2012, 03:05:40 AM »
Success!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class strings {

   /**
    * @param args
    */
   public static void main(String[] args) {
      
    System.out.print("Enter velocity value in Mph and press Enter: ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String numStr = null;
    try {
      numStr = br.readLine();
    } catch (IOException e) {
      System.out.println("Error!");
      System.exit(1);
    }
    double num = Double.parseDouble(numStr);
    System.out.println("velocity is " + (1 * num) + " Miles per hour");
   
    System.out.print("Enter time value in hours and press Enter: ");
    BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
    String numStr2 = null;
    try {
      numStr2 = br.readLine();
    } catch (IOException e) {
      System.out.println("Error!");
      System.exit(1);
    }
    System.out.println("time is " + numStr2 + " Hours");
    double num2 = Double.parseDouble(numStr2);
    System.out.println("distance is " + (num * num2) + " Miles");
}
}

OUTPUT:
Enter velocity value in Mph and press Enter: 2
velocity is 2.0 Miles per hour
Enter time value in hours and press Enter: 4
time is 4 Hours
distance is 8.0 Miles
« Last Edit: January 18, 2012, 03:30:37 AM by Dr. Tox »
Alimentary, dear Watson; I had a gut feeling.

n.snostorm

  • Larvae
  • *
  • Posts: 7
Re: Java Calculation Help Please!
« Reply #8 on: January 18, 2012, 02:10:07 PM »
Java is strongly typed language. All conversions between types are done through functions.
And java has two types of types - primitive and object oriented representation of said types. They are not interchangeable or
assignable directly. For example you can't assign value of double to variable of type(actually it's not a type but an object) Double.

When learning java, you really should read into typing system and how to do conversions between types to avoid problems
and misunderstandings later on.