简体   繁体   English

如何从文件中的一行读取单词和整数

[英]How to read word and integers from a line in a file

I have a code here:- 我在这里有一个代码:

package testFiles;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

public class ReadFile {
public static void main(String[] args){
    FileReader in=null;
    FileWriter out=null;
    String line;
    File fp=new File("readFrom.txt");
    try {
        Scanner sc=new Scanner(fp);
        //System.out.println(sc.next());
        if(sc.next().contentEquals("Coding")){
            System.out.println("####");
            while(sc.next().contentEquals("\n")==false){
                if(sc.nextInt()==1){
                    System.out.println("Coding is set.");
                }
                else{
                    System.out.println("Coding is not set.");
                }
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

What i intent to do :I want to read a word from the file say "Coding". 我打算做什么 :我想从文件中读取一个单词,说“编码”。 There will an integer after a space after this word.ie. 在这个单词后面的空格后面将有一个整数。 the file would be: Coding 1 On reading 'Coding' the program should read the corresponding number and return 'set' if the number is 1,and 'not set' if the number is not 1. 该文件为: Coding 1在读取“编码”时,程序应读取相应的数字,如果数字为1,则返回“ set”,如果数字不是1,则返回“ not set”。

My problem: I can read the string and verify if it's Coding.But i cant get the number. 我的问题:我可以读取字符串并验证它是否为Coding.But我无法获取数字。

what i want :I want the program to read the string and also the number corresponding and return statements based on the conditions.Remember the word "Coding" and the number are in the same line. 我想要的是 :我希望程序读取字符串以及相应的数字,并根据条件返回语句。记住单词“ Coding”和数字在同一行。 Please Guide me. 请指导我。

You should add sc.hasNext(); 您应该添加sc.hasNext(); to your while condition. 到你的一会儿状态。 sc.next(); will read next value and you will lose it while checking your condition. 将读取下一个值,并且在检查条件时会丢失该值。 I also add some suggestions about stream close operation on finally . 我还finally添加了一些有关流关闭操作的建议。

Code at the below prints: 下列代码中的代码:

Coding
Coding is set.

"readFrom.txt" has text: "Coding 1" “ readFrom.txt”具有文本:“编码1”

public static void main(String[] args) {

    FileReader in = null;
    FileWriter out = null;
    File fp = new File("D:/readFrom.txt");
    Scanner sc = null;
    try {
        sc = new Scanner(fp);
        String str = "";
        while (sc.hasNext()) {
            str = sc.next();
            if (str.contentEquals("Coding")) {
                System.out.println(str);
                if (sc.nextInt() == 1) {
                    System.out.println("Coding is set.");
                } else {
                    System.out.println("Coding is not set.");
                }
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

        try {
            if (out != null)
                out.close(); // you should close it on finally
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            if (in != null)
                in.close(); // you should close it on finally
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            if (sc != null)
                sc.close(); // you should close it on finally
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Sample Code 样例代码

    while(sc.hasNext()){
        try{
           String str = sc.nextLine();
           String strArray[] = str.split(" ");
           if ( strArray.length > 1 &&  strArray[0].equals("Coding")){
               int count = Integer.parseInt(strArray[1]);
               // check for count value == 1 or  not and do processing
               if ( count == 1){
                    System.out.println("Coding set");
               }else{
                   System.out.println("Coding not set");
               }
           }else{
                System.out.println("Coding not set");
           }
         }catch(Exception err){
             err.printStackTrace();
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM