Java 将数据文件输入字符串数组

Java 将数据文件输入字符串数组,java,arrays,string,Java,Arrays,String,我试图学习如何从.dat文件输入并将其迭代到字符串数组中。从我目前所读到的情况来看,这是可能的 目前,我正在尝试测试它是否使用我的一个字符串变量向数组写入任何内容 我真的很难理解如何让它发挥作用。编译时,我得到一个错误,即StudentID字符串变量从未初始化过 public class testInput { static Scanner testanswers; static PrintWriter testresults; public static void main

我试图学习如何从.dat文件输入并将其迭代到字符串数组中。从我目前所读到的情况来看,这是可能的

目前,我正在尝试测试它是否使用我的一个字符串变量向数组写入任何内容

我真的很难理解如何让它发挥作用。编译时,我得到一个错误,即StudentID字符串变量从未初始化过

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]);
             }
        }


   }

}

我的逻辑都错了吗?

在Java中使用局部变量之前,需要显式初始化。正如@ajb在下面的评论中提到的,while循环可能永远不会运行(因为文件可能是空的),因此有可能在初始化之前使用

   String StudentID = "";
   String answers = "";
此外,你需要决定是逐行阅读还是逐字阅读。不要将
hasNextLine()
next()
混用

您的代码还有许多其他语法问题。这是一个工作版本

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();
        }


   }

}
import java.util.*;
导入java.io.*;
公共类测试输入
{
静态扫描仪测试传感器;
静态PrintWriter测试结果;
公共静态void main(字符串[]args)引发IOException
{
testanswers=新扫描仪(新文件阅读器(“TestInput.dat”);
弦乐学生;
字符串答案;
//先读前两行,知道有多少条记录。
字符串answerKey=testanswers.nextLine();
int count=Integer.parseInt(testanswers.nextLine());
//根据需要的大小分配阵列。
字符串[][]应答数组=新字符串[计数][];
for(int i=0;i
在while循环中,每次
StudentID的值都会被覆盖。循环之后将只有最后一个值。

因此,在读取文件期间,即在while循环中,您应该尝试将值存储在数组中。

您的代码不好,在while循环之后,您将只有一个StudentID值,即最后一个。为什么要用7和2创建answerArray变量?

事实并非如此。局部变量并不总是需要显式初始化。如果编译器确信它们在使用之前总是被分配的,则不需要初始化它们。在上面的程序中,这是不正确的,因为<代码>而循环可以执行0次。@ AJB,更新以考虑细微差别。<代码>扫描器< /C> >代码> NeX/Cuth>函数返回一个“令牌”,它使用空格来确定令牌的起点和终点。这不会对你的一些输入行起作用,比如最后一个,在答案中间有空格。是的,我注意到了。有没有办法把数据文件上最后5行的子串起来?对学生ID使用
next()
,并使用
nextLine()
来获取该行的其余部分可能会起作用,但由于所有内容似乎都是按列对齐的,因此最好使用
nextLine()
来获取整行内容并获取该行的部分。@Sykix,看看我的答案。您可以逐行阅读,并使用String.split()将其拆分为最多两部分。谢谢merlin。我将通读你的代码,直到我更好地理解背后的逻辑。不知道line.split是如何工作的。
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();
        }


   }

}