我正在尝试读入一个文件,并且我设法读入了名称,但没有读入名称后面的数字。我需要将这些数字不是字符串,而是浮点数或双精度数。更糟糕的是,我必须读入两个数字。请帮忙?(顺便说一句,在代码中我已经导入了必要的东西)
我必须阅读的一个例子:
麦当劳农场 , 118.8 45670
public class Popcorn{
public static void main (String [] args) throws IOException {
System.out.println("Enter the name of the file");
Scanner in = new Scanner(System.in);
String filename = in.next();
Scanner infile = new Scanner(new FileReader( filename)); //
String line = "" ;
//to get stuff from the file reader
while (infile.hasNextLine())
{ line= infile.nextLine();
// int endingIndex =line.indexOf(',');
// String fromName = line.substring(0, endingIndex); //this is to get the name of the farm
// if (fromName.length()>0){
// System.out.println (fromName);
// }
// else if (fromName.length()<= 0)
// System.out.println(""); some of the durdling that goes on
// }
while (infile.hasNextLine())
{
line= infile.nextLine().trim(); // added the call to trim to remove whitespace
if(line.length() > 0) // test to verify the line isn't blank
{
int endingIndex =line.indexOf(',');
String fromName = line.substring(0, endingIndex);
String rest = line.substring(endingIndex + 1);
// float numbers = Float.valueOf(rest.trim()).floatValue();
Scanner inLine = new Scanner(rest);
System.out.println(fromName);
}
}
}
}
}