项目有问题 - java 的新手
Trouble with project - new to java
我正在为 Uni 开发一个项目,我想将文件内容加载到稍后打印的本地数组中。
当用户选择 1 查看钱包时,我希望程序将文本文件中的加密货币余额显示到控制台上。但是,我的代码没有这样做。我对使用缓冲很陌生,我对以前的作业进行了逆向工程,所以我的代码一定是错误的。
我的文本文件包含
“比特币 1.3426
以太坊 5.2519
瑞波币 158.5000
请忽略所有 // 代码并查看 viewWallet()
和 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;
}
}
您有一些错误(除了缺少我在编辑中添加的花括号)。
sCurrentLine.split("\t")
- 这里有两个错误。第一个错误是您需要转义斜杠字符。因此,正确的表达方式应该是"\t"
。但是,您的文件可能不是制表符分隔的。因此,对于 space-delimited 文本,您应该使用 "\s"
、" "
或 "\s+"
。如果您不确定有多少个空格,最后一个表达式是最好的,因为它可以使用 one-or-more 个空格。
Integer.parseInt(...)
- 这需要是 Double.parseDouble()
因为文件中的值是 floating-point 数字,而不是整数。
System.out.println(balance)
inside viewWallet()
不正确 - 该行显示对象,而不是内容,因为数组无法覆盖 toString()
。因此,您必须遍历 balance
数组并显示其内容。最简单的方法是这样的:
for(double value : balance) {
System.out.println(value);
}
这将在单独的行中显示数组中的所有值。如果数组为空,则完全跳过。
更正代码
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();
for(double value : balance) {
System.out.println(value);
}
}
// 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) {
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[] balance = new double[lineNumber];
System.arraycopy(temp, 0, balance, 0, lineNumber);
return balance;
}
}
免责声明:我创建的用于测试的文件的内容与 OP 提供的字符串完全相同(减去双引号)BTC 1.3426 ETH 5.2519 XRP 158.5000
。因此,我的解决方案假定 space-delimited 字符串在一行中。在每个数字后添加一个回车 return/line 提要,以便为不同的加密货币值设置不同的行。
我正在为 Uni 开发一个项目,我想将文件内容加载到稍后打印的本地数组中。
当用户选择 1 查看钱包时,我希望程序将文本文件中的加密货币余额显示到控制台上。但是,我的代码没有这样做。我对使用缓冲很陌生,我对以前的作业进行了逆向工程,所以我的代码一定是错误的。
我的文本文件包含 “比特币 1.3426 以太坊 5.2519 瑞波币 158.5000
请忽略所有 // 代码并查看 viewWallet()
和 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;
}
}
您有一些错误(除了缺少我在编辑中添加的花括号)。
sCurrentLine.split("\t")
- 这里有两个错误。第一个错误是您需要转义斜杠字符。因此,正确的表达方式应该是"\t"
。但是,您的文件可能不是制表符分隔的。因此,对于 space-delimited 文本,您应该使用"\s"
、" "
或"\s+"
。如果您不确定有多少个空格,最后一个表达式是最好的,因为它可以使用 one-or-more 个空格。Integer.parseInt(...)
- 这需要是Double.parseDouble()
因为文件中的值是 floating-point 数字,而不是整数。System.out.println(balance)
insideviewWallet()
不正确 - 该行显示对象,而不是内容,因为数组无法覆盖toString()
。因此,您必须遍历balance
数组并显示其内容。最简单的方法是这样的:
for(double value : balance) {
System.out.println(value);
}
这将在单独的行中显示数组中的所有值。如果数组为空,则完全跳过。
更正代码
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();
for(double value : balance) {
System.out.println(value);
}
}
// 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) {
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[] balance = new double[lineNumber];
System.arraycopy(temp, 0, balance, 0, lineNumber);
return balance;
}
}
免责声明:我创建的用于测试的文件的内容与 OP 提供的字符串完全相同(减去双引号)BTC 1.3426 ETH 5.2519 XRP 158.5000
。因此,我的解决方案假定 space-delimited 字符串在一行中。在每个数字后添加一个回车 return/line 提要,以便为不同的加密货币值设置不同的行。