Results 1 to 12 of 12
  1. #1
    Weazel Boy is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default 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 04:25 AM. Reason: code tags added

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    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.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I think that OP wants to change the speed variable. Use scanner class to get the user inputs from the command prompt.

  5. #5
    Weazel Boy is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks al Marshy - I would like to substitute 40 (speed) with any non-hardcoded variable.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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]);
    where args[0] is the value passed at the command prompt.

    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..

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  8. #8
    Weazel Boy is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    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 05:49 AM. Reason: code tags added

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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();
    Also you have a String called speed and a double called speed.



    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);
    Now you have a value for speed as a double.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Java Code:
    Scanner keyboard = new Scanner(System.in);
         // Create a new scanner object
    
    System.out.print("Enter the value for speed: ");
         // Display a message to the user
    
    String input=keyboard.nextLine();
         // Read the next line (if the use enter a text?)
    
    double speed=Double.parseDouble(input);
         // Convert the string literal to a double value
    Now you have a value for speed as a double.
    I agreed with you, OP hasn't assign any user inputs to the speed variable.

  11. #11
    Weazel Boy is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    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

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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

  1. cannot set variable
    By jruland in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 11:43 PM
  2. variable output is always 0
    By 1Sserdda in forum New To Java
    Replies: 2
    Last Post: 09-25-2009, 06:26 AM
  3. Your variable is never read
    By singularity in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 12:13 PM
  4. getting the value of variable
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 03-05-2008, 01:42 AM
  5. Getting variable value from a variable name
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:26 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
  •