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");
}
}
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.
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.
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.
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 indexendIndex - 1
. Thus the length of the substring isendIndex-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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With