找出一种正确的数据读取方式

时间:2015-03-11 04:37:31

标签: java arrays arraylist messages coordinate-systems

https://drive.google.com/a/navasotaisd.org/file/d/0B3eMFMufj6uVaVNpR0JYNnV4OTQ/view

好的,所以上面的问题要求您在带有消息的文件中读取,并使用和x,y坐标系统,找到正在读入的字符并打印出该索引值的字符。老实说,我已经尝试了多种解决方案,包括制作和阵列的arraylists,arraylists的arraylist和许多其他失败的数据结构。我需要知道的是,如何阅读信息以便我可以搜索它?

File f = new File("cipher.in");
    f.createNewFile();
    Scanner scan = new Scanner(f);
    int numOfLines = scan.nextInt();
    scan.nextLine();
    ArrayList<Character> list = new ArrayList();
    String code = "";
    for (int i = 0; i < numOfLines; i++) {
        code = scan.nextLine();
        for (int j = 0; j < code.length(); j++) {
            list.add(code.charAt(j));
        }
    }
    int index = 0;
    char[][] matrix = new char[(int)(list.size())][(int)(list.size())];
    for (int r = 0; r < matrix.length; r++) {
        for (int c = 0; c < matrix[r].length; c++) {
            matrix[r][c] = list.get(index);
            index++;
            if(index>=list.size())
                index--;
        }
    }

对不起,如果这个问题有点长。对于我的班级,我需要将此问题转化为能够在成绩簿中制作100。我只是完全陷入困境和沮丧。

3 个答案:

答案 0 :(得分:0)

尝试使用哈希表来存储数据。您稍后可以搜索它。

答案 1 :(得分:0)

该键非常适合二维charcter数组(char [] [])。我会考虑读第一行(指定键上的行数(数组的行),然后构造数组。你有一个约束定义一行不能超过100个字符,所以你现在可以定义完整的“地图”。

之后我会读取键的每一行,使用运算符作为charAt(索引)并填充地图。

从那里你有一个非常方便的结构来查找作业的下一部分中的消息。

答案 2 :(得分:0)

尝试这一点,这是你的代码,包含更改和注释,以便您可以根据链接中的指令了解更改和此代码的工作方式,我没有尝试编译它,所以如果它有任何编译错误试图解决它们。我保留了您的代码并注释掉了不需要的项目,以便您可以查看差异,

File f = new File("cipher.in");
    //f.createNewFile();           //*  you are overwriting the file here
    Scanner scan = new Scanner(f);
    int numOfLines = scan.nextInt();
    //str = scan.nextLine();      //* you just skipped one line from the numOfLInes
    //ArrayList<Character> list = new ArrayList();  //* this does not help, you need to index into the line number, char index
    TreeMap charMap = new TreeMap();                //* use this to map the line number to a char array
    String code = "";
    for (int i = 0; i < numOfLines; i++) {
        strubg code = scan.nextLine();
        charMap.put(i, code.toCharArray());         //* map the line number with the char array of each line
        //for (int j = 0; j < code.length(); j++) {
        //    list.add(code.charAt(j));
        //}
    }
    int numOfMessageLines = scan.nextInt();      //* get the number of message lines next  
    for (int i = 0; i < numOfMessageLines; i++) {
      string  str = scan.nextLine();
      string[] pairs = str.split(" ");      //* each line has several key pairs for line number char number seprated by spaces
      ArrayList<char> list = new ArrayList();  //* this does not help, you need to index into the line number, char index
      for(int j=0; j<pairs.length; j++)
      {
        string[] st = pairs[j].trim().split(",");  //* example 2,13 indicate line 2 character 13 non zero indexed
        int lineNum = Integer.parse(st[0]) - 1;    //* zero indexed line number since we stored the lines in zero index map
        int charNum = Integer.parse(st[1]) - 1;    //* zero indexed char number since we stored the char array in zero indexed array
        char[] chars = charMap.get(lineNum);       //* get the char array for this line number
        char c = chars[charNum];                   //* get the character for the first message
        list.add(c);
      }  
      String message = new String(list.toArray());   //* construct the message from the char array
      System.out.println(message);
    }

    //int index = 0;
    //char[][] matrix = new char[(int)(list.size())][(int)(list.size())];
    //for (int r = 0; r < matrix.length; r++) {
    //    for (int c = 0; c < matrix[r].length; c++) {
    //        matrix[r][c] = list.get(index);
    //        index++;
    //        if(index>=list.size())
    //            index--;
    //    }
    //}