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;
}