Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Using args level arguments

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Using args level arguments

    What I need to do is use the args command to choose which method to go to. For instance, I have methods encrypt, decrypt, and crack. The user will open their command and prompt this:
    C:\Users\Name\File>java myCodeName methodchoice inputfile outputfile key
    Where myCodeName is the name of my program, methodchoice is either encrypt, decrypt or crack; inputfile is whatever file they want to input, outputfile whatever they want to output and key is the key to the encryption (not necessary for using the crack method)

    I'm just unsure how to pull each string from the args in order so that I can do repeated if loops or something like that. Here is my code:
    public class Encrypt {
    	public static void main(String[] args) {
    		String command = args;
    		if (command.equalsIgnoreCase(encrypt) {
    			}
     
    	}
    	public static void encrypt(char[] array, int key) {
    		try {
    			FileWriter filewriter = new FileWriter(new File(""));
    			for (int i = 0; i < array.length; i++) {
    				char space = ' ';
    				if (array[i] != space) {
    					int charAsNum = (int) array[i] -97;
    					int decode = charAsNum + key;
    					if (decode > 25) {
    						decode = decode % key;
    					}
    					decode = decode + 97;
    					char numAsChar = (char)decode;
    					filewriter.write(numAsChar);
    				}
    				else {
    					char numAsChar = ' ';
    					filewriter.write(numAsChar);
    				}
    			}
    			filewriter.close();
     
    		}
    		catch (java.io.IOException ex) {
    			System.out.println("File not created");
    			System.exit(-1);  
    		}
     
    	}
    	public static void decrypt(char[] array, int key) {
    		try {
    			FileWriter filewriter = new FileWriter(new File("decrypt.txt"));
    			for (int i = 0; i < array.length; i++) {
    				char space = ' ';
    				if (array[i] != space) {
    					int charAsNum = (int) array[i] -97;
    					int decode = charAsNum - key;
    					if (decode < 0) {
    						decode = decode + 25;
    					}
    					if (decode > 25) {
    						decode = decode % key;
    					}
    					decode = decode + 97;
    					char numAsChar = (char)decode;
    					filewriter.write(numAsChar);
    				}
    				else {
    					char numAsChar = ' ';
    					filewriter.write(numAsChar);
    				}
    			}
    			filewriter.close();
     
    		}
    		catch (java.io.IOException ex) {
    			System.out.println("File not created");
    			System.exit(-1);  
    		}
    	}
    	public static void crack(char[] array) {
    				int[] code = new int[array.length];
    				for (int i = 0; i < array.length; i++) {
    					char space = ' ';
    					if (array[i] != space) {
    						code[i] = (int)array[i] - 97;
    					}
    					if (array[i] == space) {
    						code[i] = 27;
    					}
    				}
    				int[] count = new int[28];
    				for (int i = 0; i <code.length; i++) {
    					count[code[i]]++;
    				}
    				int max = count[0];
    				int indexOfMax = 0;
    				for (int i = 1; i < count.length; i++) {
    					if (count[i] > max && count[i] < count[27]) {
    						max = count[i];
    						indexOfMax = i;
    					}
    				}
    				indexOfMax = indexOfMax + 97;
    				int key = 101 - indexOfMax;
    				key = key * -1;
    				decrypt(array ,key);
    		}
    	}
    }

    I am also unsure on how to turn the filewriter into the name they want, but I'm fairly certain that can be done by adding a file x to the method's parameters itself.
    Last edited by taze; November 7th, 2013 at 07:28 PM. Reason: new question.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using args level arguments

    how to pull each string from the args
    java myCodeName methodchoice inputfile outputfile key
     args[] indexes:      0          1         2        3
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Using args level arguments

    So I have it all done, now I just have one really bizarre issue.
    You know Archie Comic's Svenson? For some reason that is the accent of my decryption software.
    The output that is supposed to come out is:
    \he more you eat the better you feel beans beans for every meal
    the key to success is to be open to opportunity
    Instead, this is what comes out:
    the more vou eat the better vou feel beans beans for everv mealthe kev to success is to be open to opportunitv
    a) I can't get the lines to separate so it is the format above and b) all my 'y's are being turned into 'v's.

    My current code:
    import java.util.*;
    import java.io.*;
    public class Encri {
    	public static void main(String[] args) {
    		try { 
    			String command = args[0];
    			String input = args[1];
    			String output = args[2];
    			File file = new File(input);
    			Scanner scanner = new Scanner(file);
    			String theString = scanner.nextLine();
    			while (scanner.hasNextLine()) {
    			       theString = theString + scanner.nextLine();
    			}
    			char[] charArray = theString.toCharArray();
    			if (command.equalsIgnoreCase("encrypt")) {
    					String keyString = args[3];
    										Scanner scanned = new Scanner(keyString);
     
    					int key = scanned.nextInt();
    					encrypt(charArray, key, output);
    			}
    			if (command.equalsIgnoreCase("decrypt")) {
    				String keyString = args[3];
    								Scanner scanned = new Scanner(keyString);
    				int key = scanned.nextInt();
    				decrypt(charArray, key, output);
     
     
    			}
    			if (command.equalsIgnoreCase("crack")) {
    				crack(charArray,output);
    			}
    		}
    		catch (java.io.FileNotFoundException ex) {  
    		System.out.println("File not found.");
    		}
     
    	}
    	public static void encrypt(char[] array, int key, String output) {
    		try {
    			FileWriter filewriter = new FileWriter(new File(output));
    			for (int i = 0; i < array.length; i++) {
    				char space = ' ';
    				if (array[i] != space) {
    					int charAsNum = (int) array[i] -97;
    					int decode = charAsNum + key;
    					if (decode > 25) {
    						decode = decode % key;
    					}
    					decode = decode + 97;
    					char numAsChar = (char)decode;
    					filewriter.write(numAsChar);
    				}
    				else {
    					char numAsChar = ' ';
    					filewriter.write(numAsChar);
    				}
    			}
    			filewriter.close();
     
    		}
    		catch (java.io.IOException ex) {
    			System.out.println("File not created");
    			System.exit(-1);  
    		}
     
    	}
    	public static void decrypt(char[] array, int key, String output) {
    		try {
    			FileWriter filewriter = new FileWriter(new File(output));
    			for (int i = 0; i < array.length; i++) {
    				char space = ' ';
    				if (array[i] != space) {
    					int charAsNum = (int) array[i] -97;
    					int decode = charAsNum - key;
    					if (decode < 0) {
    						decode = decode + 25;
    					}
    					if (decode > 25) {
    						decode = decode % key;
    					}
    					decode = decode + 97;
    					char numAsChar = (char)decode;
    					filewriter.write(numAsChar);
    				}
    				else {
    					char numAsChar = ' ';
    					filewriter.write(numAsChar);
    				}
    			}
    			filewriter.close();
     
    		}
    		catch (java.io.IOException ex) {
    			System.out.println("File not created");
    			System.exit(-1);  
    		}
    	}
    	public static void crack(char[] array, String output) {
    		int[] code = new int[array.length];
    		for (int i = 0; i < array.length; i++) {
    			char space = ' ';
    			if (array[i] != space) {
    				code[i] = (int)array[i] - 97;
    			}
    			if (array[i] == space) {
    				code[i] = 27;
    			}
    		}
    		int[] count = new int[28];
    		for (int i = 0; i <code.length; i++) {
    			count[code[i]]++;
    		}
    		int max = count[0];
    		int indexOfMax = 0;
    		for (int i = 1; i < count.length; i++) {
    			if (count[i] > max && count[i] < count[27]) {
    				max = count[i];
    				indexOfMax = i;
    			}
    		}	
    		indexOfMax = indexOfMax + 97;
    		int key = 101 - indexOfMax;
    		key = key * -1;
    		decrypt(array,key,output);
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using args level arguments

    the lines to separate
    What happened to the newline characters that were in the input file? If they are not preserved, the lines will be run together.


    all my 'y's are being turned into 'v's.
    Have you tested the code with all 26 letters: abc...xyz to see if any others are a problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Using args level arguments

    I fixed the second (the y's being v's) with a minor tweak in the decrypt method, I have no idea how, in the above code, I can save the newline characters so the output will print out correctly.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using args level arguments

    What if the code encrypts all the characters in the input file including the newline characters?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. encrypting and decrypting a string
    By mist4lyf in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 9th, 2013, 08:41 AM
  2. Replies: 0
    Last Post: March 28th, 2013, 06:27 AM
  3. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM
  4. Replies: 0
    Last Post: September 6th, 2012, 10:57 AM
  5. Problem decrypting with AES
    By wmd in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 17th, 2012, 09:41 AM