Results 1 to 9 of 9
Thread: Command line arguments help
- 12-08-2009, 02:32 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 1
- Rep Power
- 0
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?
- 12-08-2009, 04:02 AM #2
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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.
- 12-08-2009, 05:28 AM #3gcampton Guest
- 12-08-2009, 05:33 AM #4gcampton Guest
here's some sample code you can use to test:
now if you run this program using: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]); } }
EDIT: sorry I am wrong, was thinking of c++/unix. the output would beJava 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
Java Code:0. hello 1. world 2. my 3. name 4. is 5. Bob
Last edited by gcampton; 12-08-2009 at 05:38 AM.
- 12-08-2009, 11:46 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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)
- 12-08-2009, 01:01 PM #6gcampton Guest
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.
- 12-08-2009, 01:03 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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).
- 12-08-2009, 01:09 PM #8gcampton Guest
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...
- 12-08-2009, 01:20 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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 01:39 PM.
Similar Threads
-
Command Line Arguments
By Nakira in forum NetBeansReplies: 10Last Post: 02-04-2010, 03:45 PM -
Multiple Command Line Arguments
By turnergirl24 in forum New To JavaReplies: 4Last Post: 12-04-2009, 09:36 PM -
[SOLVED] command line arguments using IDE
By sandeepsai39 in forum New To JavaReplies: 5Last Post: 03-12-2009, 07:19 AM -
Printing command line arguments
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:27 AM -
Java Command Line Arguments In Eclipse IDE
By JavaForums in forum EclipseReplies: 0Last Post: 05-19-2007, 09:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks