提问人:reyna 提问时间:11/4/2020 更新时间:11/4/2020 访问量:40
我的扫描仪在运行几次后停止工作,并给了我java.util.InputMismatchException: 对于输入字符串:“7642874781”
my scanner stopped working after a couple of runs and gives me the java.util.InputMismatchException: For input string: "7642874781"
问:
在名字之后,查理被打印出来,它抛出输入不匹配异常,并说我正在向 int 扫描仪输入一个字符串,但它确实是一个数字,所以为什么它告诉我这是一个字符串?
这是我的文本文件,扫描仪正在从中读取: Alice,1234567891,优先级 Brandon,1987654321,高级 查理,7642874781,常规 Danny,5274847643,高级
package file reading;
public class Customer {
private String name;
private int phoneNumber;
private int seatNumber;
String classification;
public Customer(String name, int phoneNumber, String classification)
{
this.name = name;
this.phoneNumber = phoneNumber;
this.classification = classification;
}
public String getName() {return name;}
public int getPhoneNumber() {return phoneNumber;}
public int getSeatNumber() {return seatNumber;}
public String getClassification() {return classification;}
public void setName(String name) {this.name = name;}
public void setPhoneNumber(int phoneNumber) {this.phoneNumber = phoneNumber;}
public void setSeatNumber(int seatNumber) {this.seatNumber = seatNumber;}
public void setClassification(String classification) {this.classification = classification;}
public String toString()
{
return "Name: " + name + "\nPhone Number: " + phoneNumber +
" \nclassification: " + classification +"\n";
}
}
package file reading;
import java.util.Scanner;
import java.io.*;
public class URLDissector
{
//-----------------------------------------------------------------
// Reads customer info from a file and prints their path components.
//-----------------------------------------------------------------
public static void main(String[] args) throws IOException
{
String customerInfo;
Scanner fileScan, infoScan;
String name = "", classification = "";
int phoneNumber = 0 ,seatNumber = 0;
try {
fileScan = new Scanner(new File("info.txt"));
while (fileScan.hasNext())
{
customerInfo = fileScan.nextLine();
System.out.println("Customer: " + customerInfo);
infoScan = new Scanner(customerInfo);
infoScan.useDelimiter(",");
// Print each part of the customer info
while (infoScan.hasNext()) {
name = infoScan.next();
//The error is hapening right here, right after printing out the name Charlie it won't
//print the phone number and stops everything.
phoneNumber = infoScan.nextInt();
classification = infoScan.next();
Customer customer = new Customer(name, phoneNumber, classification);
System.out.println("Customer " + customer);
}
System.out.println();
}
}catch (FileNotFoundException f){
System.out.println ("File not found");
}
}
感谢您阅读我的问题!
答:
2赞
Kinin Roza
11/4/2020
#1
在 Java 中,数据类型的最大值为 ,但您的输入字符串是 “7642874781” ,它将转换为 。这超出了整数数据类型的最大值,这会导致错误。integer
32-bit
2,147,483,647
7,642,874,781
请考虑将 datatype 与 scanner 对象的方法一起使用。long
nextLong()
评论
int