0

I have such a program:

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

class C { public static void main (String[] args) throws IOException{

    System.out.println("Wpisz teks do zakodowania: ");

    String tekst;
        Scanner odczyt = new Scanner(System.in);
        tekst = odczyt.nextLine();
        System.out.println("Tekst odszyfrowany:" + tekst);
        char[]alfabet = {'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'};
        int dlugalf=26;
        System.out.print("Tekst zaszyfrowany:");

        int a = 0;

        for(int i=0;;){

            System.out.print(tekst.charAt(a));
            a++;

        }
    }   
}

After its launch, you should view the question and ask you to enter text. Then it should show the sign that I wrote, and the program must load each of these letters individually, not as a whole string. But then it pops up an error:

Exception in thread "main" java.lang.StringIndexOut OfBoundsException: String index out of range: 10
at java.lang.String.charAt(Unknown Source)
at C.main(C.java:34)

It is caused by an empty string. How can I get rid of it? I tried with this command:

if (!tekst.isEmpty() && tekst.charAt(0) == 'R');

but it did not work out.

Sorry for any mistakes; I do not speak English very well.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Kishieel
  • 1,811
  • 2
  • 10
  • 19
  • 1
    Well I don't know whatever language this post is in, but I can see you are doing an inifite loop printing by getting a char at index _a_ and _a_ will eventually go out of bounds of your array – Gordon Allocman Mar 28 '16 at 19:11

2 Answers2

2

This block of code:

int a=0;
for(int i=0;;){

  System.out.print(tekst.charAt(a));
  a++;
}

Should become

for(int a=0;a<tekst.length();a++){
     System.out.print(tekst.charAt(a));
}

As it is, your loop will try to go forever. You run out of characters in the String (when a=tekst.length()) and you get the exception.

Laurel
  • 5,965
  • 14
  • 31
  • 57
0

It seems that you want to implement text decryption with constant shift.

There is some problems with your code:

  1. It doesn't take into account upper case characters and non-letters
  2. Loop statement is wrong
  3. There is no decryption

Here is an example

final int shift = 1;//any shift here
final int alhpabetLength = 'z' - 'a';
String input = scanner.nextLine();
input = input.toLowerCase();
for (char c : input.toCharArray()) {
    if (c >= 'a' && c <= 'z') {
        int position = c - 'a';
        int decryptedPosition = (position + shift + alhpabetLength) % alhpabetLength;
        char decryptedC = (char)(decryptedPosition + 'a');
        System.out.print(decryptedC);
    } else {
        System.out.print(c);
    }
}

If you use shift = -1 than for the encrypted line "ifmmp!" you will get "hello!"

Denis Kokorin
  • 887
  • 8
  • 17
  • Exactly my point. But I want the same to happen. Without the final code: D – Kishieel Mar 28 '16 at 20:04
  • The best way to learn something is by analyzing it. You could paste code into your project, debug it several times line by line carefully watching values. Than you could delete pasted code and rewrite it by your own. – Denis Kokorin Mar 28 '16 at 20:25
  • This is good idea. – Kishieel Mar 28 '16 at 20:44
  • I have this error: C.java:32: error: possible loss of precision char decryptedP = (position + shift + da lf) % dalf; ^ required: char found: int C.java:33: error: possible loss of precision char decryptedC = decryptedP + 'a'; ^ required: char found: int 2 errors – Kishieel Mar 28 '16 at 21:09
  • Updated my answer. You could mark it as an *answer* if it helps you. – Denis Kokorin Mar 29 '16 at 04:33
  • Yes. all works nice. – Kishieel Mar 29 '16 at 15:43
  • Can you explain to me how this passage? for (char c: input.toCharArray ()) { if (c> = 'a' && c <= 'z') { int position = c - 'a'; decryptedPosition = int (position + shift + alhpabetLength)% alhpabetLength; decryptedC char = (char) (decryptedPosition + 'a'); – Kishieel Mar 29 '16 at 19:07