-2

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
Kay
  • 103
  • 1
  • 8
  • 4
    How about you switch the project runtime to Java 7 and see it yourself? – MightyPork Jan 03 '15 at 20:08
  • 11
    I suggest you try printing out the exception instead of *assuming* that the problem is that the file can't be read. (I'd also strongly suggest naming the exception variable `ex` or something, instead of giving it a name which is also the name of a class... that's just confusing.) – Jon Skeet Jan 03 '15 at 20:09

1 Answers1

4

You are partially right. Your code fails on java 7 but not because file is not found. Your catch block:

}catch (Exception FileNotFoundException)

will catch not only FileNotFoundException but any Exception. I suggest removing any catch blocks from your code and declare main to throw Exception:

public static void main (String[]args) throws Exception

This way your program will be much easier to debug.

The real exception is most likely java.lang.NumberFormatException: For input string: "" which is caused by different behavior of String.split in java7/8. See this question for more details. line.split("(?!^)") should work the same on both versions.

Community
  • 1
  • 1
Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65