gpt4 book ai didi

java - 如何将文本文件作为字符串读取并仅提取单个字符? "0"

转载 作者:行者123 更新时间:2023-12-01 11:05:35 25 4
gpt4 key购买 nike

如何请求字符串输入,然后提取 0 字符?我使用的是“fstream.next()”,它会输入任何内容,而不仅仅是一个字符串。我只需要一个字符,稍后可以在程序仅接受 T、D 和 E 的字符输入的循环中使用它。

然后程序读取 double 。然后程序使用参数(char、double)调用实例方法。实例方法稍后执行它的操作并保存输入。程序稍后会循环回来并重新执行一遍。

目前我收到错误“java.util.InputMismatchException”。如果您能提供建议,我将不胜感激!如果需要澄清,请告诉我。先感谢您! :)

这是我的代码:

    /** holds answer of whether user has more inputs or not */
String answer;
/** holds the results when calling inLetter() */
char letter;
/** holds the results when checking for types/strings in txt file */
String inType = "";
/** holds double results when searching line by line the text file */
double inAmount = 0;
/** holds the results when calling inAmount() */
double amount;
/** initiates infile to null */
File infile = null;
/** count system for how many valid lines were read and used */
int count = 0;

/** calls description method */
description();
/** initiates new Object */
GironEventClass newInput = new GironEventClass();
try{
/** defines new variable linked to .dat file */
infile = new File("GironEvent.dat");
/** calls fstream scanner - for use on file */
Scanner fstream = new Scanner(infile);

/** calls while loop. As long as there is a line, the loop continues */
while(fstream.hasNext())
{
/** inputs first string in line of file to variable inType */
inType = fstream.nextLine();
char a_char = inType.charAt(0);

/** inputs first int in line of file to variable inAmount */
inAmount = fstream.nextDouble();

try{
/** calls instance method with two parameters */
newInput.donations(a_char, inAmount);
/** count ticket increases */
count+=1;

}
catch(IllegalArgumentException a){
/** prints out error if exception is caught*/
System.out.println("Just caught an illegal argument exception. ");
}

}
/** closes stream between program and fiel */
fstream.close();

/** outputs line count, and totals per type */
System.out.println("\n" + count + " number of valid lines were read!\n");
System.out.println("Total Sales: " + newInput.getSale());
System.out.println("Donations: " + newInput.getDonated());
System.out.println("Expenses: " + newInput.getExpenses());
}

catch (FileNotFoundException e){
/** Outputs error if file cannot be opened. */
System.out.println("\nGironEvent.dat could not be opened. ");
}

do{
/** loop asks user if there is more that needs to be added to the totals. N exits loop. */
System.out.print("\nAre there any more items to add that were not in the text file? (Type 'Y' or 'N'): ");
answer = keyboard.next();
if (("Y".equals(answer)) || ("y".equals(answer)))
{
letter = inLetter();
amount = inAmount();

newInput.donations(letter, amount);
}

}while (("Y".equals(answer)) || ("y".equals(answer)));
/** calls instance method to display totals in a fancy manner */
newInput.display();
}

/** inLetter - displays types to user. Asks user to input type. Converts input into uppercase letter.
*
* @return resultTwo - Uppercase form of result. Result is the type the user inputted.
*/
public static char inLetter(){
Scanner keyboard = new Scanner(System.in);
String result;
String resultTwo;

System.out.println("T = Tiket Sales");
System.out.println("D = Donations");
System.out.println("E = Expenses");
System.out.print("Please input an identifier ");
result = keyboard.nextLine();
resultTwo = result.toUpperCase();

return resultTwo;
}

/** inAmount - asks user for amount. Tests that amount isn't a negative number or a zero.
*
* @return result - the amount the user inputed. regardless if it was the first or tenth time.
*/
public static double inAmount(){
Scanner keyboard = new Scanner(System.in);
double result;

System.out.println("Please input an amount ");
result = keyboard.nextDouble();

if(result <= 0){
System.out.print("Please input a positive and non-zero amount ");
result = keyboard.nextDouble();
}

return result;
}
/** description - displays a description of what the program does
* void.
*/
public static void description(){
System.out.println("The program will ask you what amount is being spent on what.");
System.out.println(" ex: expenses, ticket sales, and profts.");
System.out.println("This program will help determine whether the event generated or lost money.");
}

我需要有关以下 block 的帮助:

 /** inputs first string in line of file to variable inType */
inType = fstream.nextLine();
char a_char = inType.charAt(0);

/** inputs first int in line of file to variable inAmount */
inAmount = fstream.nextDouble();

最佳答案

您的 fstream 具有 String 和 Double 的组合。

因此,当您使用 fstream.nextDouble() 时,它会抛出 java.util.InputMismatchException

引用:http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextDouble()

您可以首先使用 hasNextDouble() 方法检查下一个字符是否为 double

引用: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextDouble()

关于java - 如何将文本文件作为字符串读取并仅提取单个字符? "0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com