-2

the task here is to create a file, create a object called Student that takes a name, age gpa. the file is filled with lines that look roughly like this name=Jane Robinson,age=19,gpa=3.81 while reading each line i am to split the "," and the "=" and then check each index of array for "name", "age", "gpa" if one of those are found, to then substring(startpostion, rest of string); and save that as a variable, call the Student setters for each variable and set them, then load the newly set up Student objects into a arrayList called result, that I may then return it.

now im assuming, and only assuming this as this could be where the logic error is, that after both splits are complete each file string should look like this [nameJaneRobinson] [age19] [gpa3.81] where I'm using [] to identify them as separate objects in the array with there own index positions.

so getting on with the current problem is, i have this mostly coded, but well it doesnt work, nor can I figure how its not working because i cant get it to compile, and i cant seem to resolve these compile errors, as well as there may be a logic error im missing but havent spotted it yet, so if anyone can spot any of the causes for me and give me a solution for this that would be great.

code here:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * 
 *
 *
 *
 */
public class StudentReader {
  public static Student[] readFromTextFile(String fileName) {
  ArrayList<Student> result = new ArrayList<Student>();
   String name =" ";
   int age = 0;
   double gpa = 0.0;
   String fill;
   
   File f = new File(fileName);
   
   try
   {
      Scanner n = new Scanner(f);
   }
  catch(FileNotFoundException ex)
   {
    ex.printStackTrace();
   }
   
   while (n.hasNextLine())
   {
     fill = n.nextLine();
     Student g =  new Student(name , age, gpa);
     String[] string1 = fill.split(",");
     
     String[][] string2 = new String[string1.length][];
     for (int i = 0; i < string1.length; ++i)
     {
       
       string2[i] = string1[i].split("=");
      if (string2[i].substring(0,4).equals("name"))
        {
      name = string2[i].substring(4,string2[i].length());
        }
      if(string2[i].substring(0,3).equals("age"))
        {
      age = Integer.parseInt(string2[i].substring(3,string2[i].length()));
        }
      if(string2[i].substring(0,3).equals("gpa"))
        {
      gpa = Double.parseDouble(string2[i].substring(3,string2[i].length()));
        }
     }
   } 
    return result.toArray(new Student[0]);
  }
}

compiler stack trace:



./StudentReader.java:33: error: cannot find symbol
      if (String2[i].substring(0,4).equals("name"))
                    ^
  symbol:   method substring(int,int)
  location: class String[]
./StudentReader.java:35: error: cannot find symbol
      name = String2[i].substring(4,String2[i].length());
                                              ^
  symbol:   method lenght()
  location: class String[]
./StudentReader.java:37: error: cannot find symbol
      if(String2[i].substring(0,3).equals("age"))
                   ^
  symbol:   method substring(int,int)
  location: class String[]
./StudentReader.java:39: error: cannot find symbol
      age = Integer.parseInt(String2[i].substring(3,String2[i].length()));
                                                              ^
  symbol:   method length()
  location: class String[]
./StudentReader.java:41: error: cannot find symbol
      if(String2[i].substring(0,3).equals("gpa"))
                   ^
  symbol:   method substring(int,int)
  location: class String[]
./StudentReader.java:43: error: cannot find symbol
      gpa = Double.parseDouble(String2[i].substring(3,String2[i].length()));
                                                                ^
  symbol:   method length()
  location: class String[]

if all those get resolved then theres another error that i cannot seem to solve but wont show up in the stack trace until all others are resolved and its:

./StudentReader.java:21: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
   Scanner n = new Scanner(f);
               ^
1 error

alright since thats the case and i need to loop throught the sub array as well, lets talk about somthing else, i only created the 2d array as a workaround for a error that throws at where string2 array is declared the error;

./StudentReader.java:41: error: incompatible types: String[] cannot be converted to String
       string2[i] = string1[i].split("=");
                                    ^
1 error

how would i resolve this instead, im thinking i need to clone the array but im unsure on how to do so, as i have never cloned before

  • Java naming conventions have variables and methods starting with a lower case letter. This makes it much easier to read for programmers and therefore easier to debug. It should be string2 instead of String2, and so on. – NomadMaker Oct 04 '20 at 03:00
  • The last error means that the ``new Scanner(f)`` should be inside the try-statement. – NomadMaker Oct 04 '20 at 03:01
  • You have a number of different compilation errors here with different causes and fixed. And then you ask about cloning. The question is too broad, and the breadth makes it difficult to address all of the question in one answer. – Stephen C Aug 28 '22 at 03:07

1 Answers1

0

You're getting all those cannot find symbol because you think you're calling String methods on a String object, but in reality, you're calling them on an array of type String. Arrays don't have a length() method, nor do they have a substring() method, etc.

So how did this happen? Look at how you defined your String2 2D array:

String[][] String2 = new String[String1.length][];

And look how you're calling String methods:

name = String2[i].substring(4,String2[i].lenght());

Let's forget for a moment that you spelled length() incorrectly. The main issue is that a two dimensional array in Java is really an array of arrays.

String2[i] is an array, not a String.

Say I declare a 2D array of String like this:

String[][] foo;

If I refer to

foo[0]

I get the sub-array at index 0.

If I refer to

foo[0][1]

I get the element in position 1 from the sub-array at index 0.

To iterate through all the elements in your 2D array, you need two loops, one that iterates through the sub-arrays, and the other to iterate through all the elements of each sub-array.

for(int subarrayCounter = 0; subarrayCounter < foo.length; subarrayCounter++)
{
    for(int elementCounter = 0; elementCounter < foo[subarrayCounter].length; elementCounter++)
    {
        // do stuff here
    }
}
MarsAtomic
  • 10,436
  • 5
  • 35
  • 56