-1

Hello I am beginning to do some questions on dmoj link here: https://dmoj.ca/problem/ccc03j2

I have written the code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int x = 0;
        int y = 0;
        boolean loop = true;
        StringBuilder result = new StringBuilder();
        while(loop){
            int pictures = Integer.parseInt(br.readLine());
            if(pictures == 0){
                break;
            }
            int max = (int) Math.ceil(Math.sqrt(pictures));
            int min = (int) Math.floor(Math.sqrt(pictures));
            if(pictures % max == 0){
                x = max;
                y = pictures / max;
            }
            else if(pictures % min == 0){
                x = min;
                y = pictures / min;
            }
            int perimeter = ((x+y) / 2) * 4;
            if(x < y){
                result.append("Minimum perimeter is " +  perimeter + " with dimensions " + x  + " x " + y);
            }else if(y < x){
                result.append("Minimum perimeter is " +  perimeter + " with dimensions " + y  + " x " + x);
            }else{
                result.append("Minimum perimeter is " +  perimeter + " with dimensions " + y  + " x " + x);
            }
            System.out.println(result);
            result.setLength(0);
        }
    }
}

but when it prints to the console it results in this:

100
15
195
0Minimum perimeter is 40 with dimensions 10 x 10 //Why is my output on the same line as my input?
Minimum perimeter is 16 with dimensions 3 x 5
Minimum perimeter is 56 with dimensions 13 x 15

Please help, I am a beginner and im having trouble with the basic input output thanks!

Count T
  • 15
  • 3
  • 2
    Where do the 4 numbers come from? Are you perhaps *pasting* them in, without the final line terminator, so all 4 numbers are in the console window *before* the Java program completes the first `br.readLine()` call? And the program is then sitting there waiting for the 4th line of input, so when you press Enter *after* the program has already printing the 3 lines of output, the program finally ends? If so, make sure to have the final line terminator in the clipboard, if you don't want the console to look like that. Or enter the number manually, so it'll print the output *as* input is entered. – Andreas Sep 03 '20 at 16:16
  • Yes thank you it was because when I pasted I didnt paste an extra line under the 0. I ran the code in the dmoj judge and it said I completed the problem but it gave me accepted with 40/100. You have to get 100/100 to get it completed so im really confused. – Count T Sep 03 '20 at 21:50
  • wait nvm my code is just wrong lol – Count T Sep 03 '20 at 22:46

1 Answers1

1

I'm assuming you are copying/pasting your input, since otherwise it would look like this:

100
Minimum perimeter is 40 with dimensions 10 x 10
15
Minimum perimeter is 16 with dimensions 3 x 5
195
Minimum perimeter is 56 with dimensions 13 x 15
0

Process finished with exit code 0

What happens is that, since your program outputs after each line read, it will start processing once the input is pasted, one entry for each line.
Include a line feed after 0 and it will prevent them from being on the same line.

100
15
195
0
Minimum perimeter is 40 with dimensions 10 x 10
Minimum perimeter is 16 with dimensions 3 x 5
Minimum perimeter is 56 with dimensions 13 x 15

Process finished with exit code 0
Gryphon
  • 384
  • 1
  • 9
  • Yea thanks broski it was cuz I didn't input an extra space after the zero – Count T Sep 03 '20 at 21:39
  • How do I implement a way so they don't need to input a new line after the zero – Count T Sep 03 '20 at 21:41
  • Oh wait what I was confused because when I entered my code into the judge it said AC 40/100. I thought it was wrong answer because it was not 100/100 does that mean my answer is correct or no. I check my finished problems and it included it but its not 100/100 – Count T Sep 03 '20 at 21:45
  • For not having to hit enter (not sure how that would work with your other elements, but could make it messy) [How to read a single char from the console in Java (as the user types it)?](https://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it) – Gryphon Sep 03 '20 at 23:22
  • Nevermind I got the code accepted on the judge 100/100. I got 40/100 because my logic was wrong not because of my output. Anyways thanks a lot for making me understand that I need to require an extra enter space key when I copy paste the input. Also thanks for not roasting me for being a noob haha :) cuz my friend told me that using stack overflow to ask questions usually gets a toxic response. – Count T Sep 04 '20 at 15:26
  • No worries at all - glad it helped. And yea, this place doesn't have the greatest rep for stuff like that =) Working to change that, at least a little bit! – Gryphon Sep 04 '20 at 17:27