0

I'm working through the problems on code chef. I'm stuck with a problem and all its says is that I have the wrong answer. I want to test my program to see its output but it reads input from a text file and I can't figure out how to do that with eclipse, my code is below:

import java.io.*;
class Holes {

public static void main(String[] args) throws IOException{
    // TODO Auto-generated method stub
    BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
    int testCases = Integer.parseInt(r.readLine());

    for (int i =0; i<testCases; i++)
    {
        int holes = 0;
        String s = r.readLine();
        for (int j= 0; j< s.length(); j++)
        {
            char c = s.charAt(j);
            if (c == 'B')
                holes += 2;
            else if (c== 'A' || c== 'D' ||c== 'O' ||c== 'P' ||c== 'Q' ||c== 'R' )
            {
                holes +=1;
            }
            System.out.println(holes);
        }
    }   
}

}
AndroidEx
  • 14,084
  • 9
  • 51
  • 50
Jerry Murphy
  • 318
  • 1
  • 5
  • 20
  • There is a similar question from 2008 that basically says the best way is to use the command line but I was hoping that eclipse had added the feature since then. – Jerry Murphy May 14 '15 at 22:21
  • Have a look at filereader and scanner for file i/o. http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file – Kevin May 14 '15 at 22:23
  • im using System.in not console.in?? – Jerry Murphy May 14 '15 at 22:29

2 Answers2

1

add folder to your eclipse project in that folder add your input file and then read it using BufferReader as follows BufferedReader br = null;

try {

    String sCurrentLine;

    br = new BufferedReader(new FileReader("yourFolder/theinputfile.txt"));

    while ((sCurrentLine = br.readLine()) != null) {
        System.out.println(sCurrentLine);
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (br != null)br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

this is one way the other way is to pass the path as argument to your program as it shown bellow

try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader(args[0]));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

how to do that when your run the application do run configuration and there you will find args you can add what ever path in it for example c:\myinput.txt hopefully this help

Alaa Abuzaghleh
  • 1,003
  • 6
  • 11
  • so using your first example would I change: BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); to: br = new BufferedReader(new FileReader("yourFolder/theinputfile.txt")); changing the directory of course. – Jerry Murphy May 14 '15 at 22:35
1
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

    public static void main(String[] args) {

        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("C:\\testing.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}
Damis Berzovitis
  • 205
  • 1
  • 3
  • 11