0

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.

RiFF RAFF
  • 131
  • 2
  • 11
  • http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – JB Nizet Oct 12 '15 at 19:38
  • Can't you separate the lines by reading up until the newline character? – George Daramouskas Oct 12 '15 at 19:39
  • You definitely do NOT want two ArrayLists for this problem. That is needlessly complicating the issue. – Mage Xy Oct 12 '15 at 19:39
  • You are not supposed to create an array list with individual elements. You are supposed to create an array list of shapes, so you have to take the information in each line and use it when you create an instance of a shape class. So (a) work on the shape classes, and (b) research how to split or scan individual lines, and how to convert the elements that look like numbers into actual numbers. – RealSkeptic Oct 12 '15 at 19:44

1 Answers1

2

In Java, there is a method in the string class called split. You could use this to split your input string into an array of string elements like this:

String[] elements = geolist.split(",");

For the String

Circle,green,false,4.0

The resulting array would be:

{"Circle","green",false","4.0"}

This makes it much easier to deal with each element separately.

mistmurk
  • 36
  • 4