Method that reads from keyboard(1 value parameter) or from a file(0 value parameter) doesn't work properly at 0 parameter

huangapple go评论75阅读模式
英文:

Method that reads from keyboard(1 value parameter) or from a file(0 value parameter) doesn't work properly at 0 parameter

问题

我想要的方法是从键盘或文件中读取三角形,然后将其添加到ArrayList中。我有两个类:Point类通过2个坐标int c1,int c2定义,Triangle类通过颜色字符串和3个点定义。我有两个方法,一个在Triangle类中,另一个在Point类中:

Triangle类方法:

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类方法:

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();
}

在测试类中:

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);

我的输出:

[Triangle [color=green, A=Point [c1=20, c2=20], B=Point [c1=20, c2=20], C=Point [c1=20, c2=20]]]

我的文件:

green
20 20
40 40
60 60

首先,我想提到第一个方法部分是可以工作的(带有参数值为1的情况可以工作),我在从文件中读取方面遇到问题。你可以从输出中看到问题:"20 20"被赋给了B和C点。

我有一些我尝试要遵守的事项:

  • 如果可能的话,我希望保留方法的结构;
  • 使用Scanner是强制性的(我知道这是一种旧的技术);
  • 我不想更改我的文件,因为如果它是一个真正的记事本,我不认为我会一直在记事本中进行更改。

其他观察:

我认为问题出在Scanner方法上,我尝试过使用while(input1.hasNext())

英文:

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(&quot;File&quot;));
			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(&quot;File&quot;));
			
            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&lt;Triangle&gt; triangleList = new ArrayList&lt;Triangle&gt;();

	for (int i = 0; i &lt; 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()).

答案1

得分: 0

代码部分不提供翻译。以下为您提供的内容的翻译:

你必须使用相同的Scanner实例。在这里,你在循环的每次迭代中创建了一个新的实例,因此每次都会从文件的开头开始。所以我建议在主类中创建一个单独的实例,并将其作为参数传递给你的方法。

Triangle类方法:

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类方法:

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();
}

在测试类中:

Scanner sc = new Scanner(System.in); // 创建Scanner(System.in)的唯一实例 -> 用于键盘输入
Scanner input = new Scanner(new File("File")); // 创建Scanner(new File("File"))的唯一实例 -> 用于文件输入

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);
英文:

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) -&gt; for keybord input
Scanner input = new Scanner(new File(&quot;File&quot;));//the only time you create new instance of Scanner(new File(&quot;File&quot;)) -&gt; for file input

int triangleNumber = sc.nextInt();
List&lt;Triangle&gt; triangleList = new ArrayList&lt;Triangle&gt;();
for (int i = 0; i &lt; triangleNumber; i++) {
	Triangle t = new Triangle();
	Triangle t1 = t.readingTriangle(0,sc,input);
	triangleList.add(t1);
	
}
System.out.println(triangleList);

答案2

得分: 0

但是,如果您坚持不改变参数并保持实际代码的相同结构,您可以使用单例模式的方法,我特别不建议在使用线程时使用该方法。如果您不使用线程,它可以正常工作。

因此,要使用单个Scanner(或任何其他数据类型或类),您可以使用设计模式“Singleton”,该模式用于为整个项目实例化对象的单个实例。

从键盘读取输入的单例定义(一个新类):

import java.util.Scanner;

public class ScannerSingletonKeyboard {
    private static Scanner sc = null;

    private ScannerSingletonKeyboard(){
        sc = new Scanner(System.in); 
    }

    public static Scanner getInstance(){
        if(sc==null)
             new ScannerSingletonKeyboard();
        return sc;
    }
}

从文件读取输入的单例定义(一个新类):

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

public class ScannerSingletonFile {
    private static Scanner sc = null;

    private ScannerSingletonFile(){
        try {
            Scanner sc = new Scanner(new File("File"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Scanner getInstance(){
        if(sc==null)
             new ScannerSingletonFile();
        return sc;
    }
}

Triangle类的方法:

public Triangle readingTriangle(int value) throws Exception {
    if(value == 1) {
        Scanner sc = ScannerSingletonKeyboard.getInstance();
        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) {
        Triangle reserve1 = new Triangle();
        Scanner input = ScannerSingletonFile.getInstance();
        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类的方法:

public Point readingPoint(int value) throws Exception {
    if(value == 1) {
        Scanner sc = ScannerSingletonKeyboard.getInstance();
        Point reserve = new Point();
        reserve.c1 = sc.nextInt();
        reserve.c2 = sc.nextInt();
        return reserve;
    }
    if(value == 0) {
        Scanner input = ScannerSingletonFile.getInstance();
        Point reserve1 = new Point();
        input.next();
        reserve1.c1 = input.nextInt();
        reserve1.c2 = input.nextInt();
        return reserve1;
    }
    return new Point();
}

在测试类中:

Scanner sc = ScannerSingletonKeyboard.getInstance();

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);

每次您想要在任何地方使用扫描仪时,您只需要调用ScannerSingletonKeyboard.getInstance()以获取键盘输入,或调用ScannerSingletonFile.getInstance()以获取文件输入,这将返回您的扫描仪的单个实例。

英文:

But, if you insist to not change your parameters and keep the same structure of your actual code, you can use the method of singleton, which I do not recommend especially if you work with threads. It works fine if you're not using threads.

So to use a single instance of Scanner (or any other datatype or class), you can use the design pattern "Singleton" which consist to instantiate a single instance of your Object for the whole project.

The Singleton definition for input from keyboard (a new class) :

import java.util.Scanner;

public class ScannerSingletonKeyboard {
      private static Scanner sc = null;

    private ScannerSingletonKeyboard(){
        sc = new Scanner(System.in); 

    }

    public static Scanner getInstance(){
        if(sc==null)
             new ScannerSingletonKeyboard();
        return sc;

    }
}

The Singleton definition for input from the file (a new class) :

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

public class ScannerSingletonFile {
      private static Scanner sc = null;

    private ScannerSingletonFile(){
        try {
			Scanner sc = new Scanner(new File(&quot;File&quot;));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

    }

    public static Scanner getInstance(){
        if(sc==null)
             new ScannerSingletonFile();
        return sc;

    }
}

Triangle class method:

public Triangle readingTriangle(int value) throws Exception {
	if(value == 1) {
		Scanner sc = ScannerSingletonKeyboard.getInstance();
		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) {
		Triangle reserve1 =new Triangle();
		Scanner input = ScannerSingletonFile.getInstance();
		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 = ScannerSingletonKeyboard.getInstance();
		Point reserve=new Point();
		reserve.c1 = sc.nextInt();
		reserve.c2 = sc.nextInt();
		return reserve;
	}
	if(value==0) {
		Scanner input = ScannerSingletonFile.getInstance();
		Point reserve1=new Point();
		input.next();
		reserve1.c1=input.nextInt();
		reserve1.c2=input.nextInt();
		
	
		return reserve1;
		
	}
	return new Point();
	
}

In test class:

Scanner sc = ScannerSingletonKeyboard.getInstance();


int triangleNumber = sc.nextInt();
List&lt;Triangle&gt; triangleList = new ArrayList&lt;Triangle&gt;();
for (int i = 0; i &lt; triangleNumber; i++) {
	Triangle t = new Triangle();
	Triangle t1 = t.readingTriangle(0);
	triangleList.add(t1);
	
}
System.out.println(triangleList);

and each time you want to use your scanner anywhere you only have to call ScannerSingletonKeyboard.getInstance() for keyboard input or ScannerSingletonFile.getInstance() for file inout which will return your single instance of scanner

huangapple
  • 本文由 发表于 2020年3月16日 16:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/60702725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定