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