Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 只需从文件中读取行中的数字_Java - Fatal编程技术网

Java 只需从文件中读取行中的数字

Java 只需从文件中读取行中的数字,java,Java,我在一个名为avar.txt的文件中有一行序列。在这个文件中,我试图读取一个不是以“;”字符开头的数字。为了做到这一点,我写了下面的代码,但我不知道如何获得其中的数字。是否有可能直接从文件中获取编号而不是我的方法,实际上我的方法不起作用 £Life <--------------- do not read £No File Name Contents <--------------- do

我在一个名为avar.txt的文件中有一行序列。在这个文件中,我试图读取一个不是以“;”字符开头的数字。为了做到这一点,我写了下面的代码,但我不知道如何获得其中的数字。是否有可能直接从文件中获取编号而不是我的方法,实际上我的方法不起作用

£Life                                  <--------------- do not read
£No File Name               Contents  <--------------- do not read

00  life.all                    ... 
£01 life.prt                    ...   <--------------- do not read
03  life.all                    ...
11  life.pl                     ...

£Expectation                           <--------------- do not read
£No File Name               Contents  <--------------- do not read
13  expectation.ops             ...
04  expectation.at              ...
£05 expectation.anil            ...   <--------------- do not read

我假设你只想用这个号码。如果你想用剩下的号码,请更新你的帖子,我会更新答案

因此,仅针对数字,您可以执行以下操作:

Scanner in = new Scanner(new FileInputStream("Avar.txt"));

while(in.hasNextLine){
    Scanner lineScanner = new Scanner(in.nextLine());
    if(lineScanner.hasNextInt()){
        int x = lineScanner.nextInt();
        // do something with x
    }

}
这将逐行读取您的输入。对于每一行,如果该行以int开头(以
“.01”
开头的行不以int开头),则该
int
将存储在变量
x
中。在那之后你可以做w/e。如果你想要所有的,你可以把它们放在一个
数组中传递

BufferedReader
版本:

BufferedReader file = new BufferedReader(new FileReader("Avar.txt"));
String line;
while((line = file.readLine()) != null){
    if(line.charAt(0) >= 48 && line.charAt(0) <= 57 ){
        String number = line.substring(0, 2);
        int x = Integer.valueOf(number);
        //do something with x
    }
}       
file.close();
BufferedReader file=newbufferedreader(newfilereader(“Avar.txt”);
弦线;
而((line=file.readLine())!=null){
如果(line.charAt(0)>=48&&line.charAt(0)
通过将行拆分为由空格符号分隔的部分,可以实现上述操作。

您可以这样做

File ifile = new File("avar.txt");
FileReader fr = new FileReader(ifile.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);

for (String line = br.readLine(); line != null; line = br.readLine()) {
  if (!line.startsWith("£")) {
       ...... Type your code.....//it takes line without "£" sign.
   }
 }
Scanner in = new Scanner("Avar.txt");
while(in.hasNextInt)
{
    int n = in.nextInt();
}
如果你想要这样的数字

File ifile = new File("avar.txt");
FileReader fr = new FileReader(ifile.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);

for (String line = br.readLine(); line != null; line = br.readLine()) {
  if (!line.startsWith("£")) {
       ...... Type your code.....//it takes line without "£" sign.
   }
 }
Scanner in = new Scanner("Avar.txt");
while(in.hasNextInt)
{
    int n = in.nextInt();
}

然后,您可以使用上面的语句,并通过尝试在try-catch语句中解析它来查看它是否是一个数字。如果它不是一个数字,它将出错并转到代码中文本文件的下一行,您在哪里“没有读取以“£”字符开头的行”?哪个语句?在while循环中,我将每一行传递到一个新的
扫描器
。然后我的
if
语句检查该行是否以
int
开头。
if(lineScanner.hasNextInt())
。正如我所说,以
.01
开头的一行并不是以
int
开头的,因此它不会进入
if
语句的内部,而是会继续到下一行。如果您仍然有问题,请告诉我。对于每一行,Scanner类的实例化实际上都非常难看。对于每一次,我都会使用一次,然后离开它。对于小文件来说,这是可以的,但是对于大文件来说,它会产生性能问题吗?首先,你在问题中没有提到性能是一个问题。其次,我们谈论的文件有多大?如果你在10的数千行中,那么缓冲类会做得更好是的。我认为你的代码在“£”的情况下不起作用。我不能保证文本正文中不会出现“镑”。我只是说,如果不是以“镑”字符开头,请阅读这一行。我错了吗?我想知道如何进行第一次推荐。换句话说,你如何从
中提取整数。顺便说一句,我并不反对你,事实上我没有足够的声誉。这要看情况而定根据您的逻辑和规则。假设每一行都以整数开头,但不带“£”符号。int i=0;String val=”“;而(line.charAt(i)!=”{val=val+line.charAt(i);i++;}或String val=“”;for(字符串s:line.split(“”){val=s;break;}