简体   繁体   中英

Java scanner separating data from an input file

I'm kinda new to java and I'm a little confused at the moment. Im trying to get some input data from a file and the like parse it/ and give it the correct fields for what its equal to. For example,my "in.txt"

Number: 2 Gender: 1 City: 4 Car: 7       
Gender: 1 City: 4 Number: 1                                                                                 
City: 2 Car: 1 Number: 3

I'm trying to get to where Number equals 2 and Gender equals 1, and City equals 4, etc.. So far I'm able to use java's Scanner to read the file so I know that its reading it right. So how would I be able to correspond the names to the numbers like (Number: 2). Should I split the line whenever it sees an ":"? Like give it a left and a right side, or would that mess up the rest of the line with "Gender: 1 City: 4 ..."? I'm trying different things as I'm typing this and I'm just becoming stuck in how I would separate the data from the file. I greatly appreciate any help. Thank you. This is my scanner file I'm using to get the file.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class example{
  public static void main(String[] args) throws FileNotFoundException{
    File inFile = new File("in.txt");
    Scanner input = new Scanner(inFile);
    while(input.hasNext()){
        String line = input.nextLine();
        System.out.println(line);
    }   
  }
}

If your file or the lines are always build like you described it, you can split for spaces and search in the array for your needed value eg "Number:" (if necessary you can remove the ":") and then watch in the array at this position + 1 and there is the value.

Splitting for ":" will end up in such an array:

{[Number],[ 2 Gender],[ 1 City],[ 4 Car],[ 7]}

so I think this is not the best solution.

One way to tackle the problem of identifying each key/value pair in each line would be to use regular expressions to find each pair in a line. The Java Tutorials on Regular Expressions provide a good, and fairly comprehensive, starting point on the topic.

To get you started on this, the regular expression [A-Za-z]+: [0-9]+ will isolate each key-value pair for you, and a regular expression helper such as this web page at regexplanet.com will help greatly with testing and to start to identify the code you would need.

Do you want to get these numbers? 1: If i understand correctly,you could do this.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Number {   
    public static void main(String[] args) {
        String regex = "\\d+";
        String candidate = "Number: 2 Gender: 1 City: 4 Car: 7  ";

        Pattern p = Pattern.compile(regex);
        Matcher matcher = p.matcher(candidate);
        while(matcher.find()){
            System.out.println(matcher.group());
        }
    }
}

2:if you can format your data,you can format to JSON ,and use json-lib.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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