0

This is the text file that I need to recognize the shape name and the length, width, radius and then send it to its constructor class. I have a triangle.java, rectangle.java, circle.java, and a shape.java(abstract class) all these classes have set and get methods for the width, length, radius and calculate its area. I need to read the numbers from this text file and send it to its appropriate class.

Rectangle
Red Rectangle
12.0
34.0
Rectangle
Square
12.0
12.0
Triangle
Right Triangle
3.0
4.0
5.0
Triangle
Right Triangle
3.0
4.0
1.0
Circle
Red Circle
2.5

what I have so far is this in my helper class

Scanner input = new Scanner(new File(
            "/Users/jb/eclipse-workspace2/CSI201Projects/src/Project05/Shapes.txt"));

    ArrayList<Shape> list = new ArrayList<Shape>();

    while (input.hasNextLine()) {
        String line = input.nextLine();
        String[] tokens = line.split("\n");
        if (tokens[1].equals("Circle")) {
            Circle c = new Circle();
            float radius = Float.parseFloat(tokens[1]);
            c.setRadius(radius);
            String color = String.valueOf(tokens[2]);
            c.setName(color);
           // Boolean filled = Boolean.valueOf(tokens[3]);
            c.getArea();
          //  c.getArea();
            list.add(c);
            System.out.println(c.toString());
        } else if (tokens[1].equals("Rectangle")) {
            Rectangle r = new Rectangle();
            float length = Integer.parseInt(tokens[1]);
            r.setlength(length);
            float width = Integer.parseInt(tokens[2]);
            r.setwidth(width);
            String name = String.valueOf(tokens[3]);
            r.setName(name);
           // Boolean filled = Boolean.valueOf(tokens[4]);
           // r.setFilled(filled);
            r.getArea();
            list.add(r);
            System.out.println(r.toString());
        }

But this is wrong because I get errors and it doesn't print or calculate. I don't know how to fix it. This is my main abstract shape class.

abstract class Shape{
    String name;
    //public abstract double area();
    public Shape() {
        name=this.getClass().getName();
        //this.name= name;
    }
    
    public Shape(String name) {
        this.name= name;
    }
    
    public String getName(String name) {
        return name;
    }

    public void setName(String name) {
        this.name=name;
    }
    
    //public abstract double area();
    
    public double getArea() {
        return getArea();
    }


    public String toString() {
        //return super.toString()+ this.name;
        //return "\nShape: "+ name+ "\n";
        return this.getClass().getSimpleName() ;
    
    }
    
    public boolean equals(Object obj){
        if (!(obj instanceof Shape)){
            return false;
        }
        
        Shape other = (Shape)obj;
        return (this.name).equalsIgnoreCase(other.name);
    }
}

This is one of my subclasses for rectangle.java

class Rectangle extends Shape{
    double width;
    double length;
    
    public Rectangle() {
        super("Rectangle");
        width=1.0;
        length=1.0;
    }
    
    public Rectangle(double w, double l, String name) {
        super(name);
        length= l;
        width= w;
        
    }
    
    public double getwidth() {
        
        return width;
    }
    public void setwidth( double w) {
        
        width=w;
    }
    
    public double getlength() {
        //return this.length;
        return length;
    }
    
    public void setlength( double l) {
        length=l;
    }
    

    public double getArea() {
        return length*width;
    }
    
    public String toString() {
        return super.toString()+ " \nLength: " + length+ " \nWidth: " +width+ " \nArea: " + getArea();
    }

    public boolean equals(Object obj){
        if (!(obj instanceof Rectangle)){
            return false;
        }
        
        Rectangle other = (Rectangle)obj;
        return super.equals(obj)&& this.width == other.width && this.length == other.length;
        
    }
Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
  • You said "But this is wrong because I get errors", but you haven't posted what the error messages are. From a quick look at your code, I suspect you are getting `ArrayIndexOutOfBoundsException`s because a) you are assuming that arrays are indexed from 1 but they are actually indexed from 0, and b) you only have 1 token per line anyway (except the colour line), and c) splitting a single line (read by nextline()) on `\n`s isn't going to do anything. – Jelaby Apr 27 '21 at 08:21
  • It would help you to be able to understand the error messages. This answer might help: https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Jelaby Apr 27 '21 at 08:22

0 Answers0