Reputation: 348
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);
}
}
}
}
Upvotes: 0
Views: 1533
Reputation: 215
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();
}
}
}
}
Upvotes: 1
Reputation: 1009
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
Upvotes: 1