Results 1 to 4 of 4
  1. #1
    beastman3226 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Setting a value from an input in a Command line

    How do I set a value from an input from the command prompt?
    I want to set the value that i get from the user to a method. For example setAge(Double age).
    I get 24 from the user, then I need to set the value (24) to their age. How do I do this?

  2. #2
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Setting a value from an input in a Command line


  3. #3
    beastman3226 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Setting a value from an input in a Command line

    Here my current code:
    Java Code:
    public static void readName(){ 
        System.out.print("What is your name? ");
    
          //  open up standard input
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
          String userName = null;
    
          //  read the username from the command-line; need to use try/catch with the
          //  readLine() method
          try {
             userName = br.readLine();
          } catch (IOException ioe) {
             System.out.println("IO error trying to read your name!");
             System.exit(1);
          }
    
          System.out.println("Thanks for the name, " + userName);
        }
    public static void readAge(){
            Scanner scan = new Scanner(System.in);
            System.out.print("What is your age? ");
            double age = scan.nextDouble();
            System.out.println("Your are " + age + " years old");
        }
        public static void readWeight(){
            Scanner scan = new Scanner(System.in);
            System.out.print("What is your age? ");
            double weigh = scan.nextDouble();
            System.out.println("Your weight " + weigh);
          }
    I want the values of weigh and age. I don't want actual commands.
    Last edited by beastman3226; 11-15-2012 at 12:46 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Setting a value from an input in a Command line

    You've got them.
    The question is what do you want to do with them, as currently they only exist in the methods themselves.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  2. command line input to array
    By minusten in forum New To Java
    Replies: 13
    Last Post: 05-09-2011, 01:56 AM
  3. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  4. Replies: 3
    Last Post: 04-26-2010, 09:00 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

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