I am working on a project for Uni, and I want to load file contents into local arrays which later print.
When the user chooses 1 to view the wallet, I want the program to display the crypto balance from the text file onto the console. However, my code is not doing that. I am very new to using buffered and I reverse engineered a previous assignment, so my code must be wrong.
My text file contains "BTC 1.3426 ETH 5.2519 XRP 158.5000"
Please ignore all the // code and see the sections for viewWallet() and readCryptoBalance().
import java.io.*;
import java.util.*;
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) { //throws Exception
int userChoice = getUserChoice();
switch (userChoice) {
case 1:
viewWallet();
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() {
double[] balance = readCryptoBalance();
System.out.println(balance);
}
// 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("wallet.txt"));
String sCurrentLine;
while ((sCurrentLine = myFile.readLine()) != null){
temp[lineNumber] =Integer.parseInt(sCurrentLine.split("\t")[1]);
lineNumber++;
}
myFile.close();
} catch (IOException e){
System.out.println("I/O exception error when reading");
}
double[] balance = new double[lineNumber];
System.arraycopy(temp, 0, balance, 0, lineNumber);
return balance;
}
}