[Java]: Square Pasture (1st Problem) Debugging Help

Square Pasture (Bronze)
Link

Java

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

 public class SquarePasture {
 
	public static void main(String[] args) throws IOException {
 		
 		Kattio io = new Kattio("square");
		int x1 = io.nextInt(), y1 = io.nextInt(), x2 = io.nextInt(), y2 = io.nextInt();
 		int x3 = io.nextInt(), y3 = io.nextInt(), x4 = io.nextInt(), y4 = io.nextInt();
 		//find the sides of the smallest rectangle covering both pastures
		int left = Math.min(x1, x3);
 		int right = Math.max(x2, x4);
 		
 		int bottom = Math.min(y1, y3);
 		int top = Math.max(y2, y4);
 		
 		int side = Math.max(right - left, top - bottom);
 		
		io.print(side * side);
 		io.close();
 		
 	}
 	
 	static class Kattio extends PrintWriter {
 		private BufferedReader r;
	private StringTokenizer st;
 	
 		// standard input
 		public Kattio() { this(System.in, System.out); }
 		public Kattio(InputStream i, OutputStream o) {
 			super(o);
 			r = new BufferedReader(new InputStreamReader(i));
		}
 		// USACO-style file input
 		public Kattio(String problemName) throws IOException {
 			super(new FileWriter(problemName + ".out"));
 			r = new BufferedReader(new FileReader(problemName + ".in"));
 		}
 	
 		// returns null if no more input
 		public String next() {
 			try {
 				while (st == null || !st.hasMoreTokens())
 					st = new StringTokenizer(r.readLine());
 				return st.nextToken();
 			} catch (Exception e) { }
 			return null;
	}
	
 		public int nextInt() { return Integer.parseInt(next()); }
 		public double nextDouble() { return Double.parseDouble(next()); }
 		public long nextLong() { return Long.parseLong(next()); }
 	}
 
 }

When I tried getting it graded, it gives me an error of:

Exception in thread “main” java.io.FileNotFoundException: square.in (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
at java.base/java.io.FileInputStream.(FileInputStream.java:153)
at java.base/java.io.FileInputStream.(FileInputStream.java:108)
at java.base/java.io.FileReader.(FileReader.java:60)
at usaco.SquarePasture$Kattio.(SquarePasture.java:47)
at usaco.SquarePasture.main(SquarePasture.java:17)

Checklist

See How to ask for help on a problem for more information.

  • Include a link to the problem
  • Format your post (especially code blocks)
  • Include what you’ve tried so far
  • Add comments to your code
  • Read the debugging module
1 Like

I just submitted this and it passed … (have you tried resubmitting it? maybe USACO died temporarily or smth)

Btw your code is not properly formatted.

(especially language-specific code blocks such as the following).

Hello Benq,
Thanks for your reply. When I submit it, it gives me 10 exclamation marks in red boxes. When I click on my submission details, it gives me a “RTE” which I’m assuming means Runtime Error.

Kattio io = new Kattio(“square”);

I think that this is the reason why it is marked wrong.

Can you share a screenshot of the result? I’m pretty sure there no way to see 10 exclamation marks … you need to at least pass the sample to see your result for the other cases.

Yes. One moment

You should have mentioned that you are not submitting directly through the USACO website. Did you try changing that line to

Kattio io = new Kattio();

so that it uses standard input/output instead of file input/output?

Oh. I didn’t know that you could submit directly to the USACO website. (Could you give me the link to that please :rofl: ) Yes! It worked tysm!!!

Scroll down to the bottom of the link you sent.

Ok ty I will close this now