NameSilo

Simple java OOP programing help

Spacemail by SpaceshipSpacemail by Spaceship
Watch

Jawn

Straight from SwedenEstablished Member
Impact
2
Hi im just learning java and im about to write my own encryption/decryption class.
Need abit help, it doesnt replace the character which i dont understand, it just returns the text message i wrote.


first file

Code:
class functions{

	private String text;
	private String[] encrypt = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"};
	private String[] decrypt = {"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"};


		//Encrypt text string and return result
		public void encryptText(String text){
			for(int i=0; i < 26; i++){
				this.text = text.replace(decrypt[i],encrypt [i]);
			}
		}

		//Decrypt text string and return result
		public void decryptText(String text){
			for(int i=0; i < 26; i++){
				this.text = text.replace(encrypt [i],decrypt[i]);
			}
		}


		public String getResult(){
			return text;
		}


}


second file


Code:
import java.util.Scanner;

class krypt {

	public static void main(String args[]){


		Scanner sc = new Scanner(System.in);
		functions myFunc = new functions();

		System.out.println("Type in a text to encrypt");
		String text = sc.nextLine();

		myFunc.encryptText(text);

		System.out.println(myFunc.getResult());


	}

}

Im still learning OOP so im pretty new at this
Would appreciate if someone would take a look at it
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
You need to use a StringBuffer - objects with a type of String can't be modified.
 
0
•••
enlytend said:
You need to use a StringBuffer - objects with a type of String can't be modified.


im not sure what you mean, im very new to java
 
0
•••
functions.java
PHP:
public class functions
{
  private static String encryptionString = "qwertyuiopasdfghjklzxcvbnm";
  private static String decryptionString = "abcdefghijklmnopqrstuvwxyz";
  // Encrypt text string and return result
  public static String encryptText(String txt)
  {
    String result = "";
    int i;
    for(i = 0; i < txt.length(); i++)
    {
      result += encryptionString.charAt(decryptionString.indexOf( txt.charAt(i)));
    }
    return result;
  }
  //Decrypt text string and return result
  public static String decryptText(String txt)
  {
    String result = "";
    int i;
    for(i = 0; i < txt.length(); i++)
    {
      result += decryptionString.charAt(encryptionString.indexOf( txt.charAt(i)));
    }
    return result;
  }
}
krypt.java
PHP:
import java.util.Scanner;
public class krypt 
{
  public static void main(String args[])
  {
    Scanner sc = new Scanner(System.in);
    System.out.println("Type in a text to encrypt/decrypt");
    String text = sc.nextLine();
    System.out.println("Encrypted: " + functions.encryptText(text));
    System.out.println("Decrypted: " + functions.decryptText(text));
  }
}

There you go. :)
 
Last edited:
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back