- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个项目,我必须创建从文本文件创建的对象数组。然而,每当我运行它时,我都会得到
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at CustomerList.main(CustomerList.java:16)
即使文本文件以整数开头。请查看我的代码并让我了解我做错了什么。
这是我必须读取的文本文件的一部分:
100
900 Amazon purchasing@amazon.com 20000.0 0.08
210 Nordstrom purchasing@nordstrom.com 50000.0 0.07
10 Rutgers purchasing@rutgers.edu 32000.0 education
520 Alamo purchasing@alamo.com 23000.0 0.05
1 Kean purchasing@kean.edu 158000.5 education
100 Allied purchasing@allied.com 85300.0 0.06
950 JoesInc purchasing@joesinc.com 999999.0 0.03
697 BostonU purchasing@tufts.edu 340020.23 education
310 TruckersInc purchasing@truckersinc.com 55000.0 0.10
820 Clothiers purchasing@clothiers.com 20044.0 0.05
849 RedCross purchasing@redcross.org 48900.2 non-profit
125 ChocolateRus purchasing@chocolate.com 3000.5 0.1
import java.util.*;
import java.math.*;
import java.text.*;
import java.io.*;
public class CustomerList {
public static void main(String[] args)throws IOException {
File f = new File("custy.txt");
Scanner in = new Scanner(f);
Customer[] obj = new Customer[1];
int count = 0;
while(in.hasNext() ){
int id = in.nextInt();
String name = in.next();
String email = in.next();
double balance = in.nextDouble();
obj[count] = new Customer (id, name, email, balance);
count++;
}
in.close();
for (int i = 0; i < count; i++)
System.out.println(obj[i]);
}
}
class Customer {
public int custId;
public String name;
public String email;
public double balance;
public Customer() {
this.custId = 0;
this.name = "";
this.email = "";
this.balance = 0.0;
}
public Customer(int custId, String name, String email, double balance) {
this.custId = custId;
this.name = name;
this.email = email;
this.balance = balance;
}
public int getId() {
return custId;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public double getBalance() {
return balance;
}
public void setName(String name) {
this.name = name;
}
public void setId(int custId) {
this.custId = custId;
}
public void setEmail(String email) {
this.email = email;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String toString() {
DecimalFormat dollar = new DecimalFormat("$.00");
return custId + " " + dollar.format(balance) + name + " " + email + " ";
}
}class TaxExempt extends Customer {
public String exempt;
public TaxExempt() {
this.exempt = "";
}
public TaxExempt(int custId, String name, String email, double balance, String exempt) {
super(custId, name, email, balance);
this.exempt = exempt;
}
public String getExempt() {
return exempt;
}
public void setExempt(String reason) {
this.exempt = reason;
}
public String toString() {
DecimalFormat dollar = new DecimalFormat("$.00");
return custId + " " + name + " " + dollar.format(balance) + " " + email + " " + exempt;
}
}
class NonExempt extends Customer {
public double tax;
public double afterTax;
public NonExempt() {
this.tax = 0.0;
this.afterTax = 0.0;
}
public NonExempt(int custId, String name, String email, double balance, double tax) {
super(custId, name, email, balance);
this.tax = tax;
}
public double getTax() {
return tax;
}
public void setTax(double tax) {
this.tax = tax;
}
public void setAfterTax() {
this.afterTax = tax * balance;
}
public String toString() {
DecimalFormat dollar = new DecimalFormat("$.00");
String add = "%";
// System.out.println("Non Exempt: " );
return custId + " " + name + " " + dollar.format(balance) + " " + email + " " + tax + add + " "
+ dollar.format(afterTax);
}
}
最佳答案
您的错误与此问题有些相关:Scanner is skipping nextLine() after using next() or nextFoo()?
注意: 假设您删除了文件中的前 100
您的应用程序正在崩溃,因为当它到达时
double balance = in.nextDouble();
并尝试再迭代一次,该行中没有任何内容
int id = in.nextInt();
要读取内容,您只需包含:
in.nextLine();
之后
double++;
你应该得到你想要的输出。
完整代码示例:
import java.util.*;
import java.math.*;
import java.io.*;
class Test {
public static void main(String[] args)throws IOException {
File f = new File("custy.txt");
Scanner in = new Scanner(f);
while(in.hasNext() ){
int id = in.nextInt();
String name = in.next();
String email = in.next();
double balance = in.nextDouble();
in.nextLine();
System.out.println(id + " " + name + " " + email + " " + balance);
}
in.close();
}
}
上面的代码打印:
900 Amazon purchasing@amazon.com 20000.0
210 Nordstrom purchasing@nordstrom.com 50000.0
10 Rutgers purchasing@rutgers.edu 32000.0
520 Alamo purchasing@alamo.com 23000.0
1 Kean purchasing@kean.edu 158000.5
100 Allied purchasing@allied.com 85300.0
950 JoesInc purchasing@joesinc.com 999999.0
697 BostonU purchasing@tufts.edu 340020.23
310 TruckersInc purchasing@truckersinc.com 55000.0
820 Clothiers purchasing@clothiers.com 20044.0
849 RedCross purchasing@redcross.org 48900.2
125 ChocolateRus purchasing@chocolate.com 3000.5
关于java - 使用数组从文本文件创建对象(无ArrayList),运行时出现InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59039701/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!