gpt4 book ai didi

java - 线程中出现异常 "main"java.util.InputMismatchException 错误消息

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:51 25 4
gpt4 key购买 nike

我尝试了很多不同的方法来让它发挥作用。我很抱歉问了一个蹩脚的问题,我完全是新手。当我从 inFile 请求信息时,它是否需要按照数据的顺序排列?

这是 inData.txt :

10.20  \\\length
5.35 \\\width
15.6 \\\radius
Randy Gill \\\first last name
31 \\\age
18500 \\\bank account balance
3.5 \\\ interest rate
A \\char 'a'

我应该打印矩形、圆形的尺寸。姓名及银行账户信息。感谢您的帮助!

import java.util.*;
import java.io.*;


public class ProgrammingExercise3_1
{
public static void main(String[] args) throws FileNotFoundException
{

double rectWidth;
double rectLength;
double radius;
int age;
double begBal;
char A;
String name;
double rate;

Scanner inFile = new Scanner(new FileReader("C:\\Users\\sierr_000\\Desktop\\Sam School\\IT-145\\Exercises\\Ch 3\\inData.txt"));

PrintWriter outFile = new PrintWriter("C:\\Users\\sierr_000\\Desktop\\Sam School\\IT-145\\Exercises\\Ch 3\\outData.out");

rectWidth = inFile.nextDouble();
rectLength = inFile.nextDouble();

outFile.println("Rectangle: ");
outFile.println("Length = " + rectLength + ", width = " + rectWidth + ", area = "
+ (rectWidth*rectLength) + ", perimeter = " + (2 * (rectWidth + rectLength)));

radius = inFile.nextDouble();

outFile.println("Circle: ");
outFile.println("Radius = " + radius + ", area = " + (radius*3.1416) + "Circumfrence = " + (2*3.1416*radius));

name = inFile.next();
age = inFile.nextInt();

outFile.println("Name: " + name + ", age: " + age);

begBal = inFile.nextDouble();
rate = inFile.nextDouble();

outFile.printf("Beginning Balance: %.2f %n" , begBal + "interest rate: %.2f" , rate);
outFile.println("The character that comes after A in the ASCII is B");




inFile.close();
outFile.close();

}
}

最佳答案

你有一个良好的开端!我发现您的代码有 2 个问题导致了问题。

name 变量应该这样分配:

name = inFile.next()+" "+inFile.next();

name = inFile.next() 只会获取“Randy”并弄乱 age 变量。

接下来,这一行引起了问题:

outFile.printf("Beginning Balance: %.2f %n" , begBal + "interest rate: %.2f" , rate);

我将其分成一系列 outFile.printoutFile.printf 语句。

  outFile.print("Beginning Balance: ");
outFile.printf("%.2f %n", begBal);
outFile.print("interest rate: ");
outFile.printf("%.2f", rate);

在这两个问题之后,一切似乎都正常了。我希望这有帮助!

关于java - 线程中出现异常 "main"java.util.InputMismatchException 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36438730/

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