So I'm doing this challenge on code eval. -> https://www.codeeval.com/open_challenges/40/
I already checked my code, and it all makes sense to me. I'm pretty sure I've solved the challenge. Also, note I'm using version Java 1.8 in eclipse and it runs just fine and the challenge looks like it is solved. However, when I submit it to code eval(they use a java 1.7 compiler), it catches the exception and prints out "Unable to read file". Is this do the version of java? It would make sense for it not to work but for methods used like this. I would think 1.7 would be able to compile it the same 1.8 does.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Main {
public static boolean isSelfDescribing(String line)
{
int numberAmount = 0;
int number = 0;
String [] numbers = line.split("");
for(int i = 0; i < numbers.length; i++)
{
numberAmount = 0;
for(int j = 0; j < numbers.length; j++)
{
number = Integer.parseInt(numbers[j]);
if(i == number)
{
numberAmount++;
}
}
if(Integer.parseInt(numbers[i]) == numberAmount)
{
return true;
}
}
return false;
}
public static void main (String[]args)
{
try{
File file = new File(args[0]);
BufferedReader in = new BufferedReader(new FileReader(file));
String line = "";
while((line=in.readLine())!=null) //Reads line by line in a file until there is no text.
{
if(!line.equals(""))
{
if(Main.isSelfDescribing(line)==true)
{
System.out.println(1);
}else
{
System.out.println(0);
}
}
}
}catch (Exception FileNotFoundException)
{
System.out.println("Unable to read file!");
}
}
}
Here is the input text file I used.
230390
177809
42101000
804181
359180
997637
475334
460137
3211000
661334
426633
21200
1210
454310
992933
572425
53338
360538
2020
549553
906210
216325
351732
684176
8552