|
-
May 16th, 2007, 12:17 PM
#1
Thread Starter
Lively Member
help with java
sorry for lack of a descriptive title, but I don't know how to describe my problem!
I'm looking to do this:
I have a program called "program" It is run via the command "java" in the command line. I have 3 strings in it: string "a", string "b", and string "c". Here's what I need to have happen:
When the user enters "java program duck cow moose". I want "duck" to be stored in string "a", "cow" to be stored in string "b", and "moose" to be stored in string "c". Also, If the person only enters "java program duck cow", then I want "duck" to be stored in string "a", "cow" to be stored in string "b", and for string "c" to remain empty.
I have no idea how to go about this... I know how to make it so user input goes into a string, but only after the program is running. How do I do it in the way I described?
Thanks,
stettybet0
-
May 16th, 2007, 01:01 PM
#2
Frenzied Member
Re: help with java
Code:
public static void main(String[] args)
{
}
args will contain the passed arguments. args[0] = first argument, args[1] = second argument, etc.
-
May 16th, 2007, 01:10 PM
#3
Thread Starter
Lively Member
Re: help with java
just to make sure I understand:
so, if i put
Code:
string a = args[0]
string b = args[1]
string c = args[2]
that would give me what I want?
-
May 16th, 2007, 01:15 PM
#4
Frenzied Member
Re: help with java
 Originally Posted by stettybet0
just to make sure I understand:
so, if i put
Code:
string a = args[0]
string b = args[1]
string c = args[2]
that would give me what I want?
Sort of. Your code would require exactly 3 arguments, and since you might not have three you need to do some range checking.
ie: if (args.length > 0)
-
May 16th, 2007, 01:18 PM
#5
Thread Starter
Lively Member
Re: help with java
to check whether args[0] exists, would i do "if (args[0].length > 0)" ?
-
May 16th, 2007, 01:32 PM
#6
Frenzied Member
Re: help with java
if (args.length > 0) ensures that there's at least one argument, so args[0] would exist in this case. It might be helpful to know that args.length returns the number of arguments. If you know there won't be more than 3 arguments, then you can do something like this:
Code:
public static void main(String[] args)
{
String a="", b="", c="";
if (args.length > 0) a = args[0];
if (args.length > 1) b = args[1];
if (args.length > 2) c = args[2];
}
or
Code:
public static void main(String[] args)
{
String a="", b="", c="";
switch(args.length)
{
case 1:
a = args[0];
break;
case 2:
a = args[0];
b = args[1];
break;
case 3:
a = args[0];
b = args[1];
c = args[2];
break;
}
}
This could be done in many different ways.
-
May 16th, 2007, 01:32 PM
#7
Thread Starter
Lively Member
Re: help with java
here's what I tried... with no success:
java Code:
class program {
public static void main (String[] args) {
String a = null;
String b = null;
String c = null;
if (args[0].length > 0) {
String a = args[0];
}
if (args[1].length > 0) {
String b = args[1];
}
if (args[2].length > 0) {
String c = args[2];
}
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
edit:
saw ur new post... trying that out now.
-
May 16th, 2007, 01:34 PM
#8
Frenzied Member
Re: help with java
Wow! We just posted at the exact same time! You are very close, but check my listings about you.
I would go for this:
Code:
public static void main(String[] args)
{
String a="", b="", c="";
if (args.length > 0) a = args[0];
if (args.length > 1) b = args[1];
if (args.length > 2) c = args[2];
}
-
May 16th, 2007, 01:38 PM
#9
Frenzied Member
Re: help with java
Of course, if you just want to print out the args, then all you have to do is this:
Code:
public static void main(String[] args)
{
for (int i=0; i<args.length; ++i)
System.out.println(args[i]);
}
-
May 16th, 2007, 01:47 PM
#10
Thread Starter
Lively Member
Re: help with java
Here's what I ended up with: (it works great)
java Code:
class program {
public static void main (String[] args) {
String a = "";
String b = "";
String c = "";
if (args.length > 0 && args.length < 2) {
a = args[0];
System.out.println(a);
}
if (args.length > 1 && args.length < 3) {
a = args[0];
b = args[1];
System.out.println(a);
System.out.println(b);
}
if (args.length > 2 && args.length < 4) {
a = args[0];
b = args[1];
c = args[2];
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
if (args.length > 3 || args.length < 1) {
System.out.println("Enter only 1, 2, or 3 arguments!");
}
}
}
anything you see that i did wrong (or inefficiently)?
thanks for the help.
-
May 16th, 2007, 02:13 PM
#11
Thread Starter
Lively Member
Re: help with java
I have also come up with this way:
java Code:
class program {
public static void main (String[] args) {
String a = "";
String b = "";
String c = "";
if (args.length > 3 || args.length < 1) {
System.out.println("Enter only 1, 2, or 3 arguments!");
System.exit(0);
}
if (args.length > 0) {
a = args[0];
System.out.println(a);
}
if (args.length > 1) {
b = args[1];
System.out.println(b);
}
if (args.length > 2) {
c = args[2];
System.out.println(c);
}
}
}
Which way do you think is more "orthodox"? (ie. it would please my old-skool teacher)
-
May 16th, 2007, 03:13 PM
#12
Thread Starter
Lively Member
Re: help with java
A third way: (only difference is that I don't define the variables before I need them... just learned that you usually shouldn't )
java Code:
class program {
public static void main (String[] args) {
if (args.length > 3 || args.length < 1) {
System.out.println("Enter only 1, 2, or 3 arguments!");
System.exit(0);
}
if (args.length > 0) {
String a = args[0];
System.out.println(a);
}
if (args.length > 1) {
String b = args[1];
System.out.println(b);
}
if (args.length > 2) {
String c = args[2];
System.out.println(c);
}
}
}
This way seems the best to me. My teacher always says that if you can do the same thing with less code, then do it!
-
May 16th, 2007, 05:00 PM
#13
Frenzied Member
Re: help with java
If you are just printing the variables out, then there's no point in even creating them:
Code:
public static void main(String[] args)
{
if (args.length > 0 && args.length < 4)
{
for (int i=0; i<args.length; ++i)
System.out.println(args[i]);
}
else
{
System.out.println("You must supply at least one argument and less than four");
}
}
No variables needed, but of course, if this is an assignment and it's required then go with your last try.
-
May 16th, 2007, 10:16 PM
#14
Thread Starter
Lively Member
Re: help with java
Yeah, I needed variables. Thanks for your help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|