简体   繁体   中英

How to access updated string from main class

It is a code for sha1 hash function. I want access the sb string from main function. I can access the sb string but not reflected in the main function.

 import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class sha1 {

    /**
     * @param args
     * @throws NoSuchAlgorithmException 
     */
    public String sb1;

    public void printkey(String convert){

        sb1=sha1(convert) ;
        System.out.println(sb1);
    }


    public String sha1(String input)  {
        StringBuffer sb = new StringBuffer();
        sha1 rr= new sha1();
        try{

            MessageDigest mDigest = MessageDigest.getInstance("SHA1");
            byte[] result = mDigest.digest(input.getBytes());

            for (int i = 0; i < result.length; i++) {
            sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));

        }

        rr.sb1=sb.toString();
        System.out.println("\n");
        System.out.println(rr.sb1);



        }
        catch ( NoSuchAlgorithmException nsae ) { 
            System.out.println(nsae);
        }
    return sb1;
    }

When I am accesing sb1 from main class it not giving the output whatever it print in printkey function. Printkey is giving right output. I want the updated sb should be seen from main function.

public static void main(String[] args) {

sha1 m=new sha1();
System.out.println("\n");
System.out.println(m.sb1);

}

Your problem is that your sha1 method is creating a new instance and setting the String of that new instance. Therefore, sb1 of your original instance (the one created in the main method) is never updated.

Change :

sha1 rr= new sha1();
....
rr.sb1=sb.toString();

To :

sb1 = sb.toString();

In addition, it doesn't look like you call printkey , which calls your sha1 method. You probably want to call it in your main method.

As I said in the comments, there are a lot of problems with your code, here is a working version:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Sha1 {
    private String value = null;

    public Sha1(final String input) {
        final StringBuffer sb = new StringBuffer();
        try {
            final MessageDigest mDigest = MessageDigest.getInstance("SHA1");
            final byte[] result = mDigest.digest(input.getBytes());

            for (int i = 0; i < result.length; i++) {
                sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
            }

            value = sb.toString();
        } catch (NoSuchAlgorithmException nsae) {
            value = null;
            nsae.printStackTrace();
        }
    }

    public String getValue() {
        return value;
    }

    public static void main(final String[] args) {
        final Sha1 testSha1 = new Sha1("test");
        System.out.println(testSha1.getValue());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM