代码如下package test;import java.io.*;
import java.util.*;import javax.swing.JOptionPane;public class protest {
static String a = JOptionPane.showInputDialog("What do you want to Exchange?");
public static void readFile(String fileName) {//传入参数fileName是要读取的资源文件的文件名
InputStream in = null;
Properties pros = new Properties();
try {  
if (null != fileName) {
in = protest.class.getResourceAsStream(fileName);//得到当前类的路径,并把资源文件名作为输入流
pros.load(in);
Enumeration en = pros.propertyNames();//得到资源文件中的所有key值
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
char d[] = a.toCharArray();
for(int i=0;i<d.length;i++){
String b = String.valueOf(d[i]);
if(b.equals(key)){
System.out.print(pros.getProperty(key));}
}
//输出资源文件中的key与value值
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取资源文件出错");
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败");
}
}
}
public static void main(String[] args){
readFile("jpexchange.properties");
}
}要实现的功能是输入一个字符串,如“aBcDe”将其转换成“AbCdE”,但结果顺序却乱了,怎么回事啊??jpexchange.properties中的代码:
a=A
A=a
b=B
B=b
c=C
C=c
d=D
D=d
e=E
E=e

解决方案 »

  1.   


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package test1;import java.util.*;/**
     *
     * @author ilrxx
     */
    public class protest {
        public static List<String> getPro(String [] s){
            List<String> list = new ArrayList<String>();
            for(String s1 : s){
                if(s1.equals(s1.toLowerCase())){
                    list.add(s1.toUpperCase());
                }
                if(s1.equals(s1.toUpperCase())){
                    list.add(s1.toLowerCase());
                }
            }
            return list;
        }
        public static void main(String[] args){
            String[] str = {"a","B","c","D","e","F"};
            List<String> list = getPro(str);
            for(String s : list){
                System.out.print(s + " ");
            }
        }
    }
      

  2.   

    十分感谢,不过我想用的是从properties里读取的信息来转换,虽然比较麻烦了点,不过要求就是这个样子的,
    问题大概出在循环里面,但不知道怎么解决,望高手指点下,感激不尽啊!
      

  3.   

    文件流是逆向读取的
    Enumeration en = pros.propertyNames();// 得到资源文件中的所有key值
    System.out.println(en);
    这个地方就已经反了,这里处理一下,反序
      

  4.   

    这里用不着Enumeration,一个循环就可以了
    public static void readFile(String fileName) {
    InputStream in = null;
    Properties pros = new Properties();
    try {
    if (null != fileName) {
    in = proptest.class.getResourceAsStream(fileName);
    pros.load(in);
    /*Enumeration en = pros.propertyNames();
    while (en.hasMoreElements()) {
    String key = (String) en.nextElement();
    char d[] = a.toCharArray();
    for (int i = 0; i < d.length; i++) {
    String b = String.valueOf(d[i]);
    if (b.equals(key)) {
    System.out.print(pros.getProperty(key));
    }
    } }*/
    char d[] = a.toCharArray();
    for (int i = 0; i < d.length; i++) {
    String b = String.valueOf(d[i]); if (pros.getProperty(b) != null) {
    System.out.print(pros.getProperty(b));
    }
    }
    }
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println("");
    } finally {
    try {
    if (null != in) {
    in.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println("");
    }
    }
    }
      

  5.   

    仔细看了一下,原来你char d[] = a.toCharArray();的位置错了,导致每次都循环;
    if (null != fileName) {
        char d[] = a.toCharArray(); //放到这里就可以了
      

  6.   

    if (null != fileName) {
    char d[] = a.toCharArray();
    in = Test.class.getResourceAsStream(fileName);// 得到当前类的路径,并把资源文件名作为输入流
    pros.load(in);
    Enumeration en = pros.propertyNames();// 得到资源文件中的所有key值
    for (int i = 0; i < d.length; i++) {
    String b = String.valueOf(d[i]);
    while (en.hasMoreElements()) {
    String key = (String) en.nextElement();
    if (b.equals(key)) {
    System.out.print(pros.getProperty(key));
    }

    }
    en = pros.propertyNames();
    }
    }
    这样就可以了,问题出了不应该用Enum去循环的