blueknight
blueknight

Reputation: 3

making a line of text from a text file into a cipher text in java

I have a line of text that I need to decrypt into a cipher text.

Let's say my line of text is abc def ghijklm n opq rstu vwx yz

and I want an output like this: aei qu c k rvzdhmptxbfjn y glosm

lets say I entered my "key" as 5. The code then will enter every 5th element of the array o f strings of the text from the text file.

This is the code I have come up with and I have hit a wall on what to do.

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

public class Files1 {


public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    int key;

    System.out.print("Enter file: ");
    String fileName = input.nextLine();
    System.out.print("Please enter your Cipher Key: ");
    key = input.nextInt();

    Scanner inputStream = null;
    System.out.println("File name is: " + fileName);

    try {
        inputStream = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        System.out.println("Error opening the file" + fileName);
        System.exit(0);
    }

    while (inputStream.hasNextLine()) {
        String text = inputStream.nextLine();
        System.out.println(text);

        char arrayText[] = text.toCharArray();
        for (int i = 0; i < arrayText.length; i += key) {
            System.out.print("\n" + arrayText[i]);
        }

    }
}

}

Here is whats happening in the console:

Enter file: abc.txt

File name is: abc.txt
abc def ghijklm n opq rstu vwx yz

a

e

i



q

u

Upvotes: 0

Views: 1995

Answers (3)

blueknight
blueknight

Reputation: 3

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

public class Files1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    int key;

    System.out.print("Enter file: ");
    String fileName = input.nextLine();
    System.out.print("Please enter your Cipher Key: ");
    key = input.nextInt();

    Scanner inputStream = null;
    System.out.println("File name is: " + fileName);

    try {
        inputStream = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        System.out.println("Error opening the file" + fileName);
        System.exit(0);
    }

    while (inputStream.hasNextLine()) {
        String text = inputStream.nextLine();
        System.out.println(text);
        for (int i = 0; i < text.length(); i++) {
            System.out.print(text.charAt((i * key) % text.length()));
        }

    }
}

}

MANY THANKS TO EJP AND Pai!

i learned alot!

Upvotes: 0

user207421
user207421

Reputation: 310980

You don't specify what should happen to the spaces, or what happens when wraparound is required, but assuming spaces are significant and wrap-around just happens naturally:

for (int i = 0; i < text.length(); i++)
{
    System.out.print(text.charAt((i*5) % text.length()));
}

prints aei qu c k rvzdhmptxbfjn y glosw, which strongly suggests an error in your expected output.

Upvotes: 0

Pai
Pai

Reputation: 85

What you need is a circular list.

Here is a very simple and crude implementation of a circular list using arrays.

import java.util.Iterator;
import java.util.List;

public class CircularList implements Iterator<String> {

    private String[] list;

    private int pointerIndex;

    private int key;

    public CircularList(String[] list, int key) {
        this.list = list;
        pointerIndex = 1 - key;
        this.key = key;
    }

    @Override
    public boolean hasNext() {
        if(list.length == 0){
            return false;
        }
        return true;
    }

    @Override
    public String next() {
        if(pointerIndex + key > list.length) {
            int diff = (list.length-1) - pointerIndex;
            pointerIndex = key - diff;
            return  list[pointerIndex];
        }else {
            pointerIndex = pointerIndex + key;
            return list[pointerIndex];
        }
    }

    @Override
    public void remove() {
        //Do Nothing
    }

}

Once you have a list in which you can iterate in a circular fashion, you can change you existing implementation to this -

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

public class Files1 {

    public static void main(String[] args) {

        System.out.print("Enter file: ");
        Scanner input = new Scanner(System.in);
        String fileName = input.nextLine();
        Scanner inputStream = null;
        System.out.println("" + fileName);

        try {
            inputStream = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("Error opening the file: " + fileName);
            System.exit(0);
        }

        while (inputStream.hasNextLine()) {
            String text = inputStream.nextLine();
            System.out.println(text);

            String[] splits = text.split("");
            CircularList clist = new CircularList(splits, 5);

            for (int i = 0; i < splits.length -1; i += 1) {
                System.out.print("" + clist.next());
            }

        }
    }

}

Output -

Enter file: resources\abc.txt
resources\abc.txt
abc def ghijklm n opq rstu vwx yz
aei qu c k rvzdhmptxbfjn  y glosw

Also the last character in your cipher should be 'w' and not 'm'.

Upvotes: 1

Related Questions