<p>I was able to resolve the issue.</p>

<pre><code>public static String estFee(String amount, String receiver, WalletAppKit kit, NetworkParameters params){
        try {
            String value2 = String.format("%.8f",Double.parseDouble(amount) * MainPage.handleFee);
            //String value1 = String.format("%.8f",Double.parseDouble(amount) - Double.parseDouble(value2));
            String value1 = String.format("%.8f",Double.parseDouble(amount));
            Coin val1 = Coin.parseCoin(value1);
            //Coin val2 = Coin.parseCoin(value2);
            System.out.println(receiver);
            Address target1 = Address.fromBase58(params, receiver);
            //Address target2 = Address.fromBase58(params, MainPage.handleAddress);
            Transaction tx = new Transaction(params);
            tx.addOutput(val1, target1);
            //tx.addOutput(val2, target2);
            SendRequest req = SendRequest.forTx(tx);
            req.feePerKb = Coin.parseCoin("0.0001");
            kit.wallet().completeTx(req);
            String[] temp = req.tx.getFee().toFriendlyString().split(" ");
            return String.format("%.8f",Double.parseDouble(temp[0]));
        }catch(InsufficientMoneyException e){
            Double mis = (double)e.missing.getValue()/100000000;
            System.out.println(BackEnd.strFormat("ERROR","NSF","INFO","Not enough money. You are missing "+mis+". This was to retrieve an estimated fee."));
            return "0";
        }
    }
</code></pre>

<p>The library wasnt so forth coming so i had to improvise for the solution.</p>

CC BY-SA 4.0
2018-11-06T17:58:13.607Z
Thanks for coming back to answer your own question!
Murch
CC BY-SA 4.0
2018-11-06T18:53:05.980Z
Well i try to close up my questions for other people.

The library was initially difficult to read but after a few weeks ive gotten a better understanding on how things work in the back. IT also helped me understand data types.
BTCDude
CC BY-SA 4.0
2018-11-06T17:33:14.803Z
2018-11-06T17:33:14.803Z
BTCDude
<p>Im trying to get the estimation of how much the fees will be when a final amount is decided on. I would say im getting familiar with bitcoinj but not completely with java so im still learning how to code and exploring how to get information.</p>

<p>this is my present code. I put everything inside a method.</p>

<pre><code>public static String SendFund(String amount, String receiver){
        String text;
        Coin value = Coin.parseCoin(amount);
        LegacyAddress to = LegacyAddress.fromBase58(MainPage.params, receiver);
        try {
            System.out.println(gbal()+" - "+gaddress());
            Wallet.SendResult result = kit.wallet().sendCoins(kit.peerGroup(), to, value);
            text = "&lt;html&gt;"+amount+" BTC sent to address &lt;br/&gt;&lt;br/&gt;"+receiver+".&lt;/html&gt;";
            System.out.println("Check transaction : https://testnet.blockchain.info/tx/"+result.tx.getHashAsString());
        }catch(InsufficientMoneyException e){
            text = "Insufficient Funds.";
        }
        return text;
    }
</code></pre>

<p>Currently it doesnt deal with fees. But when i view a transaction such as this ==> <a href="https://testnet.blockchain.info/tx/194b2a7505a537307abe9443b048bc7ddce6ca0d833d5758c3be3c03a9c7b838" rel="nofollow noreferrer">https://testnet.blockchain.info/tx/194b2a7505a537307abe9443b048bc7ddce6ca0d833d5758c3be3c03a9c7b838</a> you can clearly see the fee.</p>

<p>Also when i purposely tried to send coins when i know the wallet doesnt have enough it doesnt tell me that it failed </p>

<pre><code>0.8625106 - n2uEm2QXqe2PCSsbHTdAJN9aDm2mDjz6iZ
May 02, 2018 5:08:58 PM org.bitcoinj.wallet.Wallet completeTx
INFO: Completing send tx with 1 outputs totalling 1.00 BTC and a fee of 0.001 BTC/kB
</code></pre>

<p>What am i doing wrong for failing to check? do i have to actually perform an if statement just to check? And also how can i calculate reliably on what the final amount would be?</p>

