Coder
Coder

Reputation: 33

How to split strings and store them in list or map?

I am trying to read an input file, split the strings and store them in a list or map. Can anyone help?

I have to store it in a data structure such as a list or map and compare them with the string.equals function to match if it(String) is present in the data structure or not.

I have tried something like this. But in the output, I am getting "true" consecutively instead of strings.

public class Main {

    public static void main(String[] args) throws IOException {
        FileProcessor readInputFile;

        readInputFile = new FileProcessor(args[0]);
        String line = null;
        List<String> result = new ArrayList<>();

        while(null != (line = readInputFile.poll())) {
            String lines[] = line.split("::|__|,|=|-|\\]|\\[");
            System.out.println(result.add(String.valueOf(lines)));
            
        }
    }
}

Fileprocessor.java

public final class FileProcessor {
    private BufferedReader reader;

    /**
     * Constructs a FileProcessor that can stream the contents of the provided input file
     *  line by line.
     * @exception InvalidPathException On invalid path string.
     * @exception SecurityException On not having necessary read permissions to the input file.
     * @exception FileNotFoundException On input file not found.
     * @exception IOException On any I/O errors while reading lines from input file.
     */
    public FileProcessor(String inputFilePath)
            throws InvalidPathException, SecurityException, FileNotFoundException, IOException {

        if (!Files.exists(Paths.get(inputFilePath))) {
            throw new FileNotFoundException("invalid input file or input file in incorrect location");
        }

        reader = new BufferedReader(new FileReader(new File(inputFilePath)));
    }

    /**
     * Retrieves and returns the next line in the input file.
     *
     * @return String The next line read from the input file.
     * @exception IOException On error encountered when reading from input file.
     */
    public String poll() throws IOException {
        return reader.readLine();
    }

    /**
     * Close the buffered reader instance.
     *
     * @exception IOException On error encountered when closing the buffered reader.
     */
    public void close() throws IOException {
        reader.close();
    }
}

Input : input.txt

ADD_VIDEO::video1
ADD_VIDEO::video2
METRICS__video1::[VIEWS=1000,LIKES=20,DISLIKES=20]
AD_REQUEST__video1::LEN=8
METRICS__video2::[VIEWS=2000,LIKES=400,DISLIKES=20]
METRICS__video1::[VIEWS=20000,LIKES=1000,DISLIKES=-10]
AD_REQUEST__video2::LEN=39
METRICS__video2::[VIEWS=50,LIKES=-50,DISLIKES=0]
REMOVE_VIDEO::video2
ADD_VIDEO::video3
METRICS__video3::[VIEWS=2000,LIKES=100,DISLIKES=20]
METRICS__video1::[VIEWS=0,LIKES=-1000,DISLIKES=500]
ADD_VIDEO::video4
METRICS__video4::[VIEWS=100,LIKES=5,DISLIKES=0]
REMOVE_VIDEO::video1
AD_REQUEST__video3::LEN=15
REMOVE_VIDEO::video3
REMOVE_VIDEO::video4

Output Expected:

ADD_VIDEO
video1
ADD_VIDEO
video2
METRICS
video1
VIEWS
1000
LIKES
20
DISLIKES
20
AD_REQUEST
video1
LEN
8
METRICS
video2
VIEWS
2000
LIKES
400
DISLIKES
20
METRICS
video1
VIEWS
20000
LIKES
1000
DISLIKES
10
AD_REQUEST
video2
LEN
39
METRICS
video2
VIEWS
50
LIKES
50
DISLIKES
0
REMOVE_VIDEO
video2
ADD_VIDEO
video3
METRICS
video3
VIEWS
2000
LIKES
100
DISLIKES
20
METRICS
video1
VIEWS
0
LIKES
1000
DISLIKES
500
ADD_VIDEO
video4
METRICS
video4
VIEWS
100
LIKES
5
DISLIKES
0
REMOVE_VIDEO
video1
AD_REQUEST
video3
LEN
15
REMOVE_VIDEO
video3
REMOVE_VIDEO
video4

Output getting:

true
true
true
true
.
.
.
true

Upvotes: 0

Views: 273

Answers (6)

user11141611
user11141611

Reputation:

You can use Arrays.toString(...) to print this array in a single line:

System.out.println(Arrays.toString(lines));
// [ADD_VIDEO, video1, ADD_VIDEO, video2, ...]

or you can use Arrays.stream(...) to print separate lines:

Arrays.stream(lines).forEach(System.out::println);
// ADD_VIDEO
// video1
// ADD_VIDEO
// video2
// ...

Upvotes: 1

Sara W
Sara W

Reputation: 127

The problem here is that you are printing out the result of the function call result.add(). You will need to call result.add() on different line and call only String.valueOf(lines) or result.get(//index of last add) in the call to system.out. You can also loop through result afterward to get the same result.

Upvotes: 2

Oboe
Oboe

Reputation: 2723

I would use Guava 'Splitter' for that job:

String line = "METRICS__video1::[VIEWS=1000,LIKES=20,DISLIKES=20]";

List<String> result = Splitter.onPattern("::|__|,|=|-|\\]|\\[").omitEmptyStrings().splitToList(line);

result.forEach(str -> System.out.println(str));

This will print:

METRICS
video1
VIEWS
1000
LIKES
20
DISLIKES
20

If you want one big list for all you lines:

List<String> result = new ArrayList<>();

while(null != (line = readInputFile.poll())) {

    result.addAll(Splitter.onPattern("::|__|,|=|-|\\]|\\[").omitEmptyStrings().splitToList(line));
}

result.forEach(str -> System.out.println(str));

Upvotes: 0

xxce10xx
xxce10xx

Reputation: 79

The problem is that you are printing in the while loop if it was added to the list or not, which will give you true or false, you are not printing the element of the list

public class Main {

    public static void main(String[] args) throws IOException {
        FileProcessor readInputFile;

        readInputFile = new FileProcessor(args[0]);
        String line = null;
        List<String> result = new ArrayList<>();

        while(null != (line = readInputFile.poll())) {
            String lines[] = line.split("::|__|,|=|-|\\]|\\[");
            result.add(String.valueOf(lines)); 
        }
            result.stream().forEach(System.out::println);  
    }
}

Upvotes: 1

Gouse Mohiddin
Gouse Mohiddin

Reputation: 424

Your Split is working fine, The problem is you are printing boolean value returned by List.add(); result.add returning true which you are printing. check here

result.add(String.valueOf(lines));

For your output, you should be printing,

System.out.println(lines[0]);
System.out.println(lines[1]);

Upvotes: 1

Johannes Koch
Johannes Koch

Reputation: 21

Note: This is not a full answer, but it should enable you to solve your problem.

In your print statement System.out.println(result.add(String.valueOf(lines))); you print the return value of the add method instead of the string value you parsed. Try to print result instead.

Upvotes: 2

Related Questions