我的代码旨在读取文本文件的内容,并检查内容是否以如下格式输入:
john : martin : 2 : 1
如果遵循该格式,则它将以以下格式输出:
john [2] | martin [1]
否则它将被视为无效结果,并且总数不会被添加到其中,而如果结果是格式,那么它们将被添加到总数中,因此使用示例它将显示vaild的数量结果为1,无效为0,总数为3.
我的问题是我的代码无法正常工作,因为我收到此错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at reader.main(reader.java:33)
那么我将如何解决此问题并以我想要的方式阅读和显示数据?提前谢谢。
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;
import java.util.Scanner;
public class reader {
/**
* @param args
* @throws FileNotFoundException
* @throws FileNotFoundException
* @throws FileNotFoundException when the file cannot be loaded
*/
public static void main(String[] args) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
Scanner s = new Scanner(new BufferedReader(new FileReader("results2.txt"))).useDelimiter(":");
// create a scanner which scans from a file and splits at each colon
while ( s.hasNext() ) {
hteam = s.next(); // read the home team from the file
ateam = s.next(); // read the away team from the file
hscore = s.nextInt(); //read the home team score from the file
ascore = s.nextInt(); //read the away team score from the file
System.out.print(hteam); // output the line of text to the console
System.out.print(hscore);
System.out.print(ateam);
System.out.println(ascore);
}
System.out.println("\nEOF"); // Output and End Of File message.
}
}
答案 0 :(得分:0)
您正在寻找s.next()
而不是s.nextLine()
。
hteam = s.nextLine()
读取整行“john:martin:2:1”,只留下ateam
的任何内容。
正如你所说,这仍然无法正常工作,我猜你输入文件末尾有一个额外的换行符,导致s.hasNext()
评估为true。这将导致扫描仪在获取下一个输入行时跳闸。
尝试Scanner s = new Scanner(System.in).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");
阅读多行。
请参阅实施:http://ideone.com/yfiR2S
为了验证一行的格式是否正确,我(从osoblanco的答案中获得灵感)检查有4个单词,后两个是整数:
public static boolean verifyFormat(String[] words) {
// see endnote for isInteger()
return words.length == 4 && /*isInteger(words[2]) && isInteger(words[3])*/;
}
public static void main(String[] args) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
Scanner s = new Scanner(new BufferedReader(
new FileReader("results2.txt"))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");
while (s.hasNext()) {
String line = s.nextLine();
String[] words = line.split("\\s*:\\s*");
if(verifyFormat(words)) {
hteam = words[0]; // read the home team
ateam = words[1]; // read the away team
hscore = Integer.parseInt(words[2]); //read the home team score
ascore = Integer.parseInt(words[3]); //read the away team score
System.out.print(hteam); // output the line of text to the console
System.out.print(hscore);
System.out.print(ateam);
System.out.println(ascore);
}
}
System.out.println("EOF");
}
isInteger()
可以找到here。
答案 1 :(得分:0)
我认为扫描并不是你想要的。我只想使用BufferedReader并使用ReadLine每次通过for循环来处理1行。
然后通过以下方式验证每一行:
1)String.split(":")并验证4件。
String [] linePieces = nextLine.split(":");
if(linePieces.length!=4)
{
//mark invalid, continue loop
}
2)修剪每件
for(int i =0; i<4; i++)
linePieces[i] = linePieces[i].trim();
3)验证第3部分和第4部分是数字,使用try / catch验证Integer.parseInt。在catch块中,计算该行无效。
try
{
name1=linePieces[0];
name2=linePieces[1];
score1=Integer.parseInt(linePieces[2]);
score2=Integer.parseInt(linePieces[3]);
//count as success and do logic
}catch(NumberFormatException e){
//invalid line
}