Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Problem With * Operator

    Hello,

    For some reason, on the last line here, the * operator evidently needs to be defined but + is still recognized and I haven't defined that...
    String distance = (velocity * time); I've tried defining distance as an integer and I've tried all kinds of different syntax to multiply velocity by time and it just will not let me do it without defining *. Aren't maths operators supposed to be natively understood?

    Thank you,

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class Distance {
    
        /**
         * @param args the command line arguments
         */
        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);
        }
    }
    Last edited by Norm; 01-17-2012 at 10:00 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Problem With * Operator

    What error are you getting?

    Java Code:
     String distance = (velocity * time);
    The assignment statement has a String on the left of the = and an int expression on the right.
    If you want the int expression converted to a String you will need to tell the compiler how you want it done.
    One way is to enclose the whole int expression in "s.
    Another way is to use the String class's valueOf method.

  3. #3
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem With * Operator

    In Eclipse 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

    In NetBeans7.1 I get: "Bad operand types for binary operator "*"

    I've tried to get distance to display as an integer by trying to define it as such and any number of any other syntax combinations and nothing seems to help it understand *.
    Last edited by misant; 01-17-2012 at 10:22 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Problem With * Operator

    There is no * operator for Strings. The *operator is for numeric variables and values.
    The + operator has been extended for use with Strings as a concatenation operator.

    You need to convert the Strings to numbers and THEN you can use the * operator.
    See the Integer class for a method to convert a String to an int value

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem With * Operator

    I've tried to get distance to display as an integer by trying to define it as such
    The problem is not with distance (or how it is displayed). You have declared time and velocity as strings, and you can't multiply strings.

    Check out Oracle's Tutorial page on Converting Between Numbers and Strings. The example they give there needs a little comment: first float or Float is not often used. People prefer double or Double (or int or Integer depending on context). Secondly you might want to check out the Double method parseDouble() as an alternative to the valueOf() method they use in the example.

  6. #6
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem With * Operator

    Ahhh, ok. Thank you Norm.

    I know this is probably spoon feeding and I am a total n00b (just started last night) but is there any chance you could help me to understand how to convert a string into an integer?

    Thank you for your time & assistance!

    Mike

    Edit: Just noticed that last post.

    Thank you.

  7. #7
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem With * Operator

    So, am I understanding this correctly?

    float a = (Float.valueOf(args[velocity])).floatValue();
    float b = (Float.valueOf(args[time])).floatValue();
    System.out.println("a * b = " + (a * b));

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Problem With * Operator

    What happened when you tried that?

    What is the array notation: args[...]?
    Where did that come from?
    Last edited by Norm; 01-17-2012 at 10:59 PM.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem With * Operator

    Where did args[velocity] come from?!

    In that example they took the strings from the command line (which is what args[] is), but in your case you already have the strings as variables. If you are going to convert it makes sense to name the variables so you can see what corresponds to what.

    See if this makes any sense:

    Java Code:
    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));
        }
    }

  10. #10
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem With * Operator

    incompatible types. required: int

    That came from the linked page from pbrockway
    Converting Between Numbers and Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem With * Operator

    Usually the Tutorial explanations are good, but the one they give there is a bit odd. Sorry about that.

    Hence the example I posted to try and clarify, but don't imagine you can use it blindly. Understand every line before doing something similar in your code. There aren't a lot of them and the important one - parseDouble() - I have already linked to somewhere above. One day into Java is about right for having the API documentation open on your desktop and consulting it...

  12. #12
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem With * Operator

    Thank you guys,

    I can see what the code above is doing and while i can say that "it makes sense" in my reading of it, I couldn't whip it out from scratch....

    Also, I'm probably being a dolt, but I can't seem to see how exactly I can apply this to the code I've already got. For some reason this is eluding me. I'm not usually stupid in most areas, but I last coded anything over 10 years ago in C++ class and I hated every minute of it and retained nothing. :p

    At this point in time however I'm actually interested in learning a programming language and it's frustrating that finally, when my will is present, my capability seems to be off partying somewhere else...

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem With * Operator

    but I can't seem to see how exactly I can apply this to the code I've already got
    The problem is that you have two strings and you want to multiply them. The solution is to (A) make two double values from the strings and (B) multiply them instead.

    Your code:

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class Distance {
     
        /**
         * @param args the command line arguments
         */
        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);
            // (A) make two double values
            //String distance = (velocity * time);  // (B) multiply them instead
        }
    }

  14. #14
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem With * Operator

    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);
    double a = velocity;
    double b = time;
    System.out.println(a * b);


    This gives me errors: Cannot convert from strings to double

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem With * Operator

    This gives me errors: Cannot convert from strings to double
    So, try using Double.parseDouble() on those two lines.

  16. #16
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem With * Operator

    Like this?

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

    I get error, "Type mismatch: cannot convert from String to double"

    Should I be doing something like this instead

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

    In order to retrieve an integer value from the user?

    Result: -The primitive type int of velocity does not have a field getText //(because "getText" wasn't used as a function in the earlier definition of velocity?)
    - Duplicate local variable velocity
    Last edited by misant; 01-18-2012 at 12:48 AM.

  17. #17
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem With * Operator

    Could you post the code and the exact text of the type mismatch message?

  18. #18
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem With * Operator

    Weird, now I'm getting no error message except when I try to multiply but of course I'm still doing that incorrectly...

    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);
    Double.parseDouble(velocity);
    Double.parseDouble(time);

    }
    }

    Output:
    Enter velocity value in Mph and press Enter: 3
    velocity is 3
    Enter time value in hours and press Enter: 4
    time is 4

    So again I'm back to having to figure out how to multiply them since it still sees them as strings and won't utilize the * operator.

    Does this whole thing need to be re-written from scratch with a different method of getting user input as an integer so they can be mathematically manipulated?
    Last edited by misant; 01-18-2012 at 01:25 AM.

  19. #19
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem With * Operator

    Weird, now I'm getting no error message
    And you are also not actually doing what I suggested "(A) make two double values from the strings". You are using parseDoube(), but where are the double values?

    Compare what you have and what I posted:

    Java Code:
        // me
    double pi = Double.parseDouble(numStr);
        // you
    Double.parseDouble(velocity);
    My line is actually doing three things: (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. It is compact, but each of the three components is necessary.

    except when I try to multiply but of course I'm still doing that incorrectly...
    That's "(B) multiply them (the double values) instead". Get (A) correct and (B) will follow.

  20. #20
    misant is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem With * Operator

    Thank you, pbrockway2, for putting up with my ignorance. I do appreciate it.

    But unless I'm mistaken, you are assigning static values to your variables whereas I'm trying to get an input of any number the user may choose.

    I'm sure there's a way to adapt it to that but I'm not yet familiar enough with it to know how...

    Or am I misunderstanding completely?

Page 1 of 2 12 LastLast

Similar Threads

  1. The modulo operator
    By Blaedel in forum New To Java
    Replies: 2
    Last Post: 10-09-2011, 04:29 AM
  2. question about the operator ++
    By dardar in forum New To Java
    Replies: 10
    Last Post: 08-16-2010, 02:39 PM
  3. help with not equal to operator !=
    By manowar689 in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 12:10 AM
  4. Increment Operator Example
    By abimaran in forum New To Java
    Replies: 10
    Last Post: 11-03-2009, 04:45 PM
  5. problem with operator in case
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 03-21-2008, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •