Results 1 to 13 of 13
  1. #1
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default 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?

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

    Default

    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.

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

    Default

    Quote Originally Posted by JohnDas View Post
    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?
    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:

    Java Code:
    java MyProgram Joe -d1 1 2 3 4 -d2 5 6 7 -d3 8 9 10 -i 2
    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. ;-)

    kind regards,

    Jos

  4. #4
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    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.

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

    Default

    Quote Originally Posted by JohnDas View Post
    You are saying that inside the args array, I can have arrays of strings too?
    That is not what either one of us are saying. What didn't you understand about what we said?

  6. #6
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    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.
    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.

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

    Default

    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.

  8. #8
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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

    Quote Originally Posted by JohnDas View Post
    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.
    and

    Quote Originally Posted by JohnDas View Post
    ** 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"
    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,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  9. #9
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    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 :D
    Last edited by JohnDas; 11-10-2010 at 12:45 PM.

  10. #10
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  11. #11
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    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.

  12. #12
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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)
    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();
      }
    This code assumes the file contains the name, array 1, array 2 and the final int on 4 seperate lines.

    Cheers,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  13. #13
    JohnDas is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    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 helios
    Last edited by JohnDas; 11-10-2010 at 03:24 PM.

Similar Threads

  1. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-07-2010, 11:30 PM
  2. passing hashmap from a method and call it within main
    By rajuchacha007 in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 09:41 AM
  3. passing values from main page to pop up window
    By vicky in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-29-2008, 03:06 PM
  4. Replies: 5
    Last Post: 10-04-2008, 11:13 AM
  5. Arguments in Main
    By CyberFrog in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 09:37 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
  •