Results 1 to 9 of 9
  1. #1
    may88 is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default Command line arguments help

    Hi i am very new to java and have just written this basic piece of code

    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    import java.awt.image.BufferedImage;
    import javax.imageio.imageIO;
    import.java.awt.Color;
    public class FinalExam
    {
    public static void main(String [] args) throws IOException
    {
    Scanner keyboardInput = new Scanner(System.in);
    System.out.print("Input the address of the image (e.g. c:\\myPicture.jpg)");
    String imageLocation = keyboardInput.nextLine();
    File imageFile = new File(imageLocation);
    BufferedImage storedImage = ImagIO.read(imageFile);




    does anyone know how you would change the code to supply the name of an image file as a command line argument, when running the program?

  2. #2
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    In the array args are stored the command line arguments. Just change ImageLocation for args[0]. You also should check if the length of the array is 1.

  3. #3
    gcampton Guest

    Default

    Quote Originally Posted by sky View Post
    In the array args are stored the command line arguments. Just change ImageLocation for args[0]. You also should check if the length of the array is 1.
    I'm pretty sure program name is always stored in args[0]
    so it would be args[1]

    or I could be getting confused with c/c++ and unix scripting.

    you will just have to test this.

  4. #4
    gcampton Guest

    Default

    here's some sample code you can use to test:

    Java Code:
    public static void main(String [] args)
    {
       for ( int i=0; i < args.length; i++ )
       {
          System.out.printf("%d.\t%s\n", i, args[i]);
       }
    }
    now if you run this program using:
    Java Code:
    $ program hello world my name is Bob
    
    // the output should be:
    
    0. program
    1. hello
    2. world
    3. my
    4. name
    5. is
    6. Bob
    EDIT: sorry I am wrong, was thinking of c++/unix. the output would be
    Java Code:
    0. hello
    1. world
    2. my
    3. name
    4. is
    5. Bob
    Last edited by gcampton; 12-08-2009 at 06:38 AM.

  5. #5
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Yes, in C te name of the program is stored in argv[0], and also another parameter is passed indicating the number of arguments (argc)

  6. #6
    gcampton Guest

    Default

    yep, and in sh/bash/ksh/csh/zsh/ash/dash etc etc etc...

    $0 is always the program name, even after shifting positional parameters.

    I would think java would take after this convention, seems pretty silly not to.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Nope, it doesn't though. The args array are truely those, just the arguments (i.e. everything entered after the name of the main class).

  8. #8
    gcampton Guest

    Default

    yea there was probably a meeting that went something like this:

    p1: I think we should keep to the C convention of command line args.

    p2: name one good use for having program name stored in 0.

    p1: errrrrr......ahhhhhhhh, hmmmmm.

    p2: motion granted, args[0] will not contain filename...

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    They have made some rather stranger decisions though, IMHO. Take Calendar for instance. The months "array" starts with 0 like in nearly every other language I've seen, but the weekdays "array" starts with 1, why?

    Java Code:
    public class CalendarQuestions {
    
    	public static void main(String[] args) {
    		System.out.println("January:    " + Calendar.JANUARY);
    		System.out.println("February:   " + Calendar.FEBRUARY);
    		System.out.println("March:      " + Calendar.MARCH);
    		System.out.println("April:      " + Calendar.APRIL);
    		System.out.println("May:        " + Calendar.MAY);
    		System.out.println("June:       " + Calendar.JUNE);
    		System.out.println("July:       " + Calendar.JULY);
    		System.out.println("August:     " + Calendar.AUGUST);
    		System.out.println("September:  " + Calendar.SEPTEMBER);
    		System.out.println("October:    " + Calendar.OCTOBER);
    		System.out.println("November:  " + Calendar.NOVEMBER);
    		System.out.println("December:  " + Calendar.DECEMBER);
    		System.out.println();
    		System.out.println("Sunday:     " + Calendar.SUNDAY);
    		System.out.println("Monday:     " + Calendar.MONDAY);
    		System.out.println("Tuesday:    " + Calendar.TUESDAY);
    		System.out.println("Wednesday:  " + Calendar.WEDNESDAY);
    		System.out.println("Thursday:   " + Calendar.THURSDAY);
    		System.out.println("Friday:     " + Calendar.FRIDAY);
    		System.out.println("Saturday:   " + Calendar.SATURDAY);
    	}
    }
    Last edited by masijade; 12-08-2009 at 02:39 PM.

Similar Threads

  1. Command Line Arguments
    By Nakira in forum NetBeans
    Replies: 10
    Last Post: 02-04-2010, 04:45 PM
  2. Multiple Command Line Arguments
    By turnergirl24 in forum New To Java
    Replies: 4
    Last Post: 12-04-2009, 10:36 PM
  3. [SOLVED] command line arguments using IDE
    By sandeepsai39 in forum New To Java
    Replies: 5
    Last Post: 03-12-2009, 08:19 AM
  4. Printing command line arguments
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:27 AM
  5. Java Command Line Arguments In Eclipse IDE
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-19-2007, 09:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •