一次运行多个扫描仪

时间:2013-11-26 15:49:02

标签: java java.util.scanner

我有两台扫描仪,可以从外部文本文件中扫描货币和重量。货币的第一台扫描仪和重量的第二台扫描仪。这里的问题是,只有顶部扫描仪工作,第二个扫描仪不提供任何输出。如果第一台扫描仪完成扫描并输出后,如何重置第二台扫描仪以从文件顶部开始读取。以下是2个扫描仪

货币扫描器

String sc[]= {myTextCount};
String value, currency; 
for(String ssc : sc){
    Scanner scan = new Scanner(ssc);
    while (scan.hasNextLine()) { 
        value = scan.findInLine("\\d+(\\.\\d+)?");
        currency = scan.next();//scan.next("\\w+");
        try {
            float f = Float.valueOf(value.trim()).floatValue();
                for(int j = 0; j < currencyWords.length; j++) {
                    if ((currency).equals(currencyWords[j])) {
                        jTextArea2.append(f + currencyWords[j] + "-" +currencyFile + "\n"); 
                        currency = scan.next();//scan.next("\\w+");
                    } 
                }   
        }catch (NumberFormatException e){
            //System.err.println("NumberFormatExceptionsssss: " + nfe.getMessage());
        }
    } 
}   

重量扫描仪

String s[]= {myTextCount};
String weight, unit; 
for(String ss : s){
Scanner scanC = new Scanner(ss);
    while (scanC.hasNextLine()) { 
        weight = scanC.findInLine("\\d+(\\.\\d+)?");
        unit = scanC.next();//scan.next("\\w+");
        try {
            float f = Float.valueOf(weight.trim()).floatValue();
                for(int j = 0; j < weightWords.length; j++) {
                   if ((unit).equals(weightWords[j])) {
                      jTextArea2.append(f + weightWords[j] + "-" +weightFile + "\n"); 
                      unit = scanC.next();//scan.next("\\w+");
                   } 
                 }   
        }catch (NumberFormatException e){
            //System.err.println("NumberFormatExceptionsssss: " + nfe.getMessage());
        }
    }
} 

我尝试过使用break但仍然无效。仅第一个扫描仪运行,第二个扫描仪不运行。帮我展示如何重置扫描仪的算法

1 个答案:

答案 0 :(得分:0)

我通过加入两个扫描仪解决了这个问题。

String s[]= {myTextCount};
String weight, unit; 
for(String ss : s){
    Scanner scanC = new Scanner(ss);
    while (scanC.hasNextLine()) { 
        weight = scanC.findInLine("\\d+(\\.\\d+)?");
        unit = scanC.next();//scan.next("\\w+");
        try {
            float f = Float.valueOf(weight.trim()).floatValue();
                for(int j = 0; j < weightWords.length; j++) {
                    for(int g = 0; g < currencyWords.length; g++) {
                        if ((unit).equals(weightWords[j])) {
                            jTextArea2.append(f + weightWords[j] + "-" +weightFile + "\n"); 
                            unit = scanC.next();//scan.next("\\w+");
                        } scanC.reset();
                        if ((unit).equals(currencyWords[g])) {
                            jTextArea2.append(f + currencyWords[g] + "-" +currencyFile + "\n"); 
                            unit = scanC.next();//scan.next("\\w+");
                        }scanC.reset();
                    }
                }   
        }catch (NumberFormatException e){
            //System.err.println("NumberFormatExceptionsssss: " + nfe.getMessage());
        } 
}  scanC.close();
} 
相关问题