Results 1 to 13 of 13
- 11-09-2010, 12:46 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
Passing array arguments into main
Hello there,
I am building a program, and very close to its final state.
My problem is the input.
My basic input is of the type
string name, double[], double[],double[], int x
I know that main takes only a string array as input and that I have to cast the relevant elements with double.parsedouble().
My problem is, how to pass the arguments?
I mean, the one who would call my program should do something like
MyProgram Joe [1 2 3 4 5 6], [7 8 9 10], 2
So, my question is , HOW?
- 11-09-2010, 12:55 PM #2
You covered it. You get Strings. You have to parse them to figure out what the input means. How you do that, what format the Strings should be in, etc is really up to you.
But are you sure you want to do it this way? Perhaps prompting the user for input, one field at a time, would be better.
- 11-09-2010, 01:00 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
The de facto standard to mark command line arguments is to prefix them with -<flag> where <flag> is a short mnemonic for the command line argument(s). Maybe the following will do:
Figuring out each individual command line argument through args[i] is relatively easy. Be warned though: a lot of little interpreted languages saw the light as configuration mechanisms like this. ;-)Java Code:java MyProgram Joe -d1 1 2 3 4 -d2 5 6 7 -d3 8 9 10 -i 2
kind regards,
Jos
- 11-09-2010, 01:01 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
The main idea is to receive data from a database and check them out with some rules.
So my program takes as input the name , a list of current values, and a list of previous values.
The intent is to compare the current with the previous. (how to contact the db and what happens there is not my part)
So the db will prompt my piece with a name, and two arrays.
You are saying that inside the args array, I can have arrays of strings too?
I mean, we can have something like
args[0]=John
args[1]={1,2,3,4,5,6}
args[2]={6,7,8,9,10}
so then I ll just cast them back to numbers and do whatever the program does?
** The other idea is to wait till somebody implements the interface and getting methods with the database, incorporate them into my code and then just call the function that connects to the db, and get the results to an array directly, and not cope with what to do with "main"Last edited by JohnDas; 11-09-2010 at 01:06 PM.
- 11-09-2010, 01:11 PM #5
- 11-09-2010, 01:23 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
Sorry for misunderstanding but I am a bit overwhelmed right now (and woke up at 5am, so, brain is starting to protest :D)
From your quote, in bold, I understood that args[] can be VERY flexible.. :D
The "format" got me mixed up. I thought that args[i] could also be an array (of strings too)
damn I m tired..
Sorry if I am making stupid questions but it is my first contact with java and at first I went with the impression that
hey, of course you can call my method like a function in mathematics..
(I was aiming at something like DoIt(John, [1,2,3,4,5], [6,7,8,9],blabla)
Hmmm... lets forget this, please tell if if the following is correct :
My class instances are the name, the current, the previous and another string.
I can implement a function that gets data from an external source and call it inside my main, right?
I mean I have my Foo.class and Foo.java and from inside it I call the GetData.java with something like
Foo.name.GetData(the string) Foo.Array1.GetData(first array), etc
this sounds almost right?Last edited by JohnDas; 11-09-2010 at 01:39 PM.
- 11-09-2010, 01:39 PM #7
Sorry, no, I meant that it's up to you how to format the Strings in a way that you can parse. Using Jos's suggestion of splitting them up with -d is one way. Doing it your way with [] is another. But either way, you still have to parse the information out; there is no "automatic" way to convert the arguments into different data types.
- 11-09-2010, 09:34 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Hi John,
I know KevinWorkman suggested it as well but are you really sure about taking the data from the arguments of your program? Based on
and
it seems to me that you are building a program to compare date from a database and, as long the interface is not ready yet, fake the interface. Right?
So, probably you have a class that does the comparison (e.g. DBCompare) and a main class (e.g. TestCompare) that passes the arguments from the command line to a method of that class.
If that is the case it is much easier to ask for the data in the main class and pass it to the DBCompare class then to parse the arguments. You could use a Scanner for that. Another possibility is to read the data from a file also using a Scanner.
But... my assumption could all be wrong. In that case: feel free to ignore this reply.
Good luck,
ErikI'm new to Java but I like to help where ever I can. :)
- 11-10-2010, 12:43 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
Hi Venerik!
Yes, that's exactly the idea, I have to fake the interface.
Yes, I have my main class, and inside it methods that do comparisons etc.
The problem was how to get the data inside-without having the interface, to begin calculations
I ll use your idea and try to make a scanner that reads input from a text file..
Thank you very much!!
Follow Up question - technicalities :
Already found a readtext.class
What I ll do is import it to my "central class"
and then do something like
mydata.readtext();
etc etc etc ?
To all :
Guys, thanks for your ideas and forgive if I made some "stupid" questions and remarks. Totally new at developing software that will actually work with something else :DLast edited by JohnDas; 11-10-2010 at 12:45 PM.
- 11-10-2010, 01:08 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Hi John,
I'm glad I could be of any help.
About your follow up questions: yes, that's the good direction. I don't know the Readtext.class though. It's not part of the JDK anyway.
If the Readtext class is giving you trouble you could consider using a Scanner and a File object.
Good luck,
ErikI'm new to Java but I like to help where ever I can. :)
- 11-10-2010, 01:34 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
I went with the scanner and built a test class and a test.txt to see how it works. Here's what I did :
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TextScanner {
public static void readFile(String fileName) {
try {
String[] data=new String[100];
File file = new File(fileName);
Scanner scanner = new Scanner(file);
int i=0;
while (scanner.hasNext()) {
data[i++]=scanner.next();
System.out.println(scanner.next());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("usage: java TextScanner1"
+ "file location");
System.exit(0);
}
readFile("Test.txt");
}
}
But there's the usual error, which I dont know how to address, since the code seems to me ok :
java.lang.NoClassDefFoundError: com/sun/tools/xjc/XJCFacade
Caused by: java.lang.ClassNotFoundException: com.sun.tools.xjc.XJCFacade
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Exception in thread "main"Last edited by JohnDas; 11-10-2010 at 01:37 PM.
- 11-10-2010, 02:18 PM #12
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Based on the provided code I cannot explain the error. I am not familiair with xjc either so I can't help you on that one.
About the code: I think you will probably need hastNextLine and nextLine to read the name, hasNextDouble and nextDouble several times to read the arrays and a final hasNextInt and nextInt.
Something like (non working code follows)
This code assumes the file contains the name, array 1, array 2 and the final int on 4 seperate lines.Java Code:String name; Double[5] d1; Double[6] d2; Int i, j; if(scanner.hasNextLine()) { name = scanner.nextLine(); } i = 0; while (scanner.hasNextDouble() && i < d1.length) { d1[i] = scanner.nextDouble(); i++; } scanner.nextLine(); i = 0; while (scanner.hasNextDouble() && i < d2.length) { d2[i] = scanner.nextDouble(); i++; } scanner.nextLine(); if (scanner.hasNextInt()) { j = scanner.nextInt(); }
Cheers,
ErikI'm new to Java but I like to help where ever I can. :)
- 11-10-2010, 03:00 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
Tried your code, damn thing spills out the same error...
Thank you anyway mate :D :D
Anybody else has an idea?
Yes, it is in the same folder with other stuff that at some point used xjc (needed to read some parameteres from xml too..)
but why does the damn thing call xjc now that it's not needed???
BTW in case it matters, I m using eclipse heliosLast edited by JohnDas; 11-10-2010 at 03:24 PM.
Similar Threads
-
passing arguments
By mac in forum New To JavaReplies: 3Last Post: 04-07-2010, 11:30 PM -
passing hashmap from a method and call it within main
By rajuchacha007 in forum New To JavaReplies: 2Last Post: 04-06-2010, 09:41 AM -
passing values from main page to pop up window
By vicky in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 12-29-2008, 03:06 PM -
[SOLVED] passing array between main and method,vice-versa
By blueyan in forum New To JavaReplies: 5Last Post: 10-04-2008, 11:13 AM -
Arguments in Main
By CyberFrog in forum New To JavaReplies: 2Last Post: 03-30-2008, 09:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks