Results 1 to 12 of 12
- 07-07-2010, 05:09 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
How do I substitute any variable for a hardcoded variable
Hello Everyone,
I'm trying to get my program to work with any variable and I want to avoid hardcoding. Are there any simple fixes for this?
Java Code:public class Prog4 { public static void main(String[] args) { double speed = 40; double stoppingDistance; System.out.println("Enter speed: " + speed); stoppingDistance = (speed * (2.25 + speed / 21)); System.out.println("Stopping distance: " + stoppingDistance); if (stoppingDistance < 178.5) { System.out.println ("No problem"); }//end if else if (stoppingDistance == 178.5) { System.out.println("Minor Wreck"); }//end else if else { System.out.println("Major Wreck!"); }//end else }//end main }//end class Prog4
Last edited by Eranga; 07-07-2010 at 05:25 AM. Reason: code tags added
- 07-07-2010, 05:16 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
What variable do you wish to substitute? It does not really matter there are numerous ways for you to set variables, you can set variable from the command prompt if you wish
- 07-07-2010, 05:19 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
If you wish to send input into the program, look at Scanner (Java 2 Platform SE 5.0), or send command line arguments sent via the args variable.
- 07-07-2010, 05:38 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
I think that OP wants to change the speed variable. Use scanner class to get the user inputs from the command prompt.
- 07-07-2010, 05:38 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
Thanks al Marshy - I would like to substitute 40 (speed) with any non-hardcoded variable.
- 07-07-2010, 05:48 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
it is temporarlily gone out of my head but its a quick experiment to find out which one, when you run a program from command prompt you can provide values.. reference---> Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)
all you have to do is set that speed variable to args[0], im pretty sure the first argument is the variable. Just remember arguments come in as Strings so you will have to parse them to a double, here....
e.g.
Java Code:double speed=Double.parseDouble(args[0]);
Other than this like Eranga says use Scanner class Scanning (The Java™ Tutorials > Essential Classes > Basic I/O) (or DoWhiles link) to get values from user input and assign them to the speed variable..
- 07-07-2010, 05:50 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
And also you've think about how to validate user inputs, like string literals rather digits.
- 07-07-2010, 06:15 AM #8
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
Thanks Eranga does this look right?
import java.util.Scanner;
Java Code:public class Prog4 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String speed = ""; keyboard.nextLine(); double speed; double stoppingDistance; System.out.println("Enter speed: " + speed); stoppingDistance = (speed * (2.25 + speed / 21)); System.out.println("stoppingDistance: " + stoppingDistance); if (stoppingDistance < 178.5) { System.out.println ("No problem"); }//end if else if (stoppingDistance == 178.5) { System.out.println("Minor Wreck"); }//end else if else { System.out.println("Major Wreck!"); }//end else }//end main }//end class Prog4
Last edited by Eranga; 07-07-2010 at 06:49 AM. Reason: code tags added
- 07-07-2010, 06:30 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
I can't see where speed is read in from user input and then used.
Java Code:Scanner keyboard = new Scanner(System.in); String speed = ""; keyboard.nextLine();
try
Java Code:Scanner keyboard = new Scanner(System.in); System.out.print("Enter the value for speed: "); String input=keyboard.nextLine(); double speed=Double.parseDouble(input);
- 07-07-2010, 06:55 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 07-07-2010, 06:57 AM #11
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
Thanks for all the help everyone!! I was able to use the scanner tool to help solve it.
import java.util.Scanner;
public class Prog4
{
public static void main(String[] args)
{
Scanner xyz = new Scanner(System.in);
double speed;
speed = xyz.nextDouble();
double stoppingDistance;
System.out.println("Enter speed: " + speed);
stoppingDistance = (speed * (2.25 + speed / 21));
System.out.println("stoppingDistance: " + stoppingDistance);
if (stoppingDistance < 178.5)
{
System.out.println ("No problem");
}//end if
else if (stoppingDistance == 178.5)
{
System.out.println("Minor Wreck");
}//end else if
else
{
System.out.println("Major Wreck!");
}//end else
}//end main
}//end class Prog4
- 07-07-2010, 07:02 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Next thing, what happen if I enter the value "hello" as my inputs in your code?
Please use code tags. Didn't you notice that I've edited your previous posts and added code tags? Unformated codes are really hard to read.
Similar Threads
-
cannot set variable
By jruland in forum Advanced JavaReplies: 2Last Post: 03-19-2010, 12:43 AM -
variable output is always 0
By 1Sserdda in forum New To JavaReplies: 2Last Post: 09-25-2009, 07:26 AM -
Your variable is never read
By singularity in forum New To JavaReplies: 4Last Post: 09-10-2009, 01:13 PM -
getting the value of variable
By Lehane_9 in forum New To JavaReplies: 2Last Post: 03-05-2008, 02:42 AM -
Getting variable value from a variable name
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 10:26 PM
Bookmarks