Results 1 to 3 of 3
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    109
    Rep Power
    0

    Default Character using KeyBoard !

    Consider the following code:


    public class ValueOfDemo {
    public static void main(String[] args) {

    // this program requires two
    // arguments on the command line
    if (args.length == 2) {
    // convert strings to numbers
    float a = (Float.valueOf(args[0])).floatValue();
    float b = (Float.valueOf(args[1])).floatValue();

    // do some arithmetic
    System.out.println("a + b = " +
    (a + b));
    System.out.println("a - b = " +
    (a - b));
    System.out.println("a * b = " +
    (a * b));
    System.out.println("a / b = " +
    (a / b));
    System.out.println("a % b = " +
    (a % b));
    } else {
    System.out.println("This program " +
    "requires two command-line arguments.");
    }
    }
    }

    My Question:

    Whenever we enter something through keyboard, it's considered as character, right? For example if my arguments in the above program are 2 and 3, therefore 2 and 3 will be considered as characters, right? That's the reason we are required to convert String to Numbers?

    Question #2:

    Why does the comment section in the followig code of above program says " String to Numbers" ? It shoulc be Characters to Numbers, right?

    // convert strings to numbers
    float a = (Float.valueOf(args[0])).floatValue();
    float b = (Float.valueOf(args[1])).floatValue();

    Please clarify.

    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Character using KeyBoard !

    A character is a single letter or digit. Strings can consist of multiple characters. For example, a String can be "237", which contains 3 characters. You can convert that to the number 237.

    At no point in your example code (which should be posted with the code tags, by the way) are you breaking a String up into individual characters. For more info: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Replies: 1
    Last Post: 11-06-2012, 02:29 PM
  2. Replies: 0
    Last Post: 11-06-2012, 02:09 PM
  3. Replies: 0
    Last Post: 11-06-2012, 02:09 PM
  4. getting each character from keyboard
    By Sreejesh25 in forum New To Java
    Replies: 11
    Last Post: 01-25-2011, 01:08 PM
  5. getting each character from keyboard
    By Sreejesh25 in forum Advanced Java
    Replies: 6
    Last Post: 03-05-2010, 07:12 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
  •