0

I am trying to learn how to input from a .dat file and iterate it into a string array. From what I have read so far this is possible.

At the moment I am try to test if it is even writing anything to the array using one of my string variables.

I am truly stumped on how to get this to work. When I compile I am getting an error that StudentID string variable has never been initialized.

public class testInput
{
   static Scanner testanswers;
   static PrintWriter testresults;

   public static void main(String[] args)
   {
        testanswers = new Scanner(new FileReader("TestInput.dat"));
        testresults = new PrintWriter("TestOutput.dat"); 

        String StudentID;
        String answers;

        while(testanswers.hasNextLine())
        {
              StudentID = testanswers.next();
              answers = testanswers.next();
        }

        String[][] answerArray = new String[7][2];

        for(int row = 0; col < answerArray.length; col++)
        {
             for(int col = 0; col < answerArray.length; col++)
             {
                  answerArray[row][col] = StudentID:
                  System.out.print(answerArray[row][col]);
             }
        }


   }

}

.dat file looks like so:

TFFTFFTTTTFFTFTFTFTT
5
ABC54301 TFTFTFTT TFTFTFFTTFT
SJU12874 TTTFFTFFFTFTFFTTFTTF
KSPWFG47 FT  FT  FTFTFFTTFFTF
PAR38501 FFFFFFFFFFFFFFFFFFFF
MCY19507 TTTT TTTT TTTT TT TT

Is my logic all wrong here?

5
  • 1
    The Scanner next function returns a "token", which uses spaces to determine where the token begins and ends. This will not work on some of your input lines, such as the last one, which has spaces in the middle of the answers. Commented May 2, 2014 at 22:55
  • Yeah I am noticing that. Is there a way to substring these last 5 lines on the data file? Commented May 2, 2014 at 23:01
  • Using next() for the student ID and nextLine() to get the rest of the line may work, but since everything seems to be aligned in columns, it might be best to use nextLine() to get the whole line and substring to get the parts of that line. Commented May 2, 2014 at 23:04
  • @Sykix, See my answer. You can read line by line, and split into 2 pieces maximum with String.split(). Commented May 2, 2014 at 23:04
  • Thank you merlin. I will read through your code till I better understand the logic behind this. Was unaware of how line.split worked. Commented May 2, 2014 at 23:08

3 Answers 3

1

Local variables require explicit initialization before usage in Java. As @ajb mentioned in his comment below, the while loop might never run (because the file could be empty), so there is a possibility of use before initialization.

   String StudentID = "";
   String answers = "";

Also, you need to decide whether you will read by line or read by word. Don't mix hasNextLine() with next().

There are a variety of other syntactic issues with your code. Here is a working version.

import java.util.*;
import java.io.*;

public class testInput
{
   static Scanner testanswers;
   static PrintWriter testresults;

   public static void main(String[] args) throws IOException
   {
        testanswers = new Scanner(new FileReader("TestInput.dat"));

        String StudentID;
        String answers;
        // Read first two lines first to know how many records there are.
        String answerKey = testanswers.nextLine();
        int count = Integer.parseInt(testanswers.nextLine());

        // Allocate the array for the size needed.
        String[][] answerArray = new String[count][];

        for (int i = 0; i < count; i++)
        {
            String line = testanswers.nextLine();
            answerArray[i] = line.split(" ", 2);
        }


        for(int row = 0; row < answerArray.length; row++)
        {
             for(int col = 0; col < answerArray[row].length; col++)
             {
                  System.out.print(answerArray[row][col]);
             }
             System.out.println();
        }


   }

}
Sign up to request clarification or add additional context in comments.

2 Comments

This is not really true. Local variables do not always require explicit initialization. They do not need to be initialized if the compiler is convinced they will always be assigned before they're used. This won't be true in the above program because the while loop could be executed 0 times.
@ajb, Updated to consider nuances.
1

In the while loop you overwrite each time the value of StudentID. After the loop there will be only the last value. So you should try to store the values in an array, during you read the file, i.e. inside the while loop.

Comments

0

Your code is not good, after the while loop, you will have just one value of StudentID, the last. And why creating answerArray variable with 7 and 2 ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.