Tfarcraw IIV
Tfarcraw IIV

Reputation: 129

Reading Text From File Into Variables

I've got a text file formatted like this:

 2 
 d e 
 2 
 1 0 e 
 0 1 d 

I'd like to have a variable called size, which is equal to the first number, 4. I also need a variable, num, to hold the third line (3). I want to read the second line, d e, into an array. I would also like to read the last two lines into two arrays - one for chars, and the other for ints. I'll list my variables down here so it's easier to read:

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

I'm not really sure how to declare the size of each of these arrays before reading the text in the file. It's tricky because another text file could read:

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

The variables from this second file would be:

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

This is the fourth or fifth draft of my code. I've run into a wall. I know how to print out the file line by line, but I don't know how to do it variable by variable.

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

I'm getting this error:

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)

I want to have the results listed in the beginning:

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

EDIT: Here's the code I have now:

  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++;

          }  

paired with this text file:

2 
a b 
2 
0 0 a 
1 0 b

I get the output:

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)

Upvotes: 0

Views: 64

Answers (1)

Andreas
Andreas

Reputation: 159096

You start by calling nextInt() twice, but your file only starts with one number, so of course it'll fail in the second nextInt() call.

Your code should start out like this:

// 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

Upvotes: 1

Related Questions