发布网友 发布时间:2024-10-18 05:30
共2个回答
热心网友 时间:2024-10-21 10:55
java代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class A {
public static void main(String[] args){
int seq=0;
HashMap<String,Integer> hm=new HashMap<String,Integer>();
FileInputStream fi = null;
BufferedReader br = null;
try {
fi=new FileInputStream(new File("D:/a.txt"));
br=new BufferedReader(new InputStreamReader(fi));
String s="";
while((s=br.readLine())!=null){
if(hm.get(s)==null){
hm.put(s, ++seq);
System.out.printf("%-20s%d\n",s,seq);
}else{
System.out.printf("%-20s%d\n", s,hm.get(s));
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
D盘下 a.txt
beijing
aa
afd
beijing
aa
qq
输入:
beijing 1
aa 2
afd 3
beijing 1
aa 2
qq 4
自己试试 不行找我
热心网友 时间:2024-10-21 10:53
import java.io.*;
import java.util.*;
public class A{
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("word.txt"));
String s = null;
List<String> wordList = new ArrayList<String>();
List<Integer> numList = new ArrayList<Integer>();
while((s = br.readLine()) != null){
int len = numList.size();
for(int i = 0;i < wordList.size();i++){
if(s.equals(wordList.get(i))){
numList.add(i + 1);
}
}
if(len == numList.size()){
numList.add(len + 1);
}
wordList.add(s);
}
for(int i = 0; i < wordList.size(); i++){
System.out.println(wordList.get(i) + "\t" + numList.get(i));
}
}
}