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?
Scannernextfunction 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.next()for the student ID andnextLine()to get the rest of the line may work, but since everything seems to be aligned in columns, it might be best to usenextLine()to get the whole line andsubstringto get the parts of that line.