Q:

Swap Characters Using Command Prompt

Trying to write a program called SwapChars.java using command line arguments that allows any two characters to be swapped. For example if the program was called SwapChars then to swap all ‘a’s and ‘b’s in a file test.txt we would type 
java SwapChars test.txt ab

I typed the following code which kept giving me an exception error and I'm not sure where I'm going wrong...
import java.util.*;
import java.io.*;
public class SwapChars {

    public static void main(String[] args) throws IOException {
        String a = args[0]; //file name (test.txt entered into command line)
        FileReader fr = new FileReader(a);
        String b = args[1];//which characters to swap
        int t = fr.read();
        while(t!=-1)
        {
            if(t==b.charAt(0))
            {
                System.out.println(b.charAt(1));
            }
            else if(t==b.charAt(1))
            {
                System.out.println(b.charAt(0));
            }
            else
            {
                System.out.println((char)t);
            }
                    t=fr.read();
        }
        // TODO Auto-generated method stub

    }

}

Exception error as follows:
Exception in thread "main" java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at SwapChars.main(SwapChars.java:7)

I can get the desired output when I change my code to remove the need for commands as follows:
import java.util.*;
import java.io.*;
public class SwapCharsTest {

public static void main(String[] args) throws IOException{
    FileReader fr = new FileReader("test.txt");
    String swap = "ab";
    int t = fr.read();
    while(t!=-1)
    {
        if(t==swap.charAt(0))
        {
            System.out.print(swap.charAt(1));
        }
        else if(t==swap.charAt(1))
        {
            System.out.print(swap.charAt(0));
        }
        else
        {
            System.out.print((char)t);
        }
        t=fr.read();
    }
    // TODO Auto-generated method stub

}

}

A:

The exception is because the file cannot be found. Try to refer to the file with a full path.
Also, when the file is found, your program will loop forever. And the printout can easily be improved. Make these two changes:
        else
        {
            System.out.println((char) t);                      
        }
        t = fr.read();
    }

(t = fr.read(); reads a new char for every loop, (char) casts the integer so it looks better on screen)
If you don't want to use a full path, you need to figure out where Java will look for the file. This will (most likely) show you where to put the file:
System.out.println(new File("dummy").getAbsolutePath());

