2

In the input file, there are 2 columns: 1) stem, 2) affixes. In my coding, i recognise each of the columns as tokens i.e. tokens[1] and tokens[2]. However, for tokens[2] the contents are: ng ny nge

stem  affixes
----  -------
nyak  ng ny nge

my problem here, how can I declare the contents under tokens[2]? Below are my the snippet of the coding:

try {
    FileInputStream fstream2 = new FileInputStream(file2);
    DataInputStream in2 = new DataInputStream(fstream2);
    BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));

    String str2 = "";
    String affixes = " ";

    while ((str2 = br2.readLine()) != null) {

        System.out.println("Original:" + str2);

        tokens = str2.split("\\s");

    if (tokens.length < 4) {
            continue;
        }

    String stem = tokens[1];
    System.out.println("stem is: " + stem);

// here is my point

   affixes = tokens[3].split(" ");
   for (int x=0; x < tokens.length; x++)
    System.out.println("affix is: " + affixes);  

     }  

    in2.close();
  } catch (Exception e) {
    System.err.println(e);
  } //end of try2
2
  • Could you post the output of this code? perhaps that would help us answer your question. Commented Aug 16, 2011 at 2:33
  • I'm not sure exactly what you're trying to achieve? Do you want tokens[2] to contain an array with each of the affixes as a separate item? Commented Aug 16, 2011 at 2:34

2 Answers 2

2

You are using tokens as an array (tokens[1]) and assigning the value of a String.split(" ") to it. So it makes things clear that the type of tokens is a String[] array.

Next, you are trying to set the value for affixes after splitting tokens[3], we know that tokens[3] is of type String so calling the split function on that string will yield another String[] array.

so the following is wrong because you are creating a String whereas you need String[]

String affixes = " ";

so the correct type should go like this:

String[] affixes = null;

then you can go ahead and assign it an array.

affixes = tokens[3].split(" ");
Sign up to request clarification or add additional context in comments.

2 Comments

it works in term of syntax..however it still consider affixes as one token that consists of ng nge ny..my expectation is ng = token[1], nge=token[2] and ny=token[3].
Make Affixes as an array. Now the values will be stored as: affixes[0] = ng, affixes[1] = nge, affixes[2] = ny
0

Are you looking for something like this?

public static void main(String[] args) {
    String line = "nyak  ng ny nge";
    MyObject object = new MyObject(line);
    System.out.println("Stem: " + object.stem);
    System.out.println("Affixes: ");
    for (String affix : object.affixes) {
        System.out.println("  " + affix);
    }
}

static class MyObject {
    public final String stem;
    public final String[] affixes;

    public MyObject(String line) {
        String[] stemSplit = line.split(" +", 2);
        stem = stemSplit[0];
        affixes = stemSplit[1].split(" +");
    }
}

Output:

Stem: nyak
Affixes: 
  ng
  ny
  nge

7 Comments

definitely...does the affixes can be consider as one token for each? as i will take 'ng' and check in the other file as well goes to 'ny' and 'nge'.
Yes, the affixes are stored in String[] affixes, which is a String array of length 3.
i've tried the coding however still did not working.the affixes only came out with tokens[1]: ng..whereby it considers the other tokens as tokens[1]. since im reading from the input file, is there any syntax that i missing out?
Is it actually a space character that separates the affixes, or might it be a tab or some other whitespace? Instead of split(" +"), you could try split("\\s+").
it is only one whitespace that separates the affixes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.