I can get my bitcoin balance from private key but when i tried retrieving from my address its not possible posted below my code can any one tell me what i'm doing wrong
public static void main( String[] args )
{
NetworkParameters params = TestNet3Params.get();
WalletAppKit kit = new WalletAppKit(params, new File("."), "sano");
kit.start();
kit.isRunning();
Address address = new Address(params, "mj255GgoGKN6uTjtWdNcF7obu88iLSTZdm");
boolean add =kit.wallet().isAddressWatched(address);
Wallet wallet = new Wallet(params);
DumpedPrivateKey key = new DumpedPrivateKey(params,
"cTH6YyRZSqF8VvtjkfqABqjGF682cQkW3mMuageraH4CEzCrweqT");
wallet.addKey(key.getKey());
wallet.addWatchedAddress(new Address(params, "mj255GgoGKN6uTjtWdNcF7obu88iLSTZdm"));
BlockChain chain = new BlockChain(params, wallet,
new MemoryBlockStore(params));
PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.addPeerDiscovery(new DnsDiscovery(params));
peerGroup.addWallet(wallet);
peerGroup.start();
peerGroup.downloadBlockChain();
BigInteger balance = wallet.getBalance();
System.out.println("Wallet balance: " + balance);
}
Edit 1: The below given code retrieves balance from address but each and everytime it is creating a new address.
public static void main(String[] args) {
NetworkParameters params = TestNet3Params.get();
String filePrefix = "forwarding-service-testnet";
WalletAppKit kit = new WalletAppKit(params, new File("."), filePrefix);
// Download the block chain and wait until it's done.
kit.startAsync();
kit.awaitRunning();
List<Address> list = kit.wallet().getWatchedAddresses();
if (list.size() < 2) {
kit.wallet().addWatchedAddress(kit.wallet().freshReceiveAddress());
System.out.println("New address created");
}
System.out.println("You have " + list.size() + " addresses!");
for (Address a: list) {
System.out.println(a.toString());
}
String balance = kit.wallet().getBalance().toFriendlyString();
System.out.println(balance);
}