0

So, I'm not sure what is happening here, if I place encode or decode in the args[0], it should work right, but it doesn't. I have all the imports and I have a utility class that I'm using as well. I don't understand why, when I run the program with these arguments: java Prog4 encode fly message.txt it won't work right. It'll go straight to the last else statement.

    public class Prog4 {
        public static void main(String[] args){
            if (args.length != 3){
                    System.out.println("Enter the right amount of arguments!");
                    System.exit(0);
            }

            String command=args[0];
            String key= args[1];
            String fileName = args[2];
            File file = new File(args[2]);
            String fileExtention="";
            if(args[0]=="encode"){
                    fileExtention=".crypt";

            }
            else if (args[0]=="decode"){
                    fileExtention=".decrypt";
            }
            else{
                    System.out.println("Enter decode or encode!");
                    System.exit(0);
            }
Melika Barzegaran
  • 429
  • 2
  • 9
  • 25

2 Answers2

0

try this:

args[0].equals("encode")

and this:

args[0].equals("decode")

to compare the strings in java...

you use == to check if the references are equal.

you should use .equals() to check if the values are equal.

adrCoder
  • 3,145
  • 4
  • 31
  • 56
0
args[0]=="encode"

is WRONG!

it checks for object reference equality, not value equality!

use:

args[0].equals("encode");

or

args[0].equalsIgnoreCase("encode");

to ignore the case

EDToaster
  • 3,160
  • 3
  • 16
  • 25