-3

I made a project in java, where the user writes a sentence and when he presses enter, the letters he wrote are translated into symbols like: @#!& @&!. The problem is that those symbols are being printed the one under the other, that is a problem as the second project I made, translates the symbols back to letters, but It can't translate them as there is a blank between them. I searched for it on the internet and I found this article on StackOverflow: "https://stackoverflow.com/questions/37632058/how-to-convert-a-letter-to-an-another-letter-in-java" but I can't understand how it works. Is there a way to fix my own program? Help me please! The problem may be because I saved the characters that the user put in an Array, I am not sure though. The project is being run on NetBeans and the code of my project is this:

    package kodikopoihsh2;

import java.util.Scanner;

/**
 *
 * @author user
 */
public class Kodikopoihsh2 {

    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
        // TODO code application logic here

        Scanner input = new Scanner(System.in);
        System.out.println("write your text in order to be encrypted: ");
        
        char[] letters = input.nextLine().toCharArray();
        
        String a;
        String b;
        String c;
        String d;
        String e;
        String f;
        String g;
        String h;
        String i;
        String j;
        String k;
        String l;
        String m;
        String n;
        String o;
        String p;
        String q;
        String r;
        String s;
        String t;
        String u;
        String v;
        String w;
        String x;
        String y;
        String z;
        String aa;
        String ab;

        
        for (int counter = 0;  counter < 99999; counter++){
            if(letters[counter] == 'a'){
                a = "!";
                System.out.println(a);
            }
            if(letters[counter] == 'b'){
                b = "@";
                System.out.println(b);
            }
            if(letters[counter] == 'c'){
                c = "#";
                System.out.println(c);
            }
            if(letters[counter] == 'd'){
                d = "$";
                System.out.println(d);
            }
            if(letters[counter] == 'e'){
                e = "%";
                System.out.println(e);
            }
            if(letters[counter] == 'f'){
                f = "^";
                System.out.println(f);
            }
            if(letters[counter] == 'g'){
                g = "&";
                System.out.println(g);
            }
            if(letters[counter] == 'h'){
                h = "*";
                System.out.println(h);
            }
            if(letters[counter] == 'i'){
                i = "(";
                System.out.println(i);
            }
            if(letters[counter] == 'j'){
                j = ")";
                System.out.println(j);
            }
            if(letters[counter] == 'k'){
                k = "-";
                System.out.println(k);
            }
            if(letters[counter] == 'l'){
                l = "_";
                System.out.println(l);
            }
            if(letters[counter] == 'm'){
                m = "=";
                System.out.println(m);
            }
            if(letters[counter] == 'n'){
                n = "+";
                System.out.println(n);
            }
            if(letters[counter] == 'o'){
                o = "`";
                System.out.println(o);
            }
            if(letters[counter] == 'p'){
                p = "~";
                System.out.println(p);
            }
            if(letters[counter] == 'q'){
                q = "1";
                System.out.println(q);
            }
            if(letters[counter] == 'r'){
                r = "2";
                System.out.println(r);
            }
            if(letters[counter] == 's'){
                s = "3";
                System.out.println(s);
            }
            if(letters[counter] == 't'){
                t = "4";
                System.out.println(t);
            }
            if(letters[counter] == 'u'){
                u = "5";
                System.out.println(u);
            }
            if(letters[counter] == 'v'){
                v = "6";
                System.out.println(v);
            }
            if(letters[counter] == 'w'){
                w = "7";
                System.out.println(w);
            }
            if(letters[counter] == 'x'){
                x = "8";
                System.out.println(x);
            }
            if(letters[counter] == 'y'){
                y = "9";
                System.out.println(y);
            } 
            if(letters[counter] == 'z'){
                z = "0";
                System.out.println(z);
            } 
            if(letters[counter] == ' '){
                aa = " ";
                System.out.println(aa);
            }
            if(letters[counter] == '?'){
                ab = "?";
                System.out.println(ab);
            }

           }
        }
          }

I run the program, I wrote: "password encrypted" (without quotes) and here is the output with an error which I cannot understand what it means, help me please:

    ~
    !
    3
    3
    7
    `
    2
    $
     
    %
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
    +
    #
    2
    9
    ~
    4
    %
    $
geo
  • 1
  • 3

3 Answers3

0

First of all, System.out.println() print your string and adds a new line after it. This is why each letter is on one line. Try to use System.out.print().

Then, the error means you are trying to access an index outside of the length of your array.

Why don't you try this:

for (int counter = 0;  counter < letters.length; counter++){

With this boundaries, you always will access indexes inside your letters array.

Also, fews tips :

  • take a look a switch/case syntax to improve your code readability.
  • you could store the encrypted char into an array instead of using a String for each char
  • print your first string write your text in order to be encrypted: before the Scanner. Otherwise, you would have to type your password before the string is displayed.
Maxouille
  • 2,729
  • 2
  • 19
  • 42
  • WOW! Thanks, I will try and see if it works. Thank you for your quick response! Umm, can you tell me how to use the array? – geo Mar 31 '21 at 08:56
  • OK, so: 1. I tried your solution and indeed it fixed the error Thank you very much for that! 2. I have not learnt yet how to use the switch/case syntax, can you help me with that? Though I don't think that it is necessary 3. I also don't know how to store the encrypted char into an array. Can you help me with this? 4. I printed my first string "write your text in order to be encrypted" before the Scanner. – geo Mar 31 '21 at 09:01
  • Np, and for the array, take a look at Luis solution, it's a good idea. Don't hesitate to mark my answer as a solution if it works :) – Maxouille Mar 31 '21 at 09:02
0

Your approach is very complicated. Try using a different approach by having a input letters string and translated characters string. You then just replace the char at the given position with the char of the translated string. Like so:


static String letters = "abcdefghijklmnopqrstuvwxyz0123456789"; //etc...
static String translated = "@€#!*"; //etc...

String input = "hi";

String output = "";
for(int i = 0; i < input.length(); i++){
   output += translated.charAt(letters.indexOf(input.charAt(i)));
}

Luis Ka
  • 95
  • 7
  • I can't understand vey much your code, but I tried it and it gives me some errors. Can I do something to fix it? – geo Mar 31 '21 at 09:13
  • should I send you the errors so you can fix them? – geo Mar 31 '21 at 09:16
  • Yes send the errors and I will try to explain what's wrong. Please send your whole code and the errors. – Luis Ka Mar 31 '21 at 09:20
  • Oh, sorry... I found the answer i was looking for. Thank you though for your time. – geo Apr 01 '21 at 10:14
0

Hello i think the biggest problem is your code. I don't understand it very well but I think this code might work:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    private static final Map<Character, Character> encriptionTable = new HashMap<Character, Character>() {{
        put('a', '!');
        put('b', '*');
        //....
    }};

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("write your text in order to be encrypted: ");

        char[] letters = input.nextLine().toCharArray();

        for(int i = 0; i < letters.length; i++) if(encriptionTable.containsKey(letters[i])) letters[i] = encriptionTable.get(letters[i]);

        System.out.println(letters);
    }
}
Igolta
  • 11
  • 2