2 / 4
May 2016

I keep getting runtime error for my java code. Please help.

import java.util.*;
import java.io.*;

public class main {

public static void main(String[] args) throws java.lang.Exception {
	BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	StringBuilder builder = new StringBuilder();
	int numCases = Integer.parseInt(reader.readLine().split(" ")[0]);
	reader.readLine();
	for (int i = 1; i <= numCases; i++){
		builder.append("Case " + i + ": ");
		int numPoints = Integer.parseInt(reader.readLine().split(" ")[0]);
		reader.readLine();
		float big = 0;
		int add = 0;
		for (int x = 1; x <= numPoints; x++){
			String temp = reader.readLine();
			reader.readLine(); 
			float a = Float.parseFloat(temp.split(" ")[0]);
			float b = Float.parseFloat(temp.split(" ")[1]);
			if (a*a + b*b > big){
				big = a*a + b*b;
				add = x;
			}
		}
		builder.append(add);
		builder.append(System.getProperty("line.separator"));
		builder.append(System.getProperty("line.separator"));
	}
	System.out.print(builder.toString());
	//get numcases
	//for numcases, add case # to builder, get numpoints
	//for numpoints find largest squares, add largest squares in builder
}

}

  • created

    May '16
  • last reply

    May '16
  • 3

    replies

  • 1.1k

    views

  • 2

    users

  • 1

    link

This problem has an issue with the input format. Although I don't know how JAVA handles this, it might be the root of your problem. For python programming language, it was discussed here.

I'm not very well versed in python but it looks like instead of reading a line at a time, you just read the entire input and parsed after that?

If that isn't what you did could you explain, and why it works. Thanks again.

Yes, that's what I did. It is a simple workaround to read input if there are line breaks or other special whitespace characters in the input where you don't expect them.