I am working on a project, and I am unable to proceed with it because of an error.
I am attempting to overwrite a text file, using a method to scan through two arrays that hold specific crypto names. For example, in the section of my code removeCrypto(), which is option 4, I would type in BTC and it would remove BTC and the corresponding balance from the text file.
How can I make overWriteFile(line) work with both double and String?
shows what is located in my .txt file
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;
case 3:
newEntry();
break;
case 4:
removeCrypto();
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 4
public static void removeCrypto() {
String[] cryptoName = readCryptoNames();
double[] cryptoBalance = readCryptoBalance();
String[] line =new String[cryptoName.length];
System.out.println("What is the name of the crypto you would like to remove? ");
String cryptoNameTBF = userInputString.next();
// System.out.println("Please enter the balance");
// double newBalance = Double.parseDouble(userInputDouble.next());
for (int i=0; i<cryptoName.length;i++) {
if (cryptoName[i].equals(cryptoNameTBF)){
// cryptoBalance[i] = newBalance;
}
}
for (int i=0;i<cryptoName.length;i++){
line[i] = cryptoName[i] + "\t" + cryptoBalance[i];
}
overWriteFile(line); //CONTINUE HERE - HERE IS MY ISSUE
}
// 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;
}
// This method adds a new entry to the file and at the end of the file
public static void appendFile(String cryptoName) {
try {
PrintWriter wr = new PrintWriter(new BufferedWriter(new FileWriter("C:\\Users\\John\\IdeaProjects\\A3_591158\\src\\com\\company\\wallet.txt", true)));
wr.println(cryptoName);
wr.close();
} catch (IOException e) {
System.out.println("There is an I/O error when writing.");
}
}
// This method takes all crypto names and balances and writes on the file from scratch
public static void overWriteFile(String[] cryptoName, double[] cryptoBalance) {
try {
PrintWriter wr = new PrintWriter(
new BufferedWriter(new FileWriter("C:\\Users\\John\\IdeaProjects\\A3_591158\\src\\com\\company\\wallet.txt", false)));
for (int i = 0; i < cryptoName.length; i++) wr.println(cryptoName[i]);
wr.close();
} catch (IOException e) {
System.out.println("There is an I/O error when writing.");
}
try {
PrintWriter wr = new PrintWriter(
new BufferedWriter(new FileWriter("C:\\Users\\John\\IdeaProjects\\A3_591158\\src\\com\\company\\wallet.txt", false)));
for (int i = 0; i < cryptoBalance.length; i++) wr.println(cryptoBalance[i]);
wr.close();
} catch (IOException e) {
System.out.println("There is an I/O error when writing.");
}
}