About useDelimiter problems, tried for a long time still do not know what is the principle

Category: Java SE
 
lwecell
2013-01-19 04:01:29

Sponsored Links
I encountered a problem, read from a txt file U.S. states and state capitals, and then let the user to enter a random name of the state capital, to determine whether write on my

The idea is to build a two-dimensional array to store the State and the corresponding capital name, but when reading the file encountered some problems:

1. txt file format basically as follows:
Alabama - Montgomery
Alaska - Juneau
Arizona - Phoenix
Arkansas - Little Rock
California - Sacramento
When I was a space as a separator when, for example, such as the capital city of Little Rock name will be split into two words, which leads to the array length exceeds

2. Some states such as North Carolina's name like this, in itself will have a space in it, there will be the same problem, I am the first solution to the problem is to use. nextLine to read, so The name can read the entire capital in an array of space inside, but for what way the name of the state to complete the reading space without being affected too

3. For the middle of the "-" how to deal with it, I used the useDelimiter ("-") in such a way to deal with, but always get the desired results, do not know how to write parameter can be read correctly format

File myFile = new File ("d: \ \ stateCapitals.txt");

; Scanner input = new Scanner (myFile). useDelimiter ("-");
Scanner input1 = new Scanner (myFile). useDelimiter ("");

String [] [] SC = new String [50] [3];

int b = 0;
while (input.hasNext ()) {

; SC [b] [0] = input.next ();
; SC [b] [1] = input1.next ();
; SC [b] [2] = input.nextLine ();
; b + +;

}

This is my general procedure code, please help solve problems reading the test many times without success, thank you!

Sponsored Links

srhkkk
2013-01-19 04:05:41
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws Exception {
        String[][] ins = new String[5][2];
        int i = 0;
        BufferedReader br = new BufferedReader(new FileReader(new File(
                "d:/stateCapitals.txt")));
        String s = "";
        while ((s = br.readLine()) != null) {
            String[] ss = s.split(" - ");
            ins[i][0]=ss[0];
            ins[i++][1]=ss[1].trim();
        }
        br.close();
        System.out.println(Arrays.deepToString(ins));
    }
}
wunai1235
2013-01-19 04:16:27
import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class StateParser {

    public static void main(String[] args) throws FileNotFoundException {
        StateParser parser = new StateParser();
        List<State> states = parser.parse("i:/test/test.txt");
        for(State state : states) {
            System.out.println(state);
        }
    }

    public List<State> parse(String filename) throws FileNotFoundException {
        Scanner s = new Scanner(new File(filename)).useDelimiter("\\s*(?:\r?\n|^|$)\\s*|\\s*-\\s*");
        List<State> states = new LinkedList<State>();
        while(s.hasNext()) {
            State state = new State(s.next(), s.next());
            states.add(state);
        }
        return states;
    }
}

public class State {
    private String state;
    private String capital;

    public State() {
    }

    public State(String state, String capital) {
        this.state = state;
        this.capital = capital;
    }

    public void setState(String state) {
        this.state = state;
    }
    public String getState() {
        return this.state;
    }

    public void setCapital(String capital) {
        this.capital = capital;
    }
    public String getCapital() {
        return this.capital;
    }

    public String toString() {
        return "state: " + this.state + ", capital: " + this.capital + "|";
    }
}


The few scattered spaces, as well as between the state and the state capital "-" as a delimiter scanner

\ \ s * (?: \ r? \ n | ^ | $) \ \ s * remove newline spaces before and after, as well as leading and trailing spaces
|
\ \ s * - \ \ s * states and the state capital separator between
xiaobinghuazi
2013-01-19 04:24:09
1. Clearly a better solution with Map
2. reading, then use String.split split continents and capital
sjjjjjjjj
2013-01-19 04:40:47
can help explain \ \ s What is the meaning of it, is not something a regular expression, I saw some examples online is so used, part of my regex seems to grasp the bad
abc0675
2013-01-19 04:48:59
\ \ s regular expression null character
vinking_yip
2013-01-19 04:53:12
Thank you, and we know which sites have speaking JAVA regex do better, I am now learning programming books say this part is too small, not very clear
peterxiaoq
2013-01-19 05:08:24
here There this post, you look right you some help:
yxl1018
2013-01-19 05:10:44


this site oh
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.