Results 1 to 5 of 5
  1. #1
    bluebarca is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default BufferdReader problem

    hey everybody. strangely i've been facing a problem with java's BufferedReader; i don't know why it sometimes skips a line but most of the time when an input is entered it doesn't point to the next line but stays put at this line for 1 more reading or so.


    here's the code, which functions as follows:
    1- on the 1st line, enter the number of lines that will follow
    2- on the next lines enter numbers each at a line

    the program should return the factorial of the entered numbers
    but the problem is as follows; if i enter
    2
    4
    5
    then 2 = number of lines to follow and 4 & 5 are the numbers that the factorial will be calculated for and printed on the screen. instead the program doesn't do that and still reads lines after the 2nd line ?

    Can you please tell me why the program does that ?




    Java Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    
    public class Main {
    
        static BufferedReader in  = new BufferedReader(new InputStreamReader(System.in));
        static int number_lines = 0;
       static int [] input;
       static int [] output;
    
    
        public static void getFactorial(){
            try
            {
    
            number_lines = Integer.parseInt(in.readLine());
            }
            catch(Exception e){
    
            }
            input = new int[number_lines];
            output = new int[number_lines];
    
            init_array(input);
            init_array(output);
    
    
    
            for(int i = 0; i<number_lines;i++){
    
                try{
    
                    input[i] = Integer.parseInt(in.readLine());
    
    
                }
    
                catch(Exception e){
    
    
    
                }
    
            }
    
    
            for(int j = 0;j<number_lines;j++){
                output[j] = calculate_factorial(input[j]);
    
    
    
            }
    
            for(int k = 0; k<number_lines;k++){
    
                System.out.println(output[k]);
    
            }
    
    
    
    
    
        }
    
        public static void init_array(int [] a){
    
    
            for(int i = 0;i<a.length;i++){
    
                a[i] = 0;
    
            }
    
        }
    
    
        private static int calculate_factorial(int input){
    
            int result = 1;
            if(input == 0){
                return 1;
            }
            else{
            for(int i = input;i>0;i++){
                result = result * i;
    
            }
            return result;
        }
    
        }
    
    
    
        public static void main(String[] args) {
            getFactorial();
        }
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You just keep adding values to an array, and I cannot is any inputs handling in user level. Can you tell me what's logic you used to build this code?

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Don't do
    Java Code:
    catch(Exception e){
     }
    At least put an e.printStackTrace() to see what exceptions (if any) are being thrown.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    P.S. that init_array method is useless unless you want to initialise with a value other than 0. int[]'s, per default, have value 0 in all elements.

    How do you know it is still waiting on input?

    Change
    Java Code:
    input[i] = Integer.parseInt(in.readLine());
    to
    Java Code:
    System.out.println("--Reading next number.--");
    input[i] = Integer.parseInt(in.readLine());
    System.out.println("--Read " + input[i] + ".--");
    And then also add a few other similar SOP in other places and run again trying to diagnose what is actually happening.

    And, BTW, add something to that Exception block so that you at least know that something went wrong. Never just ignore Exceptions like that. At least log them.

  5. #5
    spamsickle is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    Why do you think the program is reading lines after the first line? What output are you seeing that leads you to that conclusion?

    I think I see a problem in this line:
    Java Code:
    for(int i = input;i>0;i++){
    Surely, that should be i-- instead. Otherwise, this looks like an infinite loop.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •