Search code examples
javafile-iodictionarybufferedreader

Take Strings from Text file and assign each line to value (2 at a time and insert into LinkedHashMap)


What I'm trying to do is, load a Text file, then take the values from each line and assign them to a variable in my program. Every two lines, I will insert them into a LinkedHashMap (As a pair)

The problem with a buffered reader is, all I can seem to do is, read one line at a time.

Here is my current code:

public static void receiver(String firstArg) {// Receives
                                                // Input
                                                // File
    String cipherText;
    String key;
    String inFile = new File(firstArg).getAbsolutePath();
    Path file = new File(inFile).toPath();

    // File in = new File(inFile);
    try (InputStream in = Files.newInputStream(file);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in))) {
        String line = null;

        while ((line = reader.readLine()) != null) {
            // System.out.println(line);
            String[] arrayLine = line.split("\n"); // here you are
                                                    // splitting
                                                    // with whitespace

            cipherText = arrayLine[0];
            // key = arrayLine[1];
            System.out.println(arrayLine[0] + " " + arrayLine[1]);

            cipherKeyPairs.put(arrayLine[0], arrayLine[1]);
        }
    } catch (IOException x) {
        System.err.println(x);
    }

The problem is, it can't find the arrayLine[1] (for obvious reasons). I need it to read two lines at a time without the array going out of bounds.

Any idea how to do this, so that I can store them into my LinkedHashMap, two lines at a time as separate values.


Solution

  • You can overcome this issue by inserting in the List every 2 lines reading.
    A description for this code is that: "Bold is the true case"

    1. Read the first line (count is 0)
      • If (secondLine is false) ==> Save the line to CipherText variable, make secondLine = true
      • Else If (secondLine is true) ==> Add to list (CipherText, line), make secondLine = false
    2. Read the second line (count is 1)
      • If (secondLine is false) ==> Save the line to CipherText variable, make secondLine = true
      • Else If (secondLine is true) ==> Add to list (CipherText, line), make secondLine = false

    String cipherText;
    boolean secondLine = false;
    String inFile = new File(firstArg).getAbsolutePath();
    Path file = new File(inFile).toPath();
    
    try {
        InputStream in = Files.newInputStream(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
    
        String line = null;
    
        while ((line = reader.readLine()) != null) {
    
    
            if (!secondLine) //first line reading
            {
                cipherText = line; 
                secondLine = true;
            }
            else if (secondLine) //second line reading
            {   
                cipherKeyPairs.put(cipherText, line);
                secondLine = false;
            }
        }
    } catch (IOException x) {
        System.err.println(x);
    }