当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Filter the Value form one Column and Displaying all Corresponding values in the Next Column from text File

问题描述:

i need a help to achieve below one.

I have requirement like , i need to populate all the corresponding values in column 2 if the the column 1 has "x" value i need output like below x- a1,a2,c2

y- b1,b2

Z-c1

File:

column 1, Column 2

x,a1

y,b1

Z,c1

x,a2

y,b2

x,c2

can you please help me out on this, how i can get the corresponding column 2 from txt file.

I have tried using the below code. But it's not giving desired result.

public class Test {

public static void main(String[] args)throws IOException {

 String filePath = "C:\\Test.txt";

HashMap<String, String> map = new HashMap<String, String>();

String line;

BufferedReader reader = new BufferedReader(new FileReader(filePath));

while ((line = reader.readLine()) != null)

{

String[] parts = line.split(",", 2);

if (parts.length >= 2)

{

String key = parts[0];

String value = parts[1];

map.put(key, value);

} else {

System.out.println("ignoring line: " + line);

}

}

for (String key : map.keySet())

{

System.out.println(key + ":" + map.get(key));

}

reader.close();

}}

Thanks,

Mani

分享给朋友:
您可能感兴趣的文章:
随机阅读: