从文件读取文本到变量

时间:2019-01-30 23:27:35

标签: java

我有一个格式如下的文本文件:

 2 
 d e 
 2 
 1 0 e 
 0 1 d 

我想要一个名为size的变量,它等于第一个数字4。我还需要一个变量num来容纳第三行(3)。我想将第二行d e读入数组。我还想将最后两行读为两个数组-一个用于字符,另一种用于整数。我将在此处列出变量,以便于阅读:

size = 2   ---> first line
char[] chars = {d, e} ---> second line
num = 2  ---> third line
int[] pos = {1, 0, 0, 1} ---> Lines 4, 5
char[] posLetters = {e, d}  ---> Lines 4, 5

在读取文件中的文本之前,我不太确定如何声明每个数组的大小。这很棘手,因为另一个文本文件可以读取:

3
a b c
4
0 3 b
2 0 c
1 2 a
0 2 b

第二个文件中的变量为:

size = 3   ---> first line
char[] chars = {a, b, c} ---> second line
num = 4  ---> third line
int[] pos = {0, 3, 2, 0, 1, 2, 0, 2} ---> Lines 4, 5, 6, 7
char[] posLetters = {b, c, a, b}  ---> Lines 4, 5, 6, 7

这是我代码的第四或第五草稿。我碰到了墙。我知道如何逐行打印文件,但我不知道如何逐个变量地打印文件。

try {
         File configFile = new File(config + "config.txt");
         Scanner configScanner = new Scanner(configFile);

         int size = configScanner.nextInt(); 



         int num = configScanner.nextInt(); 


        /*  
          int[][] board = new int[size][size];
          for(int i = 0; i < size-1; i ++) {
              for(int j = 0; j < size-1; j++) {
                  board[i][j] = 
configScanner.nextInt();
              }
          }
         */

        } catch (FileNotFoundException e) {
          // handle the exception here
          System.err.println(e);
          e.printStackTrace();
        } // try

我收到此错误:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at LatinSqauresGame.printWelcome(LatinSqauresGame.java:59)
at LatinSqauresGame.<init>(LatinSqauresGame.java:13)
at LatinSquaresDriver.main(LatinSquaresDriver.java:6)

我想在开头列出结果:

size = 2   ---> first line
char[] chars = {d, e} ---> second line
num = 2  ---> third line
int[] pos = {1, 0, 0, 1} ---> Lines 4, 5
char[] posLetters = {e, d}  ---> Lines 4, 5

编辑:这是我现在拥有的代码:

  int size = variableScan.nextInt();
          char[] chars = new char[size];
          for (int i = 0; i < size; i++) {
              chars[i] = variableScan.next().charAt(0);
          }
          // Read pos and posLetters arrays
          int num = variableScan.nextInt();
          // code here to allocate and read pos and posLetters

          System.out.println(size);

          for (int i = 0; i < size; i++)
             System.out.println(chars[i]);

          System.out.println(num);

          int iter = 0;
          while(variableScan.hasNextLine()) {

            int[] pos = new int[num*2];
            for(int i = 0; i < 2; i++) {
                pos[i + (2*iter)] = variableScan.nextInt();
            }

            char[] posLetters = new char[num];
            for (int i = 0; i < 1; i++) {
                posLetters[i + iter] = variableScan.next().charAt(0);
            }

            iter++;

          }  

与此文本文件配对:

2 
a b 
2 
0 0 a 
1 0 b

我得到了输出:

2
a
b
2
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at LatinSqauresGame.printWelcome(LatinSqauresGame.java:77)
    at LatinSqauresGame.<init>(LatinSqauresGame.java:13)
    at LatinSquaresDriver.main(LatinSquaresDriver.java:6)

1 个答案:

答案 0 :(得分:1)

您首先调用nextInt() 两次,但是您的文件仅以一个数字开头,因此当然会在第二次nextInt()调用中失败。

您的代码应如下所示:

// Read chars array
int size = configScanner.nextInt();
char[] chars = new char[size];
for (int i = 0; i < size; i++)
    chars[i] = configScanner.next().charAt(0);

// Read pos and posLetters arrays
int num = configScanner.nextInt();
// code here to allocate and read pos and posLetters
相关问题