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 2 of 2

Thread: Basic Java Encryption

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Basic Java Encryption

    public static String encryptLine(char[] cipher, String line) {		
    	char[] alphabet ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
    		'q','r','s','t','u','v','w','x','y','z'};
            	for (int j = 0; j < 26; j++) {
    		char b = alphabet[j];
    		char c = cipher[j];
    		String bString = Character.toString(b);
    		String cString = Character.toString(c); line = line.replace(bString, cString);  		
    	        }			
    	return line;
    }

    String line is later outputted by a PrintStream. The portion that is wrong with my program is shown above. I am trying to encrypt lines of String in a text file. The above code doesn't work because once you replace a character from cipher[], it can later be changed again during the loop. Any ideas on how I should go about doing this?
    Last edited by BronxBomber; April 9th, 2010 at 06:40 AM.

  2. The Following User Says Thank You to BronxBomber For This Useful Post:

    rcook6 (May 3rd, 2012)


  3. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Basic Java Encryption

    Iterate over the length of the string and not the size of the alphabet. For example
    for ( int i = 0; i < line.length(); i++ ){
        //replace character at position i in line with your encryption value
    }

Similar Threads

  1. Few very basic Java questions.
    By 01001010 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 13th, 2010, 01:14 PM
  2. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM
  3. Basic Java Program Help
    By roaster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 6th, 2009, 10:28 PM
  4. Some basic questions.
    By trips in forum Java Theory & Questions
    Replies: 5
    Last Post: July 21st, 2009, 02:15 AM
  5. help us in eclipse basic codes
    By bil_Imma in forum AWT / Java Swing
    Replies: 1
    Last Post: January 24th, 2009, 06:02 PM