0

I've been trying to get this to work. It complies but my char array won't display anything.

'cipher2.in' contains this:
3 L E ABCDEFGHIJKLMNOPQRSTUVWXYZ

Output:
Cipher Code:
Cipher Text: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher Key: 3

import java.util.*;
import java.io.*;

public class Test2
{
	public static void main (String[] args) throws IOException
	{
		Scanner input = new Scanner(new FileReader("cipher2.in"));
		String cipherText;
		String cipherDirection;
	        String cipherType;
		int cipherKey;
	
		cipherKey = input.nextInt();
		cipherDirection = input.next();
		cipherType = input.next();
		cipherText = input.next();

		encrypt(cipherText, cipherKey);
	}

	public static void encrypt(String cipherText, int cipherKey)
	{	
		int letterPosition;
		int  arrayPosition;
		char[] cipherCode = new char[cipherText.length()];
		letterPosition = cipherKey;

		for (arrayPosition = 0; arrayPosition == cipherText.length(); arrayPosition++, letterPosition++)
		{
			if (cipherKey > 0 && letterPosition == cipherText.length())
			{	
				for (letterPosition = 0; letterPosition == cipherKey; letterPosition++, arrayPosition++)
				{
					cipherCode[arrayPosition] = cipherText.charAt(letterPosition);					
				}
			}
			
			else 
			{
				cipherCode[arrayPosition] = cipherText.charAt(letterPosition);
				System.out.print(cipherCode[arrayPosition]); 
			}
		
		}
		
		
		System.out.print("Cipher Code: ");
		
		for(int i = 0; i < cipherText.length(); i++)
		{
			System.out.print(cipherCode[i]); 
		}
		
		System.out.println("\nCipher Text: " + cipherText);
		System.out.println("Cipher Key: " + cipherKey);		
	}

}
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by hfx642
1
//Wrong
for (arrayPosition = 0; arrayPosition == cipherText.length(); arrayPosition++, letterPosition++)

//Right
for (arrayPosition = 0; arrayPosition < cipherText.length(); arrayPosition++, letterPosition++)

You have used == instead of less or greater then symbol on line 29

Votes + Comments
good catch
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.