Results 1 to 6 of 6
- 02-10-2012, 05:14 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 88
- Rep Power
- 0
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:
When compiling, I get a "cannot find symbol" error referencing the statement - String userIn = argument[0] -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"); } }
So I gather I am not defining the userIn variable correctly. Can anyone show me what I am doing wrong?
Thanks again HMB
- 02-10-2012, 05:47 PM #2
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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!
- 02-10-2012, 05:48 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 88
- Rep Power
- 0
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).
- 02-10-2012, 05:52 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 88
- Rep Power
- 0
Re: New To Java: Question about arguments
- 02-10-2012, 06:09 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 60
- Rep Power
- 0
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");
- 02-10-2012, 06:27 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Passing arguments from java to python
By wavefunction in forum Advanced JavaReplies: 4Last Post: 07-10-2011, 04:36 PM -
java arguments
By bantony02 in forum Advanced JavaReplies: 15Last Post: 04-05-2011, 03:03 PM -
Sorting objects with 2 strings arguments and 2 int arguments
By tirwit in forum New To JavaReplies: 8Last Post: 09-23-2010, 12:07 AM -
java command line arguments
By Vipul03 in forum Advanced JavaReplies: 10Last Post: 02-25-2010, 10:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks