Попробуйте изменить код так, чтобы он совпадал с количеством вызовов функции sc.nextLine () с количеством строк, и я гарантирую, что ваша проблема будет решена.

учил исключение во время выполнения в моей программе, когда я читаю файл через сканер.

java.util.NoSuchElementException: No line found     
   at java.util.Scanner.nextLine(Unknown Source)    
   at Day1.ReadFile.read(ReadFile.java:49)  
   at Day1.ParseTree.main(ParseTree.java:17) 

Мой код:

while((str=sc.nextLine())!=null){
    i=0;
    if(str.equals("Locations"))
    {
        size=4;
        t=3;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Professions"))
    {
        size=3;
        t=2;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Individuals"))
    {
        size=4;
        t=4;
        str=sc.nextLine();
        str=sc.nextLine();
    }

int j=0;
String loc[]=new String[size];
while(j<size){
    beg=0;
    end=str.indexOf(',');
    if(end!=-1){
        tmp=str.substring(beg, end);
        beg=end+2;
    }
    if(end==-1)
    {
        tmp=str.substring(beg);
    }
    if(beg<str.length())
        str=str.substring(beg);
    loc[i]=tmp;
    i++;

    if(i==size ){
        if(t==3)
        {
            location.add(loc);
        }
        if(t==2)
        {
            profession.add(loc);
        }
        if(t==4)
        {
            individual.add(loc);
        }
        i=0;
    }
    j++;
    System.out.print("\n");
}

Ответы на вопрос(1)

Ваш ответ на вопрос