Question

I have written Java code for passing integer inputs from a text file e.g. 1 10 39 59 20 60 38 and I have to split the string when there is an empty space.

Inputs are given in a single line for input.txt

My code is:

public void reduce(Text key, Iterator<IntWritable> values,
        OutputCollector<Text, IntWritable> output, Reporter reporter)
        throws IOException {

    String calc;
    calc = key.toString();

    ArrayList<Integer> keys = new ArrayList<Integer>();
    String[] data = calc.split(" ");

    for (String s : data) {
        int intData = Integer.parseInt(s);
        keys.add(intData);
    }

    int val = 0;
    for (int a : keys) {
        // some tasks
    }
}

After splitting the lines I use the separated values for different tasks. My question is how to split all the values located in the same file (values also in different lines) and store them in an array?

Suppose if the following is the input given in the input.txt, how to split all the values and store them in an array?

Example input:

1 4 92 58 30 82
49 50 38 30 29 20
...

Expected output:

array1="1,4,92,58,30,82,49,50,38,30,29,20, .."

When I use my code for the above inputs, only the last line of the input file is considered - all the previous lines are neglected.

Was it helpful?

Solution

You can try the following code if you have commons-io-x.x in your class path. I have used commons-io-2.4 for demonstration. Also I have assumed the input as String here, you can use Integer.parseInt(String str) to get the integer values from your input file.

package com.stack.overflow.works.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;

/**
 * 
 * @author sarath_sivan
 *
 */

public class FileIOService {

    private static final String BLANK_SPACE = " ";

    public static void main(String[] args) {
        FileIOService.run();
    }

    public static void run() {
        long startTime = System.currentTimeMillis();
        String fileName = "C:/Users/sarath_sivan/Desktop/input.txt";
        FileIOService.display(split(getContent(fileName)));
        long stopTime = System.currentTimeMillis();
        long elapsedTime = stopTime - startTime;
        System.out.println("Total Time: "+ elapsedTime + " Milliseconds...");
    }

    public static void display(List<String> splitsList) {
        for (String split: splitsList) {
            System.out.println(split);
        }
    }

    public static List<String> split(String content) {
        List<String> splitsList = new ArrayList<String>(Arrays.asList(content.split(BLANK_SPACE)));
        return splitsList;
    }

    public static String getContent(String fileName) {
        File file = new File(fileName);
        String content = null;
        try {
            content = FileUtils.readFileToString(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

}

Hope this helps... Thank you very much!

OTHER TIPS

This is pure speculation, but it seems that these lines are the culprits:

ArrayList<Integer> keys = new ArrayList<Integer>();
            String [] data = calc.split(" ");

I assume you are invoking this method inside a for loop, which would of course only give you the last processed line. What you need to do is declare keys outside of the function so that way it isn't reinstantiated on each invocation of the surrounding function.

You could also check the calc String, my guess is that it will only contain one line and then you would have to check the

key.toString()

method.

besides, with java 1.7 you can initialize the ArrayList with

ArrayList<Integer> keys = new ArrayList<>();

It seems that you are not concatenating all lines of your file in the Text key parameter.

I guess that you can use Apache FileUtils to read your file in a single string var (http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html#readFileToString(java.io.File))

When you are reading a file you are basically invoking readLine() method which will give you a string containing the data of the current line. But in your case what will happen is when you are at the end of the file a string will be read and that string will be splitted and assigned to the string array. So here your previous values gets lost. So that to avoid this situation you will have to take one temporary array which will store your current result and then add that result into your resultant array.

String[] mainArray=new Array[255]; // 255 for example

String temp;

while((temp=br.readLine()!=null))
{

String[] tempArr=temp.split(" ");
addToMainArray(tempArr);

}

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top