2014-01-10 4 views
-1

먼저 정수 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]); 
} 
} 

예 : 프로그램 예외에

4 
2 4 
5 7 
8 9 
1 0 
+0

왜 '스캐너'와 'BufferedReader'둘 다 사용하고 있습니까? 둘 중 하나를 사용하십시오. –

+1

그래, NullPointerException을 어느 라인에서 얻을 수 있는지 알려줄 수 있습니까? – Ajeesh

+0

@RohitJain JUST TO PRACTICE .. 그리고 어떤 숙제도하지 말아주세요. – user119249

답변

1

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

OUTPUT

.....이 시도 P[i].x=Integer.parseInt(s1[0]);

에서 발생

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

이 0 당신이하고 싶은 말의 일 예이다. 귀하의 질문과 다음 의견에서 판단 해 보겠다. 필자는이 작업을 계속하기 전에 Java에 대한 몇 가지 기본 자습서를 진행할 것을 강력히 권고합니다. 왜냐하면 실제로 무슨 일이 일어나는 지 이해하지 못한다면, 당신에게이 대답을주는 것은 결국 아무 도움이되지 않을 것입니다.

관련 문제