0
我想读取一个文件,我已经设法读取名称后面的数字,但不读取名称后面的数字。我需要让这些数字不是进入字符串,而是进入浮动或双打。更糟糕的是,有两个数字我必须阅读。帮助请吗? (顺便说一下在我的代码导入了必要的东西)如何从Java中的文件中读取数字?
什么我要读的一个例子:
McDonlad的农场,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);
}
}
}
}
}
你知道你是跳过第一线,由于重复'while'循环? – jlordo 2013-03-01 12:56:29