Results 1 to 6 of 6
  1. #1
    dantzig is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default I am having issues to run this code ?

    the idea is we want generate the Key stream for encryption and decryption, we will process with the following steps
    we will start with following deck
    21 24 2 5 9 15 18 1 16 12 27 4 10 19 7 20 23 26 13 3 28 25 22 8 6 14 11 17

    Step 1: Swap the A joker (27) with the card following it. Thus, we swap 27 and 4.

    21 24 2 5 9 15 18 1 16 12 4 27 10 19 7 20 23 26 13 3 28 25 22 8 6 14 11 17

    Step 2: Move the B joker (28) down two places.

    21 24 2 5 9 15 18 1 16 12 4 27 10 19 7 20 23 26 13 3 25 22 28 8 6 14 11 17

    Step 3: Do the triple cut. Everything above the first joker (27, in this case) goes to the bottom of the deck. Everything below the second joker (28) goes to the top.

    8 6 14 11 17 27 10 19 7 20 23 26 13 3 25 22 28 21 24 2 5 9 15 18 1 16 12 4

    Step 4: The value of the bottom card is 4, so we take the first four cards of the deck and place them right before the 4.

    17 27 10 19 7 20 23 26 13 3 25 22 28 21 24 2 5 9 15 18 1 16 12 8 6 14 11 4

    Step 5: The top card is 17. So the keystream value is the 18th card, i.e., 9.
    Thank you.















    public class Deck {
    private LinkedList<Integer> deck ;

    public Deck(int[] deck)
    {
    Set s = new HashSet();
    for (int i = 1; i<29 ; i++)
    {
    s.add(i);
    }
    for (int i = 0 ; i<deck.length ; i++)
    {
    if (!s.contains(deck[i]));
    System.out.println("The deck doesn't contains the integers from 1 to 28");

    }
    }
    private int[] toIntArray(LinkedList<Integer> deck){

    {
    int[] array = new int[deck.size()];
    for(int i = 0;i < array.length;i++)
    array[i] = deck.get(i);
    return array;
    }
    }

    // private void tripleCut(LinkedList<Integer> deck){
    private LinkedList<Integer> tripleCut(){

    int[] b = toIntArray(deck);
    for (int i = 0 ; i<b.length ;i++)
    {
    if (b[i]== 27)
    {
    int temp = b[i];
    b[i]=b[i+1] ;
    b[i] = temp ;

    }
    }
    int[]c = b ;
    for (int i = 0 ; i <c.length ; i++)
    {
    if (c[i] == 28)
    {
    int temp1 = c[i];
    c[i]= c[i+1];
    c[i]= temp1 ;
    int temp2 = c[i+1] ;
    c[i+1]=c[i+2];
    c[i+2]= temp2 ;


    }

    }


    deck = new LinkedList(Arrays.asList(c));
    int pos = 0 ;
    int[] temp = new int[deck.size()] ;

    if (deck.indexOf(27) <deck.indexOf(28) ){


    // copy back to the front of middle
    for (int i = deck.indexOf(28)+1 ; i<deck.size() ;i++){
    temp[pos]= deck.get(i) ;
    pos++ ;
    }
    // copy middle section to the middle of temp
    for (int i =deck.indexOf(27); i<deck.indexOf(28) ; i++){
    temp[pos]=deck.get(i);

    }
    // Copy first section to temp array
    for (int i = 0; i < deck.indexOf(27); i++)
    {
    temp[pos] = deck.get(i);
    pos++;
    }
    //Copy temp array back to original array
    for (pos = 0; pos <deck.size(); pos++)
    {
    deck.set(pos, temp[pos]);
    }

    }
    //if the joker 28 is after 27 in the deck
    else{
    // copy back to the front of middle
    for (int i = deck.indexOf(27)+1 ; i<deck.size() ;i++){
    temp[pos]= deck.get(i) ;
    pos++ ;
    }
    // copy middle section to the middle of temp
    for (int i =deck.indexOf(28); i<deck.indexOf(27) ; i++){
    temp[pos]=deck.get(i);

    }
    // Copy first section to temp array
    for (int i = 0; i < deck.indexOf(28); i++)
    {
    temp[pos] = deck.get(i);
    pos++;
    }
    //Copy temp array back to original array
    for (pos = 0; pos <deck.size(); pos++)
    {
    deck.set(pos, temp[pos]);

    }
    int t1 = deck.get(0);
    int t2 = deck.get(1);
    int t3 = deck.get(2);
    int t4 = deck.get(3);

    for (int i = 4 ; i<deck.size()-1 ; i++){
    deck.set(i-4, deck.get(i)) ;
    }
    deck.set(23, t1);
    deck.set(24, t2);
    deck.set(25, t3);
    deck.set(26, t4);
    }

    return deck;
    }


    public int getKeystreamValue(){
    int x = deck.get(0) + 1 ;
    return deck.get(x);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I am having issues to run this code ?

    OK, you've posted your assignment, you've posted some unformatted code, and have mentioned that you are having "issues" but I think that for us to be able to help you, you need to tell us more.

    • First and foremost, tell us the details of the problems you're having.
    • If the code doesn't compile, please post any and all error messages, and indicate which lines in the code are the source of these errors.
    • Likewise for any runtime exceptions -- please post the full exception stacktrace and indicate which lines are causing the errors.
    • Consider editing your post above and adding [code] [/code] tags around your pasted code so that it will retain its formatting and be easier to read.


    Best of luck and welcome to the java-forums. org!

  3. #3
    dantzig is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: I am having issues to run this code ?

    Thank you.
    Let me send it again with the main method.

  4. #4
    dantzig is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: I am having issues to run this code ?

    That is the error
    Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to java.lang.Integer
    at WhitespaceString.tripleCut(WhitespaceString.java:1 39)
    at WhitespaceString.main(WhitespaceString.java:52)





    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.Scanner;
    import java.io.File;
    import java.lang.String;

    public class WhitespaceString{
    static LinkedList <Integer> deck ;
    private static int[] c;


    public static void main(String[] args){


    deck = new LinkedList<Integer>() ;
    deck.add(21);
    deck.add(24);
    deck.add(2);
    deck.add(5);
    deck.add(9);
    deck.add(15);
    deck.add(18);
    deck.add(1);
    deck.add(16);
    deck.add(12);
    deck.add(27);
    deck.add(4);
    deck.add(10);
    deck.add(19);
    deck.add(7);
    deck.add(20);
    deck.add(23);
    deck.add(26);
    deck.add(13);
    deck.add(3);
    deck.add(28);
    deck.add(25);
    deck.add(22);
    deck.add(8);
    deck.add(6);
    deck.add(14);
    deck.add(11);
    deck.add(17);

    //21 24 2 5 9 15 18 1 16 12 27 4 10 19 7 20 23 26
    // 13 3 28 25 22 8 6 14 11 17

    int []a = toIntArray(deck) ;
    int x = getKeystreamValue();
    System.out.println("The value of key stream is"+ x);
    LinkedList<Integer> l = tripleCut();
    for (int i = 0 ; i<l.size() ; i++){
    System.out.println(l.get(i));
    }


    }




    private static int[] toIntArray(LinkedList<Integer> deck){


    int[] array = new int[deck.size()];
    for(int i = 0;i < array.length;i++){
    array[i] = deck.get(i);
    System.out.println(array[i]) ;

    }
    return array;
    }
    private static LinkedList<Integer> tripleCut(){

    int[] b = toIntArray(deck);
    for (int i = 0 ; i<b.length ;i++)
    {
    if (b[i]== 27)
    {
    int temp = b[i];
    b[i]=b[i+1] ;
    b[i] = temp ;

    }
    }
    int[]c = b ;
    for (int i = 0 ; i <c.length ; i++)
    {
    if (c[i] == 28)
    {
    int temp1 = c[i];
    c[i]= c[i+1];
    c[i]= temp1 ;
    int temp2 = c[i+1] ;
    c[i+1]=c[i+2];
    c[i+2]= temp2 ;


    }

    }


    deck = new LinkedList(Arrays.asList(c));
    int pos = 0 ;
    int[] temp = new int[deck.size()] ;

    if (deck.indexOf(27) <deck.indexOf(28) ){


    // copy back to the front of middle
    for (int i = deck.indexOf(28)+1 ; i<deck.size() ;i++){
    temp[pos]= deck.get(i) ;
    pos++ ;
    }
    // copy middle section to the middle of temp
    for (int i =deck.indexOf(27); i<deck.indexOf(28) ; i++){
    temp[pos]=deck.get(i);

    }
    // Copy first section to temp array
    for (int i = 0; i < deck.indexOf(27); i++)
    {
    temp[pos] = deck.get(i);
    pos++;
    }
    //Copy temp array back to original array
    for (pos = 0; pos <deck.size(); pos++)
    {
    deck.set(pos, temp[pos]);
    }

    }
    //if the joker 28 is after 27 in the deck
    else{
    // copy back to the front of middle
    for (int i = deck.indexOf(27)+1 ; i<deck.size() ;i++){
    temp[pos]= deck.get(i) ;
    pos++ ;
    }
    // copy middle section to the middle of temp
    for (int i =deck.indexOf(28); i<deck.indexOf(27) ; i++){
    temp[pos]=deck.get(i);

    }
    // Copy first section to temp array
    for (int i = 0; i < deck.indexOf(28); i++)
    {
    temp[pos] = deck.get(i);
    pos++;
    }
    //Copy temp array back to original array
    for (pos = 0; pos <deck.size(); pos++)
    {
    deck.set(pos, temp[pos]);

    }
    int t1 = deck.get(0);
    int t2 = deck.get(1);
    int t3 = deck.get(2);
    int t4 = deck.get(3);

    for (int i = 4 ; i<deck.size()-1 ; i++){
    deck.set(i-4, deck.get(i)) ;
    }
    deck.set(23, t1);
    deck.set(24, t2);
    deck.set(25, t3);
    deck.set(26, t4);
    }

    return deck;
    }


    public static int getKeystreamValue(){
    int x = deck.get(0) + 1 ;
    return deck.get(x);
    }


    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I am having issues to run this code ?

    Again, please edit your post above and add the code tags as described in my first post. Please indicate which line is causing the exception, again as I have requested previously.

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: I am having issues to run this code ?

    dantzig,

    As I have free time and want the refresher, I have taken the liberty of debugging your program. There are multiple errors within your code but you can address these one at a time. The first issue is to get your code to compile.

    Comment out or remove the code below and take it from there.
    Java Code:
    deck = new LinkedList(Arrays.asList(c));
    Regards.
    Last edited by Ronin; 06-24-2012 at 02:13 AM. Reason: Typo

Similar Threads

  1. GUI Issues
    By larry_d1990 in forum Advanced Java
    Replies: 2
    Last Post: 03-17-2011, 02:35 PM
  2. having issues with this java code...please help out
    By oenhouse in forum New To Java
    Replies: 4
    Last Post: 12-20-2010, 01:21 PM
  3. Major issues with code - Please help
    By aldorfski_17 in forum New To Java
    Replies: 3
    Last Post: 03-29-2010, 04:46 AM
  4. jdk issues
    By artemff in forum New To Java
    Replies: 3
    Last Post: 01-02-2010, 03:18 AM
  5. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •