Democratic Underground Latest Greatest Lobby Journals Search Options Help Login
Google

Question for anybody who knows Java

Printer-friendly format Printer-friendly format
Printer-friendly format Email this thread to a friend
Printer-friendly format Bookmark this thread
This topic is archived.
Home » Discuss » The DU Lounge Donate to DU
 
billyskank Donating Member (1000+ posts) Send PM | Profile | Ignore Thu Aug-31-06 04:29 AM
Original message
Question for anybody who knows Java
Okay, I know this must be the Dumbest Question Ever. But how do you know how many arguments have been passed to your Java program?

Like, in C I declare my main() function like this:

void main(int argc, char *argv)

and argc tells me how many values there are in the argv array. But in Java I have to do this:

public static void main(String[] args)

so how on earth do I know how many values there are in the args array?

Printer Friendly | Permalink |  | Top
 
Fredda Weinberg Donating Member (1000+ posts) Send PM | Profile | Ignore Thu Aug-31-06 05:13 AM
Response to Original message
1. You've declared it
Then you'll initialize with the "new" operator or just assign values

names = new String<100>;

or

String[] names = {"Fred", "Wilma", "Pebbles"};
Printer Friendly | Permalink |  | Top
 
billyskank Donating Member (1000+ posts) Send PM | Profile | Ignore Thu Aug-31-06 05:20 AM
Response to Reply #1
2. I don't want to assign values to it
it already has values assigned to it, which are the arguments passed to the program from the command line. So how do I iterate through them - how do I know how many there are?
Printer Friendly | Permalink |  | Top
 
Fredda Weinberg Donating Member (1000+ posts) Send PM | Profile | Ignore Thu Aug-31-06 08:19 PM
Response to Reply #2
4. To continue our names() example
for(int i=0; i<names.length; i++){}

Obviously, you can do what you like in the curly braces. Reference each item with

names

Ain't Java terse?
Printer Friendly | Permalink |  | Top
 
Commie Pinko Dirtbag Donating Member (1000+ posts) Send PM | Profile | Ignore Thu Aug-31-06 12:26 PM
Response to Original message
3. C man here, but I'd bet that array type has some "size" property
Like args.length() or something.
Printer Friendly | Permalink |  | Top
 
aePrime Donating Member (676 posts) Send PM | Profile | Ignore Thu Aug-31-06 09:50 PM
Response to Original message
5. Somebody's mentioned the first...
Edited on Thu Aug-31-06 09:53 PM by aePrime

public class Array
{
public static void main(String[] args)
{
for (int i = 0; i < args.length; ++i) {
System.out.println(args);
}

// Java 5.0
for (String element : args) {
System.out.println(element);
}
}
}


The first will work with any Java version. The second works only in Java 5. The arrays in Java have a length, similar to C++'s STL std::vector (I actually spend all of my time writing C++, but am familiar with Java).

I noticed that the first guy who wrote the loop used "i++". I'm in the habit of writing "++i". Because Java doesn't have operator overloading, it probably doesn't matter, but in C++ "++i" is more efficient than "i++".

(Updated to make the formatting better. DU insists on italiasizing parts of this message.)
Printer Friendly | Permalink |  | Top
 
aePrime Donating Member (676 posts) Send PM | Profile | Ignore Thu Aug-31-06 09:54 PM
Response to Original message
6. Also
Be aware that unlike C, Java doesn't store the executable name in the first array slot. All values in the array are arguments.
Printer Friendly | Permalink |  | Top
 
DU AdBot (1000+ posts) Click to send private message to this author Click to view 
this author's profile Click to add 
this author to your buddy list Click to add 
this author to your Ignore list Sat Nov 18th 2017, 06:42 PM
Response to Original message
Advertisements [?]
 Top

Home » Discuss » The DU Lounge Donate to DU

Powered by DCForum+ Version 1.1 Copyright 1997-2002 DCScripts.com
Software has been extensively modified by the DU administrators


Important Notices: By participating on this discussion board, visitors agree to abide by the rules outlined on our Rules page. Messages posted on the Democratic Underground Discussion Forums are the opinions of the individuals who post them, and do not necessarily represent the opinions of Democratic Underground, LLC.

Home  |  Discussion Forums  |  Journals |  Store  |  Donate

About DU  |  Contact Us  |  Privacy Policy

Got a message for Democratic Underground? Click here to send us a message.

© 2001 - 2011 Democratic Underground, LLC