1

So basically what Im trying to do is make a vocab practice game in java. I have java reading the .txt file and reporting them to the console. The data is separated by the "|" character because I use commas in my .txt file.

Here Is The Code.

package sat.vocab.practice;

import java.io.FileReader;
import com.opencsv.CSVReader;
import java.util.Arrays;

public class SATVocabPractice {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception {
        CSVReader reader = new CSVReader(new FileReader("C:/Users/Jordan/Documents/GitHub/SAT-Vocab-Practice--New-/src/sat/vocab/practice/Words.txt"), '|' , '"' , 0);

        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            if (nextLine != null) {
                System.out.println(Arrays.toString(nextLine));
            }
        }
    }
} 

The words are formatted as follows.

Word | Type of Speach: Definition

labyrinth|n: 1. an intricate combination of paths or passages in which it is difficult to find one's way or to reach the exit. 2. any confusingly intricate state of things or events; a bewildering complex.
cower|v: 1. to crouch, as in fear or shame.

The Data That We Get Back From The Code Is Formatted As [ cower|v: 1. to crouch, as in fear or shame.]

I need the data to go into two array lists one for words (before the |) and one for definitions (after the |).

3
  • i like to use explode for this in php Commented Apr 5, 2015 at 19:35
  • 4
    @JoshuaByer It's not clear how that's helpful. Commented Apr 5, 2015 at 19:45
  • Please take care to format your code properly so it's easy to read and think about. Commented Apr 5, 2015 at 19:47

1 Answer 1

2

Try this:

String[] array = nextLine.split("\\|");

this function will split you String by character |. Assign this to new variable and use later to add each element to separate list.

array[0] this is the word

array[1] and this is the definition.

Sign up to request clarification or add additional context in comments.

2 Comments

Where would I add this into my code to add them to the array lists? Thank you
Inside the for-loop :)

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.