Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

java.util.scanner - How can i handle it with scanner (java)?

I have a question about scanner please;I working at a small company; we have a software; it generate a big text file; and we must get some useful information from it ; i want write a simple application with java for saving time; could you please guide me ?

for example i want this output ;

Output


RFID : 25 BLUID : 562 WifiID : 2610 RFID : 33

RFID Count : 2

and for example ;this is my Text file, because each generated file with our software has 14000 lines :)

--------------------------
AAAAAAAAAAAA;RFID=25;
BBBB;BBBBBBBB;BBBBBBBBBB;
CCCCC;fffdsfdsfdfsd;BLUID=562;dfsdfsf;
fgfdgdf;terter;fdgfdgtryt;
trtretrre;WifiID=2610;trterytuytutyu;
zxzxzxzxz;popopopwwepp;RFID:33;aasasds…
gfdgfgfd;gfdgfdgfd;fdgfgfgfd;

I test it with this source code but i can't handle it;

Scanner scanner = new Scanner("i:1.txt");

scanner.findInLine("RFID=");

if (scanner.hasNext())
System.out.println(scanner.next());
else
System.out.println("Error!");

please help me ;

Thanks a lot ...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's an example using StreamTokenizer:

import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Scanner;

public class ScannerTest {

    private static final String s = ""
        + "AAAAAAAAAAAA;RFID=25;
"
        + "BBBB;BBBBBBBB;BBBBBBBBBB;
"
        + "CCCCC;fffdsfdsfdfsd;BLUID=562;dfsdfsf;
"
        + "fgfdgdf;terter;fdgfdgtryt;
"
        + "trtretrre;WifiID=2610;trterytuytutyu;
"
        + "zxzxzxzxz;popopopwwepp;RFID:33;aasasds…
"
        + "gfdgfgfd;gfdgfdgfd;fdgfgfgfd;
";

    public static void main(String[] args) {
        long start = System.nanoTime();
        tokenize(s);
        System.out.println(System.nanoTime() - start);
        start = System.nanoTime();
        scan(s);
        System.out.println(System.nanoTime() - start);
    }

    private static void tokenize(String s) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        StreamTokenizer tokens = new StreamTokenizer(new StringReader(s));
        tokens.whitespaceChars(';', ';');
        try {
            int token;
            String id;
            do {
                id = tokens.sval;
                token = tokens.nextToken();
                if (token == '=' || token == ':') {
                    token = tokens.nextToken();
                    Integer count = map.get(id);
                    map.put(id, count == null ? 1 : count + 1);
                    System.out.println(id + ":" + (int) tokens.nval);
                }
            } while (token != StreamTokenizer.TT_EOF);
            System.out.println("Counts:" + map);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void scan(String s) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        Scanner scanner = new Scanner(s).useDelimiter(";");
        while (scanner.hasNext()) {
            String token = scanner.next();
            String[] split = token.split(":");
            if (split.length == 2) {
                Integer count = map.get(split[0]);
                map.put(split[0], count == null ? 1 : count + 1);
                System.out.println(split[0] + ":" + split[1]);
            } else {
                split = token.split("=");
                if (split.length == 2) {
                    Integer count = map.get(split[0]);
                    map.put(split[0], count == null ? 1 : count + 1);
                    System.out.println(split[0] + ":" + split[1]);
                }
            }
        }
        scanner.close();
        System.out.println("Counts:" + map);
    }
}
RFID:25
BLUID:562
WifiID:2610
RFID:33
Counts:{RFID=2, BLUID=1, WifiID=1}
1103000
RFID:25
BLUID:562
WifiID:2610
RFID:33
Counts:{RFID=2, BLUID=1, WifiID=1}
22772000

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...