Jump to content


Reading from args


  • Please log in to reply
1 reply to this topic

#1 Bogren

Bogren

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 25 February 2012 - 08:33 PM

I'm writing a program that takes a number of inputs from a user then stores them in a text file for later use. The following code is the section of said program that writes the information to the file.

When I run the whole program I get this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at input.main(input.java:176)

I understand it is telling me that the line "FileOutputStream outFile = new FileOutputStream(args[0]);" is erroring because args[0] is out of bounds. The problem is I don't know why.

Help?

     Thanks in advance,
     Bogren

	  try {
			  FileOutputStream outFile = new FileOutputStream(args[0]);
			  OutputStreamWriter outStream = new OutputStreamWriter(outFile,(Fname+Mname+Lname));
			  PrintWriter out = new PrintWriter(outFile);

			  out.println(Fname);
			  out.println(Mname);
			  out.println(Lname);
			  out.println(gender);
			  out.println(noun);
			  for(int i = 0; i <20; i++)
			  	out.println(movies[i]);
			  for(int i = 0; i <20; i++)
			  	out.println(music[i]);
			  for(int i = 0; i <20; i++)
			  	out.println(books[i]);
			  for(int i = 0; i <20; i++)
			  	out.println(tv[i]);
			  for(int i = 0; i <20; i++)
			  	out.println(games[i]);
			  for(int i = 0; i <20; i++)
			  	out.println(activities[i]);
			  for(int i = 0; i <20; i++)
			  	out.println(dislikes[i]);
			  out.close();

		 } catch (IOException e)
		 {
		   e.printStackTrace();
		 }


#2 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • Gender:Male
  • Location:Belgium

Posted 26 February 2012 - 12:00 PM

I'm assuming you didn't actually call your program from the command line with a filename as the first argument?
http://docs.oracle.c...mdLineArgs.html

p.s. next time, please choose a better title and post your question in the right subforum so I don't have to do this for you.