【问题标题】:String not returning any value字符串不返回任何值
【发布时间】:2016-02-07 01:58:37
【问题描述】:

我正在尝试将 15 位二进制序列编码为 {0、1、2...7} 中的一种排列。例如,任何 15 位随机序列(如“001010101000101”)将被编码为排列值之一,例如“12345670”。我正在使用 HashMap,其中键表示 15 位序列,值表示 40320 个排列值之一。 Set 存储 15 位序列。我编写了以下似乎可以工作的代码,但最后“writeString.append”没有返回任何值并且变为空。有什么好的修复方法吗?

package wordnet;


    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;

    import javax.print.DocFlavor.STRING;

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




                String theString = "";

                File file = new File("C:\\Users\\Vinay\\WordNet\\test");
                Scanner scanner = new Scanner(file);

                theString = scanner.nextLine();
                while (scanner.hasNextLine()) {
                       theString = theString + "\n" + scanner.nextLine();
                }
                theString =theString.replaceAll("\\s+","");
                List<Integer> b = new ArrayList<Integer>();
                String a= "01234567";
                Set<String> permutations= crunchifyPermutation(a);
                Iterator<String> code = permutations.iterator();
                //System.out.println(permutations);
                Map<String, String> mapping = new HashMap<String, String>();
                File file1 = new File("C:\\Users\\Vinay\\WordNet\\output.txt");

                // if file doesnt exists, then create it
                if (!file1.exists()) {
                    file1.createNewFile();
                }

                FileWriter fw = new FileWriter(file1.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                StringBuffer writeString =new StringBuffer();
                for(int i=0,j=15;j<theString.length();i=i+15,j=j+15){
                    String value = theString.substring(j,i);
                    if(mapping.get(value)==null){
                        mapping.put(value, code.next());
                        //code.remove();

                    }

                    writeString.append(mapping.get(value));
                }
                System.out.println(writeString.toString());
                //bw.write((writeString.toString()));
                bw.close();
    }
        public static Set<String> crunchifyPermutation(String str) {
            Set<String> crunchifyResult = new HashSet<String>();
            if (str == null) {
                return null;
            } else if (str.length() == 0) {
                crunchifyResult.add("");
                return crunchifyResult;
            }

            char firstChar = str.charAt(0);
            String rem = str.substring(1);
            Set<String> words = crunchifyPermutation(rem);
            for (String newString : words) {
                for (int i = 0; i <= newString.length(); i++) {
                    crunchifyResult.add(crunchifyCharAdd(newString, firstChar, i));
                }
            }
            return crunchifyResult;
        }

        public static String crunchifyCharAdd(String str, char c, int j) {
            String first = str.substring(0, j);
            String last = str.substring(j);
            return first + c + last;
        }    

    }

【问题讨论】:

  • 您对String value = theString.substring(j,i); 有多少把握?您的开始索引看起来比结束索引大。
  • 是的。我也尝试过 (i.j) (我猜这是错误)。但是 writeString.append 仍然无法正常工作

标签: java arrays string hashmap hashset


【解决方案1】:

for 循环似乎提前终止了。使用

for (int i = 0, j = 15; i < theString.length(); i = i + 15, j = j + 15) {

为我带来了预期数量的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 2014-02-14
    • 1970-01-01
    • 2021-09-13
    • 2022-10-01
    • 2013-10-24
    • 2022-10-18
    相关资源
    最近更新 更多