Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove the first and the last characters from a Multimap's String representation?

Tags:

java

syntax

guava

I am trying to output the result of the a Multimap.get() to a file. However I get the [ and ] characters appear as the first and last character respectively.

I tried to use this program, but it doesn't print any separators between the integers. How can I solve this problem?

package main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class App {

public static void main(String[] args) {

    File file = new File("test.txt");
    ArrayList<String> list = new ArrayList<String>();
    Multimap<Integer, String> newSortedMap = ArrayListMultimap.create();

    try {
        Scanner s = new Scanner(file);
        while (s.hasNext()) {
            list.add(s.next());
        }
        s.close();
    } catch (FileNotFoundException e) {
        System.out.println("File cannot be found in root folder");
        ;
    }

    for (String word : list) {
        int key = findKey.convertKey(word);
        newSortedMap.put(key, word);
    }

    // Overwrites old output.txt
    try {
        PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
        for (Integer key: newSortedMap.keySet()) {
            writer.println(newSortedMap.get(key));
        }
        writer.close(); 
    } catch (FileNotFoundException e) {
        System.out.println("FileNotFoundException e should not occur");
    } catch (UnsupportedEncodingException e) {
        System.out.println("UnsupportedEncodingException has occured");
    }
}
like image 449
SDG Avatar asked Aug 25 '15 03:08

SDG


People also ask

How do I remove the last character of a string?

The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.

How do you remove the first and last character of a string in C++?

To remove the first character of a string, we can use the built-in erase() function by passing the 0,1 as an arguments to it. Where 0 is the first character index, 1 is the number of characters we need to remove from that index. Note: The erase() function modifies the original string instead of creating a new string.


2 Answers

You may assign newSortedMap.get(key).toString() to a variable, lets say stringList. Now call writer.println(stringList.substring(1,stringList.length()-1));

Understand that when you pass a list into writer.println method, it will invoke the toString() method of the object and write the output. The list.toString() method returns a string with all values separated by , and adds [ and ] to the beginning and end of the string.

like image 127
Harsh Poddar Avatar answered Oct 17 '22 01:10

Harsh Poddar


Just modify the String itself using substring.

substring(int, int)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

So, convert the Map into a String. Then, 1 is the second character of the String representation, and use mapString.length() - 1 for the rest of it.

Here's some working code:

PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
String mapString = newSortedMap.toString();
writer.println(mapString.substring(1, mapString.length() - 1);
like image 44
durron597 Avatar answered Oct 16 '22 23:10

durron597