To push a value of a int into a string without reassigning the original string value

StackOverflow https://stackoverflow.com/questions/22741926

  •  24-06-2023
  •  | 
  •  

Question

So i have a value that is being returned from a class as an integer and this done several times according to the length of the text entered. I can print out the output for each character but i would like to combine the output into one string. This is an example of the output.

Enter plain-text: hi

The vaule of H is 7

The encrypted value is 721 The decrypted value is 7

The vaule of I is 8

The encrypted value is 330 The decrypted value is 8

I would like it to print out the final cipher text as "721330", so that it would send it to a database and compare that value with the value store at the database.

What would be the best way to do this?

@SuppressWarnings("resource") public static void main(String[] args) {

    int p = 29;
    int q = 41;
    int d = 83;
    int n = 0;
    int w = 0;
    int e=27;
    int c =1;
    String plainText;
    String Str = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    //System.out.println(m);
    n = p*q;
    //System.out.println("The n value equals " + n);
    w = (p-1)*(q-1);
    //System.out.println("The w value equals " + w);

    int qo=0;
    int re=0;

    w = w*1+d*0;
    d = w*0+d*1;


    qo = w/d;
    //System.out.println("quotient value " + qo);
    re = w-qo*d;
    //System.out.println("reminder value " + re);
    qo=d/re;
    //System.out.println("quotient value " + qo);
    re = d-qo*re;
    //System.out.println("reminder value " + re);
    Scanner scn=new Scanner(System.in);

    System.out.println("Enter plain-text:");
    plainText=scn.nextLine();

    plainText=plainText.toUpperCase();
    char[] chars = plainText.toCharArray();

    for (int i=0;i<chars.length;i++)
    { 

    int m= Str.indexOf(chars[i]);

    System.out.println("The value of " +chars[i] + " is " + m +"\n");

    toBin(e);
    //System.out.println("The binary value of e " + toBin(e));
    de_encryption(c,n,m,toBin(e));
    int C = de_encryption(c,n,m,toBin(e));

    //String final_output = String.valueOf(C);

    StringBuilder sb = new StringBuilder(); 
    int part = C;
    sb.append(part);

    String result = sb.toString();
    //I want the final_output value to be appended into a stringbuilder
    System.out.println("The encrypted value is " + result);

    toBin(d);
    //System.out.println("The binary value of d " + toBin(d));

    int D = de_encryption(c,n,C,toBin(d));
    StringBuilder sb1 = new StringBuilder(); 
    int part1 = D;
    sb1.append(part1);

    String result1 = sb1.toString();
    System.out.println("The decrypted value is "+ result1 +"\n");


    }

}
Was it helpful?

Solution

If I understand correctly, you're asking us how to create a String from multiple parts, by concatenating the parts.

Use a StringBuilder:

StringBuilder sb = new StringBuilder();
for (...) { 
    String part = ...;
    sb.append(part);
} 
String result = sb.toString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top