Java 使用scanner类get InputMismatchException读取txt文件。我做错了什么?
我是Java新手,下面是Murach Java书中的示例。我目前正在学习如何处理记录。我遇到了一个我似乎无法解决的问题。逻辑编译时没有任何问题,但是当我尝试运行程序时,在“ReadRecords”类的第23行出现“INputMismatchException”错误。我在谷歌上搜索了这个问题,并搜索了关于这个问题的软件,我确实找到了一些解决方案,但它们并没有帮助。如果有人能帮我解决这个问题,我将不胜感激。我对这些课程有兴趣:Java 使用scanner类get InputMismatchException读取txt文件。我做错了什么?,java,Java,我是Java新手,下面是Murach Java书中的示例。我目前正在学习如何处理记录。我遇到了一个我似乎无法解决的问题。逻辑编译时没有任何问题,但是当我尝试运行程序时,在“ReadRecords”类的第23行出现“INputMismatchException”错误。我在谷歌上搜索了这个问题,并搜索了关于这个问题的软件,我确实找到了一些解决方案,但它们并没有帮助。如果有人能帮我解决这个问题,我将不胜感激。我对这些课程有兴趣: 记录类 ReadRecords类 档案类别代码: class Recor
class Records{
public int id; //ID
public String name; //NAME
public String gender; //GENDER
}//CLASS
import java.util.Scanner;
import java.io.*;
public class ReadRecords {
//METHOD: MAIN
public static void main(String[] args){
Scanner input;
final int MAX_LEN = 25;
int counter = 0;
String filePath = "files/records.txt";
Records[] rec = new Records[ MAX_LEN ];
File file;
try {
file = new File( filePath );
input = new Scanner( file );
do{
rec[counter].id = input.nextInt(); //INT
rec[counter].name = input.next(); //STRING
rec[counter].gender = input.next(); //STRING
++counter;
}
while(rec[counter -1].id != 0);
}
catch ( IOException ex ){
System.out.println( "File access error" );
counter = 0;
}//
System.out.println(rec[0].name);
}//
}//
ReadRecords类的代码:
class Records{
public int id; //ID
public String name; //NAME
public String gender; //GENDER
}//CLASS
import java.util.Scanner;
import java.io.*;
public class ReadRecords {
//METHOD: MAIN
public static void main(String[] args){
Scanner input;
final int MAX_LEN = 25;
int counter = 0;
String filePath = "files/records.txt";
Records[] rec = new Records[ MAX_LEN ];
File file;
try {
file = new File( filePath );
input = new Scanner( file );
do{
rec[counter].id = input.nextInt(); //INT
rec[counter].name = input.next(); //STRING
rec[counter].gender = input.next(); //STRING
++counter;
}
while(rec[counter -1].id != 0);
}
catch ( IOException ex ){
System.out.println( "File access error" );
counter = 0;
}//
System.out.println(rec[0].name);
}//
}//
文本文件“records.txt”
多谢各位
您正在搜索的输入是
int
,但您首先有字符串
,因此您会得到inputMismatchException,因为扫描仪的默认删除项是空格
在文本文件中,您有“,”作为分隔符
input = new Scanner( file ).useDelimiter(", ");
其次,rec[counter].id
抛出NullPointerException
,因为数组中最初会有空值。因此,如下图所示进行更改
do{
Records record=new Records();
record.id = input.nextInt(); //INT
record.name = input.next(); //STRING
record.gender = input.next(); //STRING
rec[counter]=record;
++counter;
}
while(input.nextLine() != null);
您正在搜索的输入是
int
,但您首先有string
,因此您得到的是inputMismatchException,因为扫描仪的默认删除项是空格
在文本文件中,您有“,”作为分隔符
input = new Scanner( file ).useDelimiter(", ");
其次,rec[counter].id
抛出NullPointerException
,因为数组中最初会有空值。因此,如下图所示进行更改
do{
Records record=new Records();
record.id = input.nextInt(); //INT
record.name = input.next(); //STRING
record.gender = input.next(); //STRING
rec[counter]=record;
++counter;
}
while(input.nextLine() != null);
您正在搜索的输入是
int
,但您首先有string
,因此您得到的是inputMismatchException,因为扫描仪的默认删除项是空格
在文本文件中,您有“,”作为分隔符
input = new Scanner( file ).useDelimiter(", ");
其次,rec[counter].id
抛出NullPointerException
,因为数组中最初会有空值。因此,如下图所示进行更改
do{
Records record=new Records();
record.id = input.nextInt(); //INT
record.name = input.next(); //STRING
record.gender = input.next(); //STRING
rec[counter]=record;
++counter;
}
while(input.nextLine() != null);
您正在搜索的输入是
int
,但您首先有string
,因此您得到的是inputMismatchException,因为扫描仪的默认删除项是空格
在文本文件中,您有“,”作为分隔符
input = new Scanner( file ).useDelimiter(", ");
其次,rec[counter].id
抛出NullPointerException
,因为数组中最初会有空值。因此,如下图所示进行更改
do{
Records record=new Records();
record.id = input.nextInt(); //INT
record.name = input.next(); //STRING
record.gender = input.next(); //STRING
rec[counter]=record;
++counter;
}
while(input.nextLine() != null);
扫描器的默认删除符是空格,但在文本文件中,您有按空格和新行字符分隔的数据。所以您需要将分隔符用作“,”以及“\n\r” 您也可以将您的wile条件更改为 while(input.hasNext()&&(input.next()!=null)) 这是为我运行的修改后的代码。为了简单起见,我删除了txt文件中的所有逗号,并使用以下模式作为delimter-[\r\n]。它成功地运行了
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
class Records
{
public int id; // ID
public String name; // NAME
public String gender; // GENDER
}// CLASS
public class ReadRecords
{
// METHOD: MAIN
public static void main(final String[] args)
{
Scanner input = null;
final int MAX_LEN = 25;
int counter = 0;
final String filePath = "records.txt";
final Records[] rec = new Records[MAX_LEN];
File file;
try
{
file = new File(filePath);
input = new Scanner(file);
final Pattern pat = Pattern.compile("[ \r\n]");
input.useDelimiter(pat);
do
{
rec[counter] = new Records();
rec[counter].id = input.nextInt(); // INT
rec[counter].name = input.next(); // STRING
rec[counter].gender = input.next(); // STRING
++counter;
}
while (input.hasNext() && (input.next() != null));
}
catch (final IOException ex)
{
System.out.println("File access error");
counter = 0;
}//
System.out.println(rec[0].name);
}//
}//
扫描器的默认删除符是空格,但在文本文件中,您有按空格和新行字符分隔的数据。所以您需要将分隔符用作“,”以及“\n\r” 您也可以将您的wile条件更改为 while(input.hasNext()&&(input.next()!=null)) 这是为我运行的修改后的代码。为了简单起见,我删除了txt文件中的所有逗号,并使用以下模式作为delimter-[\r\n]。它成功地运行了
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
class Records
{
public int id; // ID
public String name; // NAME
public String gender; // GENDER
}// CLASS
public class ReadRecords
{
// METHOD: MAIN
public static void main(final String[] args)
{
Scanner input = null;
final int MAX_LEN = 25;
int counter = 0;
final String filePath = "records.txt";
final Records[] rec = new Records[MAX_LEN];
File file;
try
{
file = new File(filePath);
input = new Scanner(file);
final Pattern pat = Pattern.compile("[ \r\n]");
input.useDelimiter(pat);
do
{
rec[counter] = new Records();
rec[counter].id = input.nextInt(); // INT
rec[counter].name = input.next(); // STRING
rec[counter].gender = input.next(); // STRING
++counter;
}
while (input.hasNext() && (input.next() != null));
}
catch (final IOException ex)
{
System.out.println("File access error");
counter = 0;
}//
System.out.println(rec[0].name);
}//
}//
扫描器的默认删除符是空格,但在文本文件中,您有按空格和新行字符分隔的数据。所以您需要将分隔符用作“,”以及“\n\r” 您也可以将您的wile条件更改为 while(input.hasNext()&&(input.next()!=null)) 这是为我运行的修改后的代码。为了简单起见,我删除了txt文件中的所有逗号,并使用以下模式作为delimter-[\r\n]。它成功地运行了
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
class Records
{
public int id; // ID
public String name; // NAME
public String gender; // GENDER
}// CLASS
public class ReadRecords
{
// METHOD: MAIN
public static void main(final String[] args)
{
Scanner input = null;
final int MAX_LEN = 25;
int counter = 0;
final String filePath = "records.txt";
final Records[] rec = new Records[MAX_LEN];
File file;
try
{
file = new File(filePath);
input = new Scanner(file);
final Pattern pat = Pattern.compile("[ \r\n]");
input.useDelimiter(pat);
do
{
rec[counter] = new Records();
rec[counter].id = input.nextInt(); // INT
rec[counter].name = input.next(); // STRING
rec[counter].gender = input.next(); // STRING
++counter;
}
while (input.hasNext() && (input.next() != null));
}
catch (final IOException ex)
{
System.out.println("File access error");
counter = 0;
}//
System.out.println(rec[0].name);
}//
}//
扫描器的默认删除符是空格,但在文本文件中,您有按空格和新行字符分隔的数据。所以您需要将分隔符用作“,”以及“\n\r” 您也可以将您的wile条件更改为 while(input.hasNext()&&(input.next()!=null)) 这是为我运行的修改后的代码。为了简单起见,我删除了txt文件中的所有逗号,并使用以下模式作为delimter-[\r\n]。它成功地运行了
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
class Records
{
public int id; // ID
public String name; // NAME
public String gender; // GENDER
}// CLASS
public class ReadRecords
{
// METHOD: MAIN
public static void main(final String[] args)
{
Scanner input = null;
final int MAX_LEN = 25;
int counter = 0;
final String filePath = "records.txt";
final Records[] rec = new Records[MAX_LEN];
File file;
try
{
file = new File(filePath);
input = new Scanner(file);
final Pattern pat = Pattern.compile("[ \r\n]");
input.useDelimiter(pat);
do
{
rec[counter] = new Records();
rec[counter].id = input.nextInt(); // INT
rec[counter].name = input.next(); // STRING
rec[counter].gender = input.next(); // STRING
++counter;
}
while (input.hasNext() && (input.next() != null));
}
catch (final IOException ex)
{
System.out.println("File access error");
counter = 0;
}//
System.out.println(rec[0].name);
}//
}//
老实说,我会使用另一种访问文本文件的方法。GoogleFileReader和BufferedReader都是我在介绍类中被告知要使用的文件访问器。我将在下面发布我提出的代码,以获取从文本文件创建的记录对象。这绝不是最好的方法,但它确实完成了任务。请随意询问有关我使用的代码的问题,这些问题可能不熟悉
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
class Records {
public int id; //ID
public String name; //NAME
public String gender; //GENDER
}
public class ReadRecords {
public static void main(String[] args) {
String filePath = "records.txt";
ArrayList<Records> rec = new ArrayList<Records>();
try {
FileReader fr = new FileReader(filePath);
BufferedReader br = new BufferedReader(fr);
String nextLine;
String splitText[];
while((nextLine = br.readLine())!= null){
Records temp = new Records();
splitText = nextLine.split(", ");
temp.id = Integer.parseInt(splitText[0]);
temp.name = splitText[1];
temp.gender = splitText[2];
rec.add(temp);
}
br.close();
}
catch ( IOException ex ){
System.out.println( "File access error" );
}//
for (int i = 0; i < rec.size(); i++) {
System.out.println("Record ID: " + rec.get(i).id
+ "\n" + "Record Name: " + rec.get(i).name + "\n" + "Record Gender: " + rec.get(i).gender);
}
}//
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
课堂记录{
public int id;//id
公共字符串名称;//名称
公共字符串性别;//性别
}
公开课阅读记录{
公共静态void main(字符串[]args){
字符串filePath=“records.txt”;
ArrayList rec=新的ArrayList();
试一试{
FileReader fr=新的FileReader(filePath);
BufferedReader br=新的BufferedReader(fr);
字符串下一行;
字符串拆分文本[];
而((nextLine=br.readLine())!=null){
记录温度=新记录();
splitText=nextLine.split(“,”);
temp.id=Integer.parseInt(splitText[0]);
temp.name=splitText[1];
临时性别=拆分文本[2];
记录添加(温度);
}
br.close();
}
捕获(IOEX){
系统