so I've been given an assignment with array lists and I can't quite seem to figure out how to add each element in a text file to a seperate index in the array list (rather index 0 being the entire first line, index 1 being the second entire line etc.).
So the code i currently have is
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
public class hw2redo
{
public static void main(String args[]) throws FileNotFoundException
{
//Scan file for data
GeometricObject g = null;
BufferedReader file = new BufferedReader(new FileReader("file.txt"));
Scanner diskScanner = new Scanner(file);
//Create dynamic array list
ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
//Scan data and add data to list
while(diskScanner.hasNext())
{
String geolist = diskScanner.next();
g = recreateObject(geolist);
list.add(g);
}
showObjects(list);
System.out.println("");
}
private static GeometricObject recreateObject(String data)
{
GeometricObject object = new GeometricObject(data);
return object;
}
private static void showObjects(ArrayList<GeometricObject> list)
{
for(GeometricObject o : list)
{
System.out.println(o);
System.out.println(list.get(0));
}
}
}
class GeometricObject
{
private String data;
public GeometricObject()
{
}
public GeometricObject(String data)
{
this.data = data;
}
@Override
public String toString()
{
return data;
}
}
class SimpleCircle extends GeometricObject
{
double radius;
/** Construct a circle with radius 1 */
SimpleCircle()
{
radius = 1;
}
/** Construct a circle with a specified radius */
SimpleCircle(double newRadius)
{
radius = newRadius;
}
/** Return the area of this circle */
double getArea()
{
return radius * radius * Math.PI;
}
/** Return the perimeter of this circle */
double getPerimeter()
{
return 2 * radius * Math.PI;
}
/** Set a new radius for this circle */
void setRadius(double newRadius)
{
radius = newRadius;
}
}
and my file.txt conatins
Circle,green,false,4.0
Circle,blue,false,2.0
Circle,blue,true,7.0
Rectangle,orange,true,10.0,6.0
Rectangle,green,false,5.0,11.0
Rectangle,red,true,14.0,12.0
So, when i run my code how it is, my output is
Circle,green,false,4.0
Circle,green,false,4.0
Circle,blue,false,2.0
Circle,green,false,4.0
Circle,blue,true,7.0
Circle,green,false,4.0
Rectangle,orange,true,10.0,6.0
Circle,green,false,4.0
Rectangle,green,false,5.0,11.0
Circle,green,false,4.0
Rectangle,red,true,14.0,12.0
Circle,green,false,4.0
This is as I expected, because it is taking index 0 as the entire first line. My question is if there is any way to add each seperate element to an entire new array. For example get(0) would return circle, get(1) would return green etc.
The assignment requires my output to look like
csu.Lin.Circle@55f96302
GeometricObject [color=red, filled=false, dateOfCreation=Wed Feb 11 12:21:51 EST
2015]
Circle [ radius= 4.0 Area=50.27 Perimeter=25.13 ]
so I believe I will have to use individual elements methods? in the rectangle and circle classes i will create but i cannot figure out how to create the arraylist with individual elements rather than entire lines.