0

I am working on a project for Uni that involves a program reading a .txt file with information regarding cryptos.

I am currently stuck on the lookupBalance() section. My goal is to have a user input which crypto they would like to see the balance of, and use that input to provide them a balance of that crypto. I have tried tons of different things and cant seem to get it right. For example, if the user inputs "BTC", I want the program to provide "1.3426".

My .txt file contains what is in the attached .png enter image description here

public class Main {
static Scanner userInputInt = new Scanner(System.in);
static Scanner userInputString = new Scanner(System.in);
static Scanner userInputDouble = new Scanner(System.in);

public static void main(String[] args) {
    int userChoice = getUserChoice();
    switch (userChoice) {
        case 1:
            viewWallet();
            break;
         case 2:
            lookupBalance();
            break;
    }
}

// This method asks and returns what the user wants to do
public static int getUserChoice() {
    System.out.println("****************************************************************");
    System.out.println("****************************************************************");
    System.out.println("************ Crypto Wallet **********");
    System.out.println("****************************************************************");
    System.out.println("****************************************************************");
    System.out.println("");
    System.out.println("What do you want to do?");
    System.out.println("(1) View wallet");
    System.out.println("(2) Look up the balance of a given crypto");
    System.out.println("(3) Add new cryptos");
    System.out.println("(4) Remove an existing crypto");
    System.out.println("(5) Update wallet");
    System.out.println("****************************************************************");
    System.out.print("Please enter your choice (1, 2, 3, 4, or 5): ");
    return userInputInt.nextInt();
}

// This method is called for user choice 1
public static void viewWallet() {
    String[] cryptoName = readCryptoNames();
    double[] cryptoBalance = readCryptoBalance();
    System.out.println("Crypto in portfolio " + Arrays.toString(cryptoName) + " Balance of crypto " +  Arrays.toString(readCryptoBalance()));
    }

// This method is called for user choice 2
public static void lookupBalance() {
    String[] cryptoName = readCryptoNames();
    double[] cryptoBalance = readCryptoBalance();
    Arrays.toString(cryptoName);
    System.out.println(Arrays.toString(cryptoName));
    String[] line = new String[cryptoName.length];
    int BTC = (int) 1.3426;
    int ETH = (int) 5.2519;
    int XRP = (int) 158.5000;

   System.out.println("Which crypto would you like to view the balance of? ");
    String cryptoNameTBF = userInputString.next();

    if (cryptoNameTBF == BTC)
        System.out.println("1.3426");//example of what I want as an output
    }


// This method reads and returns the crypto names from the file
 public static String[] readCryptoNames() {

   String[] temp = new String[100];
   int lineNumber = 0;
   try {

       BufferedReader myFile = new BufferedReader(new FileReader("C:\\Users\\John\\IdeaProjects\\A3_591158\\src\\com\\company\\wallet.txt"));
       String sCurrentLine;
       while ((sCurrentLine = myFile.readLine()) != null) {
           String[] tokens = sCurrentLine.split("\\s+");
           temp[lineNumber] = (tokens[0]);
           lineNumber++;
       }
       myFile.close();
   } catch (IOException e) {
       System.out.println("Error reading crypto name: " + e.getMessage());
   }
   String[] cryptoName = new String[lineNumber];
   System.arraycopy(temp, 0, cryptoName, 0, lineNumber);
   System.out.println(cryptoName);
   return cryptoName;
}

// This method reads and returns the crypto balances from the file
public static double[] readCryptoBalance() {
    double[] temp = new double[100];
    int lineNumber = 0;
    try {

        BufferedReader myFile = new BufferedReader(new FileReader("C:\\Users\\John\\IdeaProjects\\A3_591158\\src\\com\\company\\wallet.txt"));
        String sCurrentLine;
        while ((sCurrentLine = myFile.readLine()) != null) {
            String[] tokens = sCurrentLine.split("\\s+");
            temp[lineNumber] = Double.parseDouble(tokens[1]);
            lineNumber++;
        }
        myFile.close();
    } catch (IOException e) {
        System.out.println("Error reading crypto balance: " + e.getMessage());
    }
    double[] cryptoBalance = new double[lineNumber];
    System.arraycopy(temp, 0, cryptoBalance, 0, lineNumber);
    return cryptoBalance;
}
Ddddd12312
  • 15
  • 4
  • what's the question – rhowell Feb 22 '22 at 21:30
  • How do I use what the user inputs, for example, user inputs BTC, to create a statement that returns the Balance of BTC. E.g., cryptoNameTBF == BTC leads to System.out.println("1.3426"); – Ddddd12312 Feb 22 '22 at 21:33
  • Do these two lines work as intended? `String[] cryptoName = readCryptoNames();` `double[] cryptoBalance = readCryptoBalance();` ? If so then you can just find the index of the crypto in the first array and get that same index in the second array. Really you shouldn't need to read the file like that into two different arrays with multiple buffered readers.. You can use `Files.lines` to easily read each line of your text, split on space and store key/value into a Map. – user1738539 Feb 22 '22 at 21:35
  • @user1738539 that is exactly what I want to do! I am just having trouble implementing it – Ddddd12312 Feb 22 '22 at 21:37
  • it's a very simple for loop: for (int i = 0; i < cryptoName.length; i++) { if (cryptoName[i].equals(cryptoNameTBF)) { System.out.println("Your balance of " + cryptoName[i] + " is " + cryptoBalance[i]); return; } } – user1738539 Feb 22 '22 at 21:47
  • That is extremely simple and would be exactly what I need, however, when I use a "for" statement, I get a ton of errors saying "Cannot resolve method 'readCryptoNames' in 'Main". I honestly do not know how to resolve this issue (I am new to Java). – Ddddd12312 Feb 22 '22 at 21:56
  • Actually "for" statements are fine, the issue is with "if (cryptoName[i].equals(cryptoNameTBF)" – Ddddd12312 Feb 22 '22 at 22:03

0 Answers0