Results 21 to 27 of 27
Thread: Problem With * Operator
- 01-18-2012, 02:12 AM #21
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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()).
- 01-18-2012, 04:07 AM #22
Member
- Join Date
- Jan 2012
- Posts
- 13
- Rep Power
- 0
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!!! :DLast edited by misant; 01-18-2012 at 04:23 AM.
- 01-18-2012, 04:16 AM #23
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 01-18-2012, 04:26 AM #24
Member
- Join Date
- Jan 2012
- Posts
- 13
- Rep Power
- 0
Re: Problem With * Operator
Ah, ok, I'll clean it up!
Is there a way to call and run a function like this
with a single word term like "function1" in order to abbreviate another section of code later on?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);Last edited by misant; 01-18-2012 at 05:15 AM.
- 01-18-2012, 05:01 AM #25
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
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.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); } }
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.
- 01-18-2012, 05:59 AM #26
Member
- Join Date
- Jan 2012
- Posts
- 13
- Rep Power
- 0
Re: Problem With * Operator
Thank you!
Do I only have to add
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?Java Code:e.printStackTrace();
- 01-18-2012, 06:26 AM #27
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
Similar Threads
-
The modulo operator
By Blaedel in forum New To JavaReplies: 2Last Post: 10-09-2011, 04:29 AM -
question about the operator ++
By dardar in forum New To JavaReplies: 10Last Post: 08-16-2010, 02:39 PM -
help with not equal to operator !=
By manowar689 in forum New To JavaReplies: 9Last Post: 06-15-2010, 12:10 AM -
Increment Operator Example
By abimaran in forum New To JavaReplies: 10Last Post: 11-03-2009, 04:45 PM -
problem with operator in case
By jimJohnson in forum New To JavaReplies: 2Last Post: 03-21-2008, 08:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks