Java 读取文本文件并跳过不使用分隔符的行
所以我需要做的就是跳过文本文件中不使用delimeter的行,即“\t” 其中一些数据写为: 对象\t价格 而有些则写为 对象价格(它们之间只有一两个空格)Java 读取文本文件并跳过不使用分隔符的行,java,Java,所以我需要做的就是跳过文本文件中不使用delimeter的行,即“\t” 其中一些数据写为: 对象\t价格 而有些则写为 对象价格(它们之间只有一两个空格) 如果(!line.contains(“\t”){//这里的任何内容都不包含“\t”}谢谢,我相信这是有效的。 private static final String delim = "\t"; public void readFile(){ int total=0; try{ Scanner fileSc
如果(!line.contains(“\t”){//这里的任何内容都不包含“\t”}
谢谢,我相信这是有效的。
private static final String delim = "\t";
public void readFile(){
int total=0;
try{
Scanner fileScanner = new Scanner(new File("prizeList.txt"));
while(fileScanner.hasNextLine()){
String nextLine = fileScanner.nextLine();
String[] splitStrings = nextLine.split(delim);
if(splitStrings.length != 2)
continue;
String name = splitStrings[0];
double prize = Double.parseDouble(splitStrings[1]);
prizes[total++] = new Prize(name,prize);
if (total == 50) {
break;
}
}
fileScanner.close();
}
catch(Exception e)
{
System.out.println("ERROR");
}
}