check my code. codechef is giving it wrong.

here is my code for EASY problem—Life, the Universe, and Everything

import java.io.BufferedReader;
import java.io.InpputStreamReader;
import java.util.ArrayList;

class Life
{
	ArrayList<Integer> numbers=new ArrayList<Integer>();
	int number;

	public void readInput(BufferedReader br) throws IOException
	{
		while((number=Integer.parseInt(br.readLine()))<99)
		{
			if(number<=42)
			{
				numbers.add(number);
			}
		}
		
		result();
	}

	public void result()
	{
		for(int n:numbers)
		{
			System.out.println(n);
		}
	}
}

class TestLife
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		Life l=new Life();
		l.readInput(br);
	}
}

Here is the problem/s :

1)The statement while((number=Integer.parseInt(br.readLine()))<99) is unnecessary. Codechef makes sure that the input is in the format given so you dont need to check on your own. Besides the number 99 is a valid input so it it should be <= 99.

  1. You are doing if(number<=42) then add it to the array. you have misinterpreted the question . It asks you to print result when number is 42.So condition should be something like if number is not 42 then add to array. If number is 42 then print result and exit.

Here is accepted version of your code

1 Like

thanks. i got it