Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
  1. #21
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Problem With * Operator

    There is nothing static about numStr it's just a string variable like your velocity and time are just string variables.

    I pass the value of numStr to parseDouble(), just as you pass velocity.

    But I also (1) declare a double variable and (3) assign the number returned by parseDouble() to that variable. And your code does neither of these things. It just parses the string (== obtain a number from it), then... nothing. It does nothing with the number it gets from the string.

    Compare the two lines of code I posted before. Even at a very superficial level, mine has an = and yours does not. (At a less superficial level I tried to explain what that = was doing there: joining a new variable I had declared with the number returned by parseDouble()).

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

    Default Re: Problem With * Operator

    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.valueOf(numStr)).doubleValue();
    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.valueOf(numStr2)).doubleValue();
    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

    THANK YOU PBROCKWAY2!!! :D
    Last edited by misant; 01-18-2012 at 05:23 AM.

  3. #23
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Problem With * Operator

    You're welcome. I'm glad you've got it figured out.

    I see you've gone back to valueOf(). Most people (?) would say "Double.parseDouble(numStr);" where you say "(Double.valueOf(numStr)).doubleValue();" but the two do exactly the same.

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

    Default Re: Problem With * Operator

    Ah, ok, I'll clean it up!

    Is there a way to call and run a function like this
    Java Code:
    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);
    with a single word term like "function1" in order to abbreviate another section of code later on?
    Last edited by misant; 01-18-2012 at 06:15 AM.

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

    Default Re: Problem With * Operator

    Yes, you can make a method out of code that is repeated, then reuse it. It's considered a Good Thing.

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Test {
        public static void main(String[] args) {
            double num = getResponse("Enter numerator");
            double denom = getResponse("Enter denominator");
    
            System.out.println(num + "/" + denom + "=" + (num / denom));
        }
            // your code, but noting that the system.out.print()
            // business is also repeated
        private static double getResponse(String prompt) {
            System.out.print(prompt + ": ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String numStr = null;
            try {
                numStr = br.readLine();
            } catch (IOException e) {
                e.printStackTrace(); // make it a habit to add this line
                System.out.println("Error!");
                System.exit(1);
            }
                // now we don't even need a num variable
            return Double.parseDouble(numStr);
        }
    }
    printStackTrace() prints out diagnostic information in the event of an exception. A "real" program might want to do something more clever when an exception occurs but printStackTrace() is the bare minimum. You'd be amazed how many posts there are here where something goes wrong and the first thing people say is to add this line.

    It might strike you as a bit odd that you keep creating a buffered reader. I mean there is only one System.in so the same one could be reused. This involves having a variable declared outside of main() or the other method. I won't do it for you, but you might get the idea from:

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Test {
            // you could initialise arrow here...
        private static String arrow;
        
        public static void main(String[] args) {
                // ... but just to show that arrow really is shared
                // between the two methods
            arrow = " --> ";
            
            double num = getResponse("Enter numerator");
            double denom = getResponse("Enter denominator");
            
            System.out.println(num + "/" + denom + "=" + (num / denom));
        }
    
        private static double getResponse(String prompt) {
            System.out.print(prompt + arrow);
                // TODO: share br as well
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String numStr = null;
            try {
                numStr = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Error!");
                System.exit(1);
            }
            return Double.parseDouble(numStr);
        }
    }
    -----

    All the "statics" in that code are an artifact of Java that arises because you are not using classes. Typical code does not use the static keyword very much.

    -----

    When you post code here, use the code tags. You put [code] at the start of the code and [/code] at the end. That way the code will be formatted and readable when it appears here.

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

    Default Re: Problem With * Operator

    Thank you!

    Do I only have to add
    Java Code:
    e.printStackTrace();
    once in a given program, say, at the end, or must it be added at the end of every functional chunk of code where a system exit could potentially be executed?

  7. #27
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Problem With * Operator

    Put it in every "catch" block. As well as, or instead of just saying "Error!". The point is that if something does go wrong then "Error!" is not very helpful. When you've seen one of these stack traces (and you will!) you'll see what I mean: they list the line number and file where the problem occurred, which method was being run, which method called it etc.

Page 2 of 2 FirstFirst 12

Similar Threads

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