java - How do you remove the first and the last characters from a Multimap's String representation? -
i trying output result of multimap.get()
file. [
, ]
characters appear first , last character respectively.
i tried use program, doesn't print separators between integers. how can solve 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 found in root folder"); ; } (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"); (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"); } }
you may assign newsortedmap.get(key).tostring()
variable, lets stringlist
. call writer.println(stringlist.substring(1,stringlist.length()-1));
understand when pass list writer.println
method, invoke tostring()
method of object , write output. list.tostring()
method returns string values separated ,
, adds [
, ]
beginning , end of string.
Comments
Post a Comment