CC BY-SA 4.0
2018-05-05T12:14:26.157Z
*bump* Trying to bump this thread.
BTCDude
CC BY-SA 4.0
2018-10-06T19:53:09.617Z
Perhaps you should gather the details of the code libraries you are using, and try the question on stackoverflow proper, since this might be more a programming question than a bitcoin question.
qxotk
<bitcoinj><java>
Handling fees and setting them
BTCDude
# Handling fees and setting them

(Asked by: username_0 on 2018-05-03)

Im trying to get the estimation of how much the fees will be when a final amount is decided on. I would say im getting familiar with bitcoinj but not completely with java so im still learning how to code and exploring how to get information.
this is my present code. I put everything inside a method.
public static String SendFund(String amount, String receiver){
        String text;
        Coin value = Coin.parseCoin(amount);
        LegacyAddress to = LegacyAddress.fromBase58(MainPage.params, receiver);
        try {
            System.out.println(gbal()+" - "+gaddress());
            Wallet.SendResult result = kit.wallet().sendCoins(kit.peerGroup(), to, value);
            text = ""+amount+" BTC sent to address "+receiver+".";
            System.out.println("Check transaction : https://testnet.blockchain.info/tx/"+result.tx.getHashAsString());
        }catch(InsufficientMoneyException e){
            text = "Insufficient Funds.";
        }
        return text;
    }

Currently it doesnt deal with fees. But when i view a transaction such as this ==> https://testnet.blockchain.info/tx/194b2a7505a537307abe9443b048bc7ddce6ca0d833d5758c3be3c03a9c7b838 you can clearly see the fee.
Also when i purposely tried to send coins when i know the wallet doesnt have enough it doesnt tell me that it failed 
0.8625106 - n2uEm2QXqe2PCSsbHTdAJN9aDm2mDjz6iZ
May 02, 2018 5:08:58 PM org.bitcoinj.wallet.Wallet completeTx
INFO: Completing send tx with 1 outputs totalling 1.00 BTC and a fee of 0.001 BTC/kB

What am i doing wrong for failing to check? do i have to actually perform an if statement just to check? And also how can i calculate reliably on what the final amount would be?

**Tags:** bitcoinj, java

> **Comments:**
> **username_0**: *bump* Trying to bump this thread.  
> **username_1**: Perhaps you should gather the details of the code libraries you are using, and try the question on stackoverflow proper, since this might be more a programming question than a bitcoin question.  

---

## 1 answers

### Answer by username_0 on 2018-11-06. Score: 2 | Accepted answer

I was able to resolve the issue.
public static String estFee(String amount, String receiver, WalletAppKit kit, NetworkParameters params){
        try {
            String value2 = String.format("%.8f",Double.parseDouble(amount) * MainPage.handleFee);
            //String value1 = String.format("%.8f",Double.parseDouble(amount) - Double.parseDouble(value2));
            String value1 = String.format("%.8f",Double.parseDouble(amount));
            Coin val1 = Coin.parseCoin(value1);
            //Coin val2 = Coin.parseCoin(value2);
            System.out.println(receiver);
            Address target1 = Address.fromBase58(params, receiver);
            //Address target2 = Address.fromBase58(params, MainPage.handleAddress);
            Transaction tx = new Transaction(params);
            tx.addOutput(val1, target1);
            //tx.addOutput(val2, target2);
            SendRequest req = SendRequest.forTx(tx);
            req.feePerKb = Coin.parseCoin("0.0001");
            kit.wallet().completeTx(req);
            String[] temp = req.tx.getFee().toFriendlyString().split(" ");
            return String.format("%.8f",Double.parseDouble(temp[0]));
        }catch(InsufficientMoneyException e){
            Double mis = (double)e.missing.getValue()/100000000;
            System.out.println(BackEnd.strFormat("ERROR","NSF","INFO","Not enough money. You are missing "+mis+". This was to retrieve an estimated fee."));
            return "0";
        }
    }

The library wasnt so forth coming so i had to improvise for the solution.

> **Comments:**
> **username_2**: Thanks for coming back to answer your own question!  
> **username_0**: Well i try to close up my questions for other people.  The library was initially difficult to read but after a few weeks ive gotten a better understanding on how things work in the back. IT also helped me understand data types.  

---

