Andreas Rydberg

Home

Learning Java, again - part 1

Andreas — Fri, 2011-12-09 21:43

I've been working for my new company Appland as web developer. Professionally I've mostly worked with PHP and Javascript, not counting HTML or CSS as programming, so I thought that would start to learn Java, again, since we are developing an Android app.

Coming from PHP, I guess I'm bit spoiled, but I was prepered to get make hands dirty. Since I'm somewhat to dedicated to learn Java and make Android apps, I think this post will be the first part of a series of posts.

Splitting strings

In PHP I would do something like this

<?php
$input
= 'key=value';
$output = explode('=', $input);

echo
$output[0]; // prints "key"
echo $output[1]; // prints "value"
?>

But if the value was missing I would expect this:

<?php
$input
= 'key=';
$output = explode('=', $input);

echo
$output[0]; // prints "key"
echo $output[1]; // prints "" (an empty string)
?>

I assumed that above behaviour would be similar in Java, or possibly that output would be null instead of empty string. Consider this Java code:

String input = "key=value";
String[] output = input.split("=");
Log.d("length", output.length + ""); // prints "2" to log
Log.d("debug", output[0]); // prints "key" to log
Log.d("debug", output[1]); // prints "value" to log

Above example works like the first PHP example. output.length returns the integer 2. Then consider this java code:

String input = "key=";
String[] output = input.split("=");
Log.d("length", output.length + ""); // prints "1" to log
Log.d("debug", output[0]); // prints "key" to log
Log.d("debug", output[1]); // throws IndexOutOfBoundsException.

Since Java only return an array with only one item in it. output[1] is therefore out of bounds. To get similar result as PHP, with exception handling, like this:

String input = "key=";
String[] output = input.split("=");
Log.d("length", output.length + ""); // prints "1" to log
Log.d("debug", output[0]); // prints "key" to log
try {
    Log.d("value", output[1]);
}
catch (IndexOutOfBoundsException e) {
    Log.d("value", "(empty)"); // Since Lod.d() doesn't print empty strings like "" I had to do this.
}
catch (Exception e) {
    // capture other exceptions.
}

Or you could just use the general capture:

String input = "key=";
String[] output = input.split("=");
Log.d("length", output.length + ""); // prints "1" to log
Log.d("debug", output[0]); // prints "key" to log
try {
    Log.d("value", output[1]);
}
catch (Exception e) {
    Log.d("value", "(empty)"); // Since Lod.d() doesn't print empty strings like "" I had to do this.
}

Have fun!

  • Java

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li>
  • Lines and paragraphs break automatically.

More information about formatting options

Blog posts

2011.12.16
Appland launched
2011.12.09
Learning Java, again - part 1
2011.11.20
Installing Lighttpd on Ubuntu 11.10
2011.03.15
New job and Aegir
2010.11.16
Acquia Drupal stack installer
  •  
  • 1 of 3
  • ››
Syndicate content

Powered by Drupal, an open source content management system