Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default New To Java: Question about arguments

    Hello. I'm new to Java, so sorry about the lame question.

    I want to write a quick program that will take an coomand-line argument (a written digit "one", "two", etc, up to "ten") and output the correct numeric digit (1, 2, etc). I'm starting small using only digits one, two, and three.

    So I wrote this code:

    Java Code:
    class Numbers {
    	public static void main(String[] arguments) {
    
    		String userIn = argument[0];
    		if (userIn == "one")
    			System.out.println("1");
    		else if (userIn == "two")
    			System.out.println("2");
    		else if (userIn == "three")
    			System.out.println("3");
    	}
    }
    When compiling, I get a "cannot find symbol" error referencing the statement - String userIn = argument[0] -

    So I gather I am not defining the userIn variable correctly. Can anyone show me what I am doing wrong?

    Thanks again HMB

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: New To Java: Question about arguments

    You are slightly skewed on invoking your main method. It doesn't work like you are trying.
    there are a couple ways of doing what you are trying to do. I would really suggest doing some more reading or watching of tutorials. Here is a good basic (free) series:Java Video Tutorials: Learn Java the easy way! You need to declare your variables for userIn1.....userIn2...... Then you need to import the java util class. Read the api here:Java Platform SE 7 You have to instantiate the Scanner class to take the input and assign them to the variables you declared(userIn1 and so on). Read the API on the Scanner class. This should point you in the right direction.

    Hope it helps!

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: New To Java: Question about arguments

    So, I tried to change the variable definition as follows:

    class Numbers {
    public static void main(String[] arguments) {

    String userIn = "argument[0]";
    if (userIn == "one")
    System.out.println("1");
    else if (userIn == "two")
    System.out.println("2");
    else if (userIn == "three")
    System.out.println("3");
    }
    }

    And this compiled without errors, but the program did not print out anything. When I ran the program - java Numbers one - the program did not print out anything (I was hoping the program would print out: 1).

  4. #4
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: New To Java: Question about arguments

    Hello. Thanks for your reply. Clearly, I have a lot yet to learn, and I appreciate you pointing me to the videos. I will check them out.

    I haven't learned yet about the Scanner class, so that goes on my "to do" list.

    Thanks again.

    Quote Originally Posted by mwr1976 View Post
    You are slightly skewed on invoking your main method. It doesn't work like you are trying.
    there are a couple ways of doing what you are trying to do. I would really suggest doing some more reading or watching of tutorials. Here is a good basic (free) series:Java Video Tutorials: Learn Java the easy way! You need to declare your variables for userIn1.....userIn2...... Then you need to import the java util class. Read the api here:Java Platform SE 7 You have to instantiate the Scanner class to take the input and assign them to the variables you declared(userIn1 and so on). Read the API on the Scanner class. This should point you in the right direction.

    Hope it helps!

  5. #5
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: New To Java: Question about arguments

    if you want to compare two strings you cannot use "==" operator. This might work in c or C++ but not in Java.
    Better use a equals function.

    For example
    if(userIn.equals("one"))
    System.out.println("1");

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: New To Java: Question about arguments

    The name of your parameter is 'arguments', not 'argument'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Passing arguments from java to python
    By wavefunction in forum Advanced Java
    Replies: 4
    Last Post: 07-10-2011, 04:36 PM
  2. java arguments
    By bantony02 in forum Advanced Java
    Replies: 15
    Last Post: 04-05-2011, 03:03 PM
  3. Replies: 8
    Last Post: 09-23-2010, 12:07 AM
  4. java command line arguments
    By Vipul03 in forum Advanced Java
    Replies: 10
    Last Post: 02-25-2010, 10:12 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
  •