Encrypt / Decrypt homework help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Encrypt / Decrypt homework help

  1. #1
    Join Date
    Oct 2004
    Posts
    2

    Encrypt / Decrypt homework help

    Hello
    I got a hw assignment in which I have to ask the user weather they want to encrypt or decrypt. then it asks them a value of N
    to do the encrypt I take the ASCII value and add N to it. Finaly I ask them a sentence.

    all has gone well except one issue. In this class, code follows, I have the methods to en/decrypt the text. when I run I get a out of memory error on the "sbb.append(aChar);" line

    Thanks for any and all help!


    import java.text.*;
    import java.lang.*;
    import java.io.*;
    import javax.swing.*;

    public class Calculator {

    private String choice;
    private int N;
    String results;
    char aChar;
    String test;

    public void setN(int n){
    N = n;
    }

    public void encrypt(String e){

    StringBuffer sbb = new StringBuffer("");

    choice = e;
    StringBuffer sb = new StringBuffer(choice);
    int length = choice.length();

    int i = 0;
    while( i < length){
    aChar = choice.charAt(i);
    aChar += N;
    sbb.append(aChar);
    }
    test = sbb.toString();
    }

    public void decrypt(String d){

    StringBuffer sbb = new StringBuffer("");

    choice = d;
    StringBuffer sb = new StringBuffer(choice);
    int length = choice.length();

    int i = 0;
    while( i < length){
    aChar = choice.charAt(i);
    aChar -= N;
    sbb.append(aChar);
    }
    test = sbb.toString();

    }


    public String sendDisplay(){
    results = test;
    return results;
    }

    }

  2. #2
    Join Date
    Sep 2004
    Posts
    150

    Re: Encrypt / Decrypt homework help

    Originally posted by tnb3993
    // Infinite loop here:

    int i = 0;
    while( i < length){
    aChar = choice.charAt(i);
    aChar += N;
    sbb.append(aChar);
    }
    test = sbb.toString();
    }

    public void decrypt(String d){

    StringBuffer sbb = new StringBuffer("");

    choice = d;
    StringBuffer sb = new StringBuffer(choice);
    int length = choice.length();



    // Infinite loop here also
    int i = 0;
    while( i < length){
    aChar = choice.charAt(i);
    aChar -= N;
    sbb.append(aChar);
    }
    test = sbb.toString();

    }

    }
    You might want to consider doing something with "i", i++ would probably work

    Good luck.

  3. #3
    Join Date
    Oct 2004
    Posts
    2
    once again I turn something that is way to easy i++ and forget it

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center