Problem with basic DataOutputStream script

Hi, when I run this code (below) I get this error message:

Abnormal error java.lang.ArrayIndexOutOfBoundsException: 0

I should get A-Z as my output but I don't, any ideas why?

import java.io.*;

publicclass FileWrite{

publicstaticvoid main(String[] args){

DataOutputStream os;

try

{

os =new DataOutputStream(new FileOutputStream(args[0]));

for (int n = 65; n <91;n++)

{

os.write(n);

}

}

catch (FileNotFoundException f){System.out.println("File not found " +f);}

catch (IOException io){ System.out.println("I/O error " + io);}

catch (Throwable t){System.out.println("Abnormal error " +t);}

}

}

[1626 byte] By [kikemellya] at [2007-10-2 23:36:17]
# 1
>Abnormal error java.lang.ArrayIndexOutOfBoundsException: 0Thats because you didn't use any arguments when executing your class.BTW, its not a script - its a class.
IanSchneidera at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks, yeah I know its a class, been using UNIX a little latley, about the argument, isnt this part of my script ment to provide the argument.

for (int n = 65; n <91;n++)

{

os.write(n);

}

numbers 65 - 91 are the ASCII for A-Z in caps.

Mike

kikemellya at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 3
> args[0]You need to invoke your application with at least one command-line argument, or the above will throw the exception you are seeing.java ... FileWrite application command-line arguments go here
warnerjaa at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 4

> Thanks, yeah I know its a class, been using UNIX a

> little latley, about the argument, isnt this part of

> my script ment to provide the argument.

What value do you think args[0] holds here that will get passed to new FileOutputStream(...)? Where do you think that value came from?

new FileOutputStream(args[0])

jverda at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 5
I dont get it, what does the value need to be to get A-Z, am I right in saying with args[0] I am passing an empty string to FileOutputStream? what do I need to pass? a file name to right too or data to process?thanks for the replies, Mike
kikemellya at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 6

> I dont get it, what does the value need to be to get

> A-Z, am I right in saying with args[0] I am

> passing an empty string to FileOutputStream?

No.

args is an array. It has a certain number of elements.

java MyClass ABC 123

Here, args will have 2 elements.

args[0] --> ABC

args[1] --> 123

args[2] --> ArrayIndexOutOfBoundsException

http://java.sun.com/docs/books/tutorial/java/data/arrays.html

jverda at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 7
>am I right in saying with args[0] I am passing an empty string to FileOutputStream?Even if you were right, what do you think that would do?
IanSchneidera at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 8
Okay thanks, so currently I am telling my script that the argument is element 0 of my array, which doesnt exist hence the out of bounds error. where in my script to I declare what my array is?Mike
kikemellya at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 9
> Okay thanks, so currently I am telling my script that> the argument is element 0 of my array, which doesnt> exist hence the out of bounds error. > > where in my script to I declare what my array is?Read reply 6 carefully.
jverda at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 10

Hey, I finally get it that i need to call my class from a DOS prompt, I was just executing it in the Sun One studio.

I am not at home to check but I guess this should work:

java FileWrite filename.txt

obviously from within the correct directory.

cheers people

Mike

kikemellya at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 11
BTW does anyone know if you can invoke from Sun one studio or does it have to be at a DOS prompt or BASH shell?TIAMike
kikemellya at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...
# 12
Hi, can anyone tell me if there is away to invoke this script without using a command prompt, i.e. using the sun one studio, can I include anything in my script to make it invoke itself once its executed?Mike
kikemellya at 2007-7-14 16:18:37 > top of Java-index,Java Essentials,New To Java...