[英]Exception in thread “main” java.util.InputMismatchException error message
I have tried many different things trying to get this to work. 我试过很多不同的事情试图让它发挥作用。 I am sorry for just a lame question I am a total newbie.
我很抱歉只是一个蹩脚的问题我是一个新手。 When I request information from my inFile does it need to be in the order the data is in?
当我从我的inFile请求信息时,它是否需要按照数据的顺序排列?
This is the inData.txt : 这是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'
I am supposed to print dimensions of a rectangle, circle. 我应该打印一个矩形,圆形的尺寸。 Name with bank account info.
姓名与银行帐户信息。 Thanks for the help!
谢谢您的帮助!
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();
}
}
You were off to a good start! 你有一个良好的开端! I found 2 issues with your code which were causing issues.
我发现您的代码有2个问题导致了问题。
The name
variable should be assigned this way: 应该以这种方式分配
name
变量:
name = inFile.next()+" "+inFile.next();
name = inFile.next()
only gets "Randy" and messing up the age
variable. name = inFile.next()
只获取“Randy”并弄乱age
变量。
Next, This line was causing issues: 接下来,这一行造成了问题:
outFile.printf("Beginning Balance: %.2f %n" , begBal + "interest rate: %.2f" , rate);
I split it into a series of outFile.print
and outFile.printf
statements. 我将它拆分为一系列
outFile.print
和outFile.printf
语句。
outFile.print("Beginning Balance: ");
outFile.printf("%.2f %n", begBal);
outFile.print("interest rate: ");
outFile.printf("%.2f", rate);
After those 2 issues, everything seems to work. 在这两个问题之后,一切似乎都有效。 I hope this helps!
我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.