-1

I'm making a currency tracker to track the live value of bitcoin.

the class "bitcoinlive" runs Correctly if I run it in its own main method, but it won't work when I make an instance of the file. I need it to print the live value you of bitcoin.

I try to print out the variable "a53" but I don't know if I'm doing it right. Here are the list of imports for the bitcoinlive class because it kept giving me an error message and wouldn't allow this to be apart of the code when posting this.

public static void main(String[] args) {
    Dates d = new Dates();
    String s = d.getDate();
    System.out.println("Date is" + s);
    W3 mywallet = new W3();
    Bitcoinlive mybitcoinlive = new Bitcoinlive();
    L3 myledger = new L3();
    Scanner myscanner = new Scanner(System.in);
    double buy = 0.0;
    int choice = 0;
    double bitcoin = 4000;
    double USD = 20000;

    while (choice != 5) {
        System.out.println("Welcome! Enter a command. \n"
                + "Enter 1) Buy Bitcoin \n"
                + "Enter 2) Sell Bitcoin  \n"
                + "Enter 3) Print Balance \n"
                + "Enter 4) Print History \n"
                + "ENTER 5) Exit Program\n");
        choice = myscanner.nextInt();

        if (choice == 1) {
            System.out.println("How many? ");
            buy = myscanner.nextDouble();
            mywallet.add(buy);
            bitcoin = bitcoin * buy;
            USD = USD - bitcoin;

            myledger.save(s);

            System.out.println("you have bought:" + mywallet.numcoins);
            System.out.println(USD);
            System.out.println(mybitcoinlive.a53);
            bitcoin = 4000;

        } else if (choice == 2 && USD >= bitcoin) {
            System.out.println("How many?");

            buy = myscanner.nextDouble();
            mywallet.subtract(buy);
            System.out.println("you have sold:" + mywallet.numcoins);
            USD = USD + bitcoin;
            System.out.println(USD);
            bitcoin = 4000;
            myledger.save(s);

        } else if (choice == 3) {
            System.out.println("Balance:" + mywallet.numcoins);

        } else if (choice == 4) {
            System.out.println("Transaction history:  ");

            System.out.println("you have made" + myledger.getsize() + "transactions"
                    + d.getDate());

        } else if (choice == 5) {
            // exit
            break;

        } else if (choice == 7) {
            System.out.println(mybitcoinlive.price);
        }

    }

    System.out.println("Bye");

}

this is my separate class

public class Bitcoinlive {



    Double a53=0.0;
    double price;     

    Double get() {

        try {
            String urlcoincapeth13 = "https://api.coinmarketcap.com/v1/ticker/bitcoin/";
            Document docblocktradescoincapeth13 = Jsoup.parse(new URL(urlcoincapeth13).openStream(), "UTF-8", "", Parser.xmlParser());
            String a13 = docblocktradescoincapeth13.toString();
            int a23 = a13.indexOf("price_usd") + 13;
            int a33 = a13.indexOf("price_btc") - 4;
            String a43 = a13.substring(a23, a33);
            a53 = Double.parseDouble(a43);
        } catch (Exception e) {
            System.out.println("Error accessing bitcoin values");
        }

        return a53;
    }
}
Michael Hampton
  • 9,737
  • 4
  • 55
  • 96
Devin
  • 23
  • 6
  • import java.io.*; import java.io.IOException; import java.net.URL; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.parser.Parser; import org.jsoup.select.Elements; import java.util.Scanner; For some reason the website wouldn't let me post the imports for the livebitcoin class. so here they are above – Devin Apr 15 '19 at 00:55
  • Show us the exception message and stacktrace for the exception you are catching. – Stephen C Apr 15 '19 at 02:38
  • I'm not getting any errors or exceptions. (I'm a beginner so I'm sorry if I don't fully understand what you mean.) If you run the bitcoin live class along with the imports I left in the comment, the output is correct. I'm just really having trouble linking the live result to my main method. I assumed that a53 is the variable that would contain the live result, but as you can see in option 7 of my program it simply prints "0.0" while if I run the bitcoinlive class in its own main class it works and prints 5000 something. – Devin Apr 15 '19 at 03:06

1 Answers1

0

Your class Bitcoinlive stores the price in a field called a53. You can update this field by calling get(). However, it looks like you're never calling get() - you're just calling the field:

System.out.println(mybitcoinlive.a53);

Try replacing that line with:

System.out.println(mybitcoinlive.get());

Or refresh it first:

mybitcoinlive.get();
System.out.println(mybitcoinlive.a53);
Malcolm Crum
  • 4,345
  • 4
  • 30
  • 49