0

I have a text file structured like this:

    class Object0 extends Object1
    class Object2 extends Object3
    class Object1
    class Object4 extends Object1
    class Object3

I want to split each string and store it. I know how to do this when the number of strings is known on each line, however in this case there can either be two or four words on a given line.

Here's what I have for splitting when the number of strings are known:

public static void main(String[] args) {

        try {
            File f = new File("test.txt");
            Scanner sc = new Scanner(f);
            while(sc.hasNextLine()){
                String line = sc.nextLine();
                String[] details = line.split(" ");
                String classIdentifier = details[0];
                String classNameFirst = details[1];
             // String classExtends = details[2];
             // String classNameSecond = details[3];
            }
        } catch (FileNotFoundException e) {         
            e.printStackTrace();
        }
2
  • 1
    Use details.length to check if it's 2 or 4. Commented Sep 13, 2017 at 6:36
  • What's your definition of a working code? Commented Sep 13, 2017 at 6:37

2 Answers 2

1

You can loop on the details array to get each splitted string no matter how many they are. Plus I made some changes in your main method to make it more correct (added a finally clause to close the Scanner resource).

public static void main(String[] args) {

    Scanner sc = null;
    try {
        File f = new File("test.txt");
        sc = new Scanner(f);
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] details = line.split(" ");
            for(String str: details) {
                //variable str contains each value of the string split
                System.out.println(str);
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        sc.close();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

On each iteration you can check if there are two more words, this way:

public static void main(String[] args) {

    try {
        File f = new File("test.txt");
        Scanner sc = new Scanner(f);
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] details = line.split(" ");
            String classIdentifier = details[0];
            String classNameFirst = details[1];
            // this can be done because you declared there
            // can be only 2 or 4 words per line
            if (details.length==4) {
                String classExtends = details[2];
                String classNameSecond = details[3];
            }
            // do something useful here with extracted fields (store?)
            // before they get destroyed in the next iteration
        }
    } catch (FileNotFoundException e) {         
        e.printStackTrace();
    }
}

Comments

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.