Lỗi không đọc được file trong Java

Tình hình là mình đọc file như này:

Rectangle 5.5 3.7
Circle 	3.5
Circle		7.7
Rectangle  8.6   9.2
Circle  11   34

Nhưng không được, nó cứ bị lỗi ở hàm khởi tạo nhưng trên java không báo lỗi chạy mới ghi lỗi ở đó. Giúp mình với.
Cảm ơn.

package shape;

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

public class ArrayOfShape {
		
	private Shape[] s=new Shape[100];
	private int n;
	
	
	public int getN() {
		return n;
	}
	public void setN(int n) {
		if(n<0) this.n = 0 ;
		else this.n=n; 
	}
	
	
	
	public Shape[] getS() {
		return s;
	}
	public void setS(Shape s[]) {
		this.s=s;
	}
	
	
	
	public void input() {
		Scanner kb = new Scanner(System.in);
		System.out.println("Enter n : ");
		setN(kb.nextInt());
		for(int i=0;i<this.n;i++) {
			System.out.println("Enter Shape"+(i+1)+" : ");
			System.out.println("1-Triangle/2-Circle/3-Rectangle ");
			int a = kb.nextInt();
			Shape s = (a == 1 ? new Triangle() : a == 2 ? new Circle() : a == 3 ? new Rectangle() : null) ;
		}
	}
	
	
	
	public void output() {
		System.out.println("ArrayOfShape : ");
		for(int i = 0 ; i < this.n ; i++)
			s[i].output();
		System.out.println(this.n);
	}
	
	
	
	public void readFile() throws FileNotFoundException {
		Shape a ;
		Scanner kb = new Scanner( new File("src/shape/test.txt") ) ;
		while(kb.hasNextLine() == true) {
			
			if(kb.next().compareTo("circle") == 0) {
				a = new Circle(kb.nextDouble()) ;
				s[n] = a ;
				n++ ;
			}
			else {
				if(kb.next().compareTo("rectangle") == 0) {
					a = new Rectangle(kb.nextDouble(),kb.nextDouble()) ;
					s[n] = a ;
					n++ ;
				}
			}
			
		}
	}
	
	
	
	public void sortArea() throws FileNotFoundException {
		
		for(int i = 0 ; i < this.n ; i++) 
			for(int j = i+1 ; j < this.n ; j++)
				if(s[i].area() < s[j].area()) {
					Shape t = s[i] ;
					s[i] = s[j] ;
					s[j] = s[i] ;
				}
		
		for(int i = 0 ; i < this.n ; i++) 
			s[i].write("src/shape/test3");
		
		
	}
	
	
	
	public static void main(String[] args) throws FileNotFoundException {
		ArrayOfShape a1 = new ArrayOfShape();
		a1.readFile();
		a1.output();
		a1.sortArea();
		
		
	}	
}

class Rectangle

package shape;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class Rectangle  implements Shape{
	
	private double lenth,width ;

	
	public Rectangle() {		
	}
	
	public Rectangle(double l,double w) {
		this.setLenth(l);
		this.setWidth(w);
	}
	
	
	public double getLenth() {
		return lenth;
	}
	public void setLenth(double lenth) {
		this.lenth = lenth;
	}

	

	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width = width;
	}


	@Override
	public void input() {
		Scanner kb = new Scanner(System.in) ;
		System.out.print("Enter the lenth : ");
		this.setLenth(kb.nextDouble());
		System.out.println("Enter the width : ");
		this.setWidth(kb.nextDouble());
	}
	
	
	@Override
	public void output() {
		System.out.println("The lenth : "+this.lenth);
		System.out.println("The width : "+this.width	);
	}
	
	
	@Override
	public double area() {
		return this.getLenth()*this.getWidth() ;
	}
	
	
	@Override
	public double perimeter() {
		return (this.getLenth()+this.getWidth()) / 2 ;
	}
	
	
	@Override
	public void write(String link) throws FileNotFoundException {
		PrintStream p = new PrintStream(link) ;
		p.append("Rectangle " + this.lenth +" "+this.width) ;
	}


}

class Circle

package shape;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class Circle implements Shape{
	
	protected double r ;


	
	public Circle() {
		
	}
	
	public Circle(double r) {
		this.setR(r);
	}
	
	
	public double getR() {
		return r;
	}
	public void setR(double r) {
		this.r = r;
	}
	
	
	@Override
	public void input() {
		Scanner kb = new Scanner(System.in) ;
		System.out.println("Enter r : ");
		this.setR(kb.nextDouble());
	}
	
	
	@Override
	public void output() {
		System.out.println("r : "+this.getR());
	}
	
	
	@Override
	public double area() {
		return 3.14*r*r ;
	}
	
	
	@Override
	public double perimeter() {
		return 2*r*3.14 ;
	}
	
	
	@Override
	public void write(String link) throws FileNotFoundException {
		PrintStream p = new PrintStream(link) ;
		p.append("Circle " + this.r) ;
	}
}

class Shape

package shape;

import java.io.FileNotFoundException;

public interface Shape {
	
	
	public void input() ;
	
	
	
	public void output() ;
	
	
	
	public double area();
	
	
	public  double perimeter();
	
	
	public  void write(String link) throws FileNotFoundException ;
		
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

Cái quan trọng hiện giờ là caia lỗi đấy bạn à. Không biết lỗi đó là gì thì sao bọn mình giúp được chứ.
Mà lớp ArrayOfShape không có hàm dựng (constructor) à?

2 Likes

bạn xem thử hình . array mình nghĩ không cần contructor nên bỏ qua

Trong bài của bạn còn nhiều chỗ bị “thủng” lắm. Nếu “vá” chỗ này, có khi lại “lòi” chỗ khác.

Về ngoại lệ NoSuchElementException:
Bạn dùng phương thức Scanner.next() cùng với điều kiện if - else thì không được ổn.
Thay vì:

Thử nghĩ xem, nếu if không đúng thì qua else if sẽ gọi next() thêm 1 lần nữa. Chết chỗ này.
Bạn phải lưu giá trị cần so sánh để dùng nhiều lần trong if - else.
Và không cần if - else - if lồng nhau đâu.

			String tk = kb.next().toLowerCase();// so sánh có phân biệt HOA - thường nhé.
			if(tk.compareTo("circle") == 0) {
				s[n++] = new Circle(kb.nextDouble()); // gọn luôn
			}
			else if(tk.compareTo("rectangle") == 0) { // else if
					a = new Rectangle(kb.nextDouble(),kb.nextDouble()) ;
					s[n] = a ;
					n++ ;
			}

Xem như đã “vá” 1 chỗ “thủng”.
Khuyên bạn nên đọc từng dòng rồi bóc tách, đọc kiểu này, lỗi như chơi.


Đến lúc nào đó bạn sẽ thấy Regex hữu dụng đến thế nào.

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?