Reputation: 81
So I'm trying to accept a text file from the Linux command line into my Java program, but the compiler gives me that error mentioned in the title. It says the error occurs at the line that says "String fileName = args[0];". Does anyone happen to know why? Here is my code:
public class Parsons_Decoder
{
// method: main
// purpose: receives key-phrase and sequence of integers and
// prints the secret message to the screen.
public static void main(String[] args) throws IOException
{
String fileName = args[0];
// reads incoming file (if it exists) and saves the key-phrase to
// String variable "keyPhrase"
File testFile = new File(fileName);
if(!testFile.exists())
{
System.out.println("\nThis file does not exist.\n");
System.exit(0);
}
Scanner inputFile = new Scanner(args[0]);
String keyPhrase = inputFile.nextLine();
// creates an ArrayList and stores the sequence of integers into it
ArrayList<Integer> numArray = new ArrayList<Integer>();
while(inputFile.hasNextInt())
{
numArray.add(inputFile.nextInt());
}
// decodes and prints the secret message to the screen
System.out.println();
System.out.print("Your secret message is: ");
for(int i = 0; i < numArray.size(); i++)
{
int num = numArray.get(i);
System.out.print(keyPhrase.charAt(num));
}
System.out.println("\n");
//keyboard.close();
inputFile.close();
}
}
Upvotes: 1
Views: 119
Reputation: 522234
Update:
Your professor is asking you to read in a file with stdin, using a command like the following:
java Diaz_Decoder < secretText1.txt
Your main()
method should then look something like the following:
public static void main(String[] args) throws IOException {
// create a scanner using stdin
Scanner sc = new Scanner(System.in);
String keyPhrase = inputFile.nextLine();
// creates an ArrayList and stores the sequence of integers into it
ArrayList<Integer> numArray = new ArrayList<Integer>();
while (inputFile.hasNextInt()) {
numArray.add(inputFile.nextInt());
}
// decodes and prints the secret message to the screen
System.out.println();
System.out.print("Your secret message is: ");
for (int i = 0; i < numArray.size(); i++) {
int num = numArray.get(i);
System.out.print(keyPhrase.charAt(num));
}
System.out.println("\n");
}
Upvotes: 4
Reputation:
Based on your description and the link you provided (which should be in the question, not a comment), your prof wants you to write a program that accepts the contents of a file via "standard in" (STDIN) when run as a POSIX style shell command line using redirection.
If this is indeed a requirement, you can't just read the file given as an argument, but need to change your program such that it reads from STDIN. The key concept here is that the "<" is not available to your program argument list. It will be consumed by the shell (Bash, Ksh, etc.) running the Java process, and a "pipe" setup between the file on the right side and the process on the left side. In this case, the process is your Java process running your program.
Try doing a search for "java STDIN" to get some ideas on how to write a Java program that can read its standard in.
By the way, if your program crashes with an ArrayIndexOutOfBoundError when run with redirection in this manner, it still has a bug in it. You need to test for and handle the case where you have 0 file arguments after the shell has finished processing the command line. If you want full marks, you need to handle the error and edge cases.
Upvotes: 1