Posts Tagged ‘eclipse’

Java: Determine if String is a URL/URI or file

In the spirit of making a more polymorphous app, you may need to pull off this trick, as I did in a recent assignment at Berkeley. I compiled a few different ways of getting the job done:
public boolean isLocalFile(String file) {
     try {
         new URL(file);
         return false;
     } catch (MalformedURLException e) {
         return true;
there’s also a util for this in Android’s toolkit (not worth grabbing unless you’re specifically writing for Android, though).
another semi-related thing;
  1. Make sure the filename is correct (proper capitalization, matching extension etc – as already suggested).
  2. Use the Class.getResource method to locate your file in the classpath – don’t rely on the current directory:
    URL url = insertionSort.class.getResource("10_Random");
    File file = new File(url.toURI());
  3. Specify the absolute file path via command-line arguments:
    File file = new File(args[0]);

In Eclipse:

  1. Choose “Run configurations”
  2. Go to the “Arguments” tab
  3. Put your “c:/Users/HackR/somewhere/10_myjava.txt.or.something” into the “Program arguments” section


Python: Notes on Indentation

In Python the interpreter identifies blocks of code based on indentation.

By way of example, in an if-loop if the next line is indented then it means that this indented block is within the loop. You can use either spaces or tabs, e.g. you can not mix both, to quote PEP 8 – Style Guide for Python Code:

Python 3 disallows mixing the use of tabs and spaces for indentation.
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

The standard method is to use 4 spaces for indentation, which also happens to be the necessary number of spaces to designate a codeblock on Stackoverflow.

Fortunately, if you’re using the PyDev plugin for Eclipse or many other popular IDE’s, then you can press tab in the text editor and the IDE will automatically translate that to 4 spaces.


Stackoverflow Solutions

Just started! Have not answered any questions.