-3

Here is the problem I want to solve https://www.codeeval.com/open_challenges/100/

and here is my solution:

public static void main(String[] args) {
    // TODO code application
    File file = null;
    try{
    FileReader f = new FileReader("C:\\Users\\ranaa\\Desktop\\merna1.txt");
    BufferedReader br = new BufferedReader(f);
    String getContent;
    while((getContent=br.readLine())!=null){
        int content = Integer.parseInt(getContent);
        System.out.println(content);
        if(content %2==0)
            System.out.println("1");
        else
            System.out.println("0");
    }
    }catch(Exception e){
        System.out.println(e);
    }
}

no output appears to me nor exception.

Can anybody help me?

tshepang
  • 10,772
  • 21
  • 84
  • 127

1 Answers1

1

Before seeing my code

First, it is good to put whatever is closeable in try catch block with resources so there is no need to be worried about closing it at end because closing connection is done implicitly. read this: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Second, How to use BuffeRreader just take a look at this

How to use Buffered Reader in Java

Third, How to use Scanner just take a look at this

How can I read input from the console using the Scanner class in Java?

Code:

    String filePath = "C:\\Users\\KICK\\Desktop\\number.txt";
    File file = new File(filePath);
    try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
        String line;
        while ((line = reader.readLine()) != null) {
           int current = Integer.parseInt(line);

            if( current % 2 == 0)
                System.out.println("1");
            else
                System.out.println("0");
        }
    } catch (Exceptio e) {
        System.out.println(e);
    }

or you can use Scanner class

Code:

String filePath = "C:\\Users\\KICK\\Desktop\\number.txt";
        File file = new File(filePath);
        try(Scanner input = new Scanner(file)){
           while(input.hasNext()){
                int current = Integer.parseInt(input.next());

                if( current % 2 == 0)
                    System.out.println("1");
                else
                    System.out.println("0");
                     }
       }catch(Exception e){
           System.out.println(e);
       }

output:

0
0
1

number.txt content :

701
4123
2936
Community
  • 1
  • 1
Kick Buttowski
  • 6,371
  • 12
  • 34
  • 54