I want my method to read triangles from keyboard or from file and then be added to ArrayList
.. I have 2 classes: Point defined by 2 coordinates:int c1,int c2
, and Triangle class defined by a color string and 3 points. I have 2 methods, one in Triangle
class and one in Point
class:
Triangle class method:
public Triangle readingTriangle(int value) throws Exception {
if(value == 1) {
Scanner sc = new Scanner(System.in);
Triangle reserve =new Triangle();
reserve.color = sc.next();
reserve.setA(reserve.A.readingPoint(1));
reserve.setB(reserve.B.readingPoint(1));
reserve.setC(reserve.C.readingPoint(1));
return reserve;
}
if(value == 0) {
Scanner input = new Scanner(new File("File"));
Triangle reserve1 =new Triangle();
reserve1.color=input.next();
reserve1.setA(reserve1.A.readingPoint(0));
reserve1.setB(reserve1.B.readingPoint(0));
reserve1.setC(reserve1.C.readingPoint(0));
return reserve1;
}
return new Triangle();
}
Point class method:
public Point readingPoint(int value) throws Exception {
if(value==1) {
Scanner sc = new Scanner(System.in);
Point reserve=new Point();
reserve.c1 = sc.nextInt();
reserve.c2 = sc.nextInt();
return reserve;
}
if(value==0) {
Point reserve1=new Point();
Scanner input1 = new Scanner(new File("File"));
input1.next();
reserve1.c1=input1.nextInt();
reserve1.c2=input1.nextInt();
return reserve1;
}
return new Point();
}
In test class:
Scanner sc = new Scanner(System.in);
int triangleNumber = sc.nextInt();
List<Triangle> triangleList = new ArrayList<Triangle>();
for (int i = 0; i < triangleNumber; i++) {
Triangle t = new Triangle();
Triangle t1 = t.readingTriangle(0);
triangleList.add(t1);
}
System.out.println(triangleList);
My output:
[Triangle [color=green, A=Point [c1=20, c2=20], B=Point [c1=20, c2=20], C=Point [c1=20, c2=20]]]
My File:
green
20 20
40 40
60 60
First, I want to mention that first piece of method is working (1 value parameter is working), I have problems with reading from file. You can see the problem from output:"20 20" is given for B and C points.
I have some things that I try to respect:
I want my structures of methods to be, if possible, respected;
Using Scanner is mandatory(I know it's old technique);
I don't want to change my file, because if it was a real notepad, I don't think I will stay to change things in notepad.
Other observations:
I think the problem is at scanner methods, I tried with while(input1.hasNext()
).
you have to use the same instance of Scanner
. here you are creating a new instance in each iteration in the loop, so each time it will start from the beginning of the file. So I suggest to create a single instance in main class and pass it as parameter in your methods.
Triangle class method:
public Triangle readingTriangle(int value, Scanner sc,Scanner input) throws Exception {
if(value == 1) {
Triangle reserve =new Triangle();
reserve.color = sc.next();
reserve.setA(reserve.A.readingPoint(1,sc,input));
reserve.setB(reserve.B.readingPoint(1,sc,input));
reserve.setC(reserve.C.readingPoint(1,sc,input));
return reserve;
}
if(value == 0) {
Triangle reserve1 =new Triangle();
reserve1.color=input.next();
reserve1.setA(reserve1.A.readingPoint(0,sc,input));
reserve1.setB(reserve1.B.readingPoint(0,sc,input));
reserve1.setC(reserve1.C.readingPoint(0,sc,input));
return reserve1;
}
return new Triangle();
}
Point class method:
public Point readingPoint(int value, Scanner sc,Scanner input) throws Exception {
if(value==1) {
Point reserve=new Point();
reserve.c1 = sc.nextInt();
reserve.c2 = sc.nextInt();
return reserve;
}
if(value==0) {
Point reserve1=new Point();
reserve1.c1=input.nextInt();
reserve1.c2=input.nextInt();
return reserve1;
}
return new Point();
}
In test class:
Scanner sc = new Scanner(System.in); //the only time you create new instance of Scanner(System.in) -> for keybord input
Scanner input = new Scanner(new File("File"));//the only time you create new instance of Scanner(new File("File")) -> for file input
int triangleNumber = sc.nextInt();
List<Triangle> triangleList = new ArrayList<Triangle>();
for (int i = 0; i < triangleNumber; i++) {
Triangle t = new Triangle();
Triangle t1 = t.readingTriangle(0,sc,input);
triangleList.add(t1);
}
System.out.println(triangleList);