Java 以空格分隔的读取输入

Java 以空格分隔的读取输入,java,nullpointerexception,Java,Nullpointerexception,我想先取一个整数N,后跟N行,其中每行有两个坐标x和y。(用空格隔开)。我尝试过这样做,但它给出了NullPointerException class solution{ class Point{ int x; int y; } public static void main(String[] args) throws IOException { int N; Scanner in = new Scanner(System.in); BufferedReader inp = new Buff

我想先取一个整数N,后跟N行,其中每行有两个坐标x和y。(用空格隔开)。我尝试过这样做,但它给出了
NullPointerException

class solution{

class Point{
int x;
int y;
} 
public static void main(String[] args) throws IOException {
int N;
Scanner in = new Scanner(System.in);
BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
N=Integer.parseInt(in.next());
Point[] P = new Point[N];
for(i=0;i<N;i++){
String[] s1 = inp.readLine().split(" ");
P[i].x=Integer.parseInt(s1[0]);
P[i].y=Integer.parseInt(s1[1]);
} 
}

在您的程序中,异常发生在
p[i].x=Integer.parseInt(s1[0])

试试这个

public class solution{

    Set<Point> s=new LinkedHashSet<>();
    class Point{
    int x;
    int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    } 

    public static void main(String[] args) throws IOException {
    solution s=new solution();
    s.setValues();
    s.printValues();
    }

    private void setValues() {
    int N;
    System.out.println("Enter Limit:");
    Scanner in = new Scanner(System.in);
    N=Integer.parseInt(in.nextLine());
    System.out.println("Enter "+N+" numbers with space:");
    for(int i=0;i<N;i++){
    String[] s1 = in.nextLine().split(" ");
    s.add(new Point(Integer.parseInt(s1[0]),Integer.parseInt(s1[1])));
    } 
    }

    private void printValues() {
        System.out.println("Enter numbers are:");
        for (Iterator<Point> it = s.iterator(); it.hasNext();) {
            Point s = it.next();
            System.out.println("x="+s.getX()+"  and y="+s.getY());
        }
    }
}
导入java.awt.Point;
导入java.io.IOException;
导入java.util.Scanner;
公共类解决方案{
公共静态void main(最终字符串[]args)引发IOException{
整数大小;
扫描仪输入=新扫描仪(系统输入);
size=Integer.parseInt(in.nextLine());
点[]点=新点[大小];
对于(int i=0;i

这是一个你说你想做的工作的例子。从您的问题和下面的评论来看,我强烈建议您在继续本教程之前,先学习一些关于Java的基本教程。因为除非你真的了解在哪里发生了什么,否则给出这个答案最终将毫无用处。

你为什么要同时使用
Scanner
BufferedReader
?使用它们中的任何一个。好的,你能告诉我们你在哪一行得到NullPointerException吗?@RohitJain只是为了练习..请不要这样homework@AjeeshP[i].x=Integer.parseInt(s1[0]);在这一行中,p[i]为null,因为您从未初始化过它。因此P[i].x抛出NPE。创建数组不会自动填充其字段。
public class solution{

    Set<Point> s=new LinkedHashSet<>();
    class Point{
    int x;
    int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    } 

    public static void main(String[] args) throws IOException {
    solution s=new solution();
    s.setValues();
    s.printValues();
    }

    private void setValues() {
    int N;
    System.out.println("Enter Limit:");
    Scanner in = new Scanner(System.in);
    N=Integer.parseInt(in.nextLine());
    System.out.println("Enter "+N+" numbers with space:");
    for(int i=0;i<N;i++){
    String[] s1 = in.nextLine().split(" ");
    s.add(new Point(Integer.parseInt(s1[0]),Integer.parseInt(s1[1])));
    } 
    }

    private void printValues() {
        System.out.println("Enter numbers are:");
        for (Iterator<Point> it = s.iterator(); it.hasNext();) {
            Point s = it.next();
            System.out.println("x="+s.getX()+"  and y="+s.getY());
        }
    }
}
Enter Limit:
4
Enter 4 numbers with space:
4 8
1 9
7 8
2 3
Enter numbers are:
x=4  and y=8
x=1  and y=9
x=7  and y=8
x=2  and y=3
import java.awt.Point;
import java.io.IOException;
import java.util.Scanner;


public class Solution {

    public static void main(final String[] args) throws IOException {
        int size;
        Scanner in = new Scanner(System.in);
        size = Integer.parseInt(in.nextLine());
        Point[] points = new Point[size];
        for (int i = 0; i < size; i++) {
            String[] pointInput = in.nextLine().split(" ");
            points[i] = new Point();
            points[i].x = Integer.parseInt(pointInput[0]);
            points[i].y = Integer.parseInt(pointInput[1]);
        }
        in.close();
        for (Point point : points) {
            System.out.println("This is a point: " + point.x + ", " + point.y);
        }
    }
}