Ryan Rampersad

Thoughts, Ideas & Opinions


  • Home
  • About
  • Contact
  • Archives

Java: syntax error on token “else”, delete this token

By Ryan on February 22, 2012

Before school ended, I donated some of my time to helping out the Intro to Java kids. In that class, I realized that when you tell beginning programmers “End each line with a semi-colon,” they think you really mean it. As in, every single line with actual code (e.g. not brackets) must end with a semi-colon. Well, it’s sad news but that is not true. Not every line needs a semi-colon.

Here’s an example. Look at this code. I dare you even to try running it. You’ll end up with an error.

public static void main(String[] args) {
	int magic = 10;
	if (magic < 20);
	{
		System.out.println("weak magic");
	}
	else
	{
		System.out.println("strong magic");
	}
}

The error is of course, uncommunicative of the true problem. Did you spot the error yet?

syntax error on token “else”, delete this token

The problem here isn’t the else or even the if. No, the problem is a semi-colon. Seriously. Take a look at the if-statement expression. The oddity comes about when you actually see the semi-colon attached to the end of it. And because the if-statement ended there, the else has no preceding pair so it has no choice but to error out.

This example is actually a clear cut case of putting a semi-colon after an if-statement expression. Imagine you didn’t have an error to remind you what you just did. You might just have a plain statement and proper following brackets.

public static void main(String[] args) {
	int magic = 10;
	if (magic < 20); {
		System.out.println("weak magic");
	}
}

Without the else to throw an error message up, how easy would this be to spot? It’s tricky. What if your if-case was a rare edge case condition that you were hard pressed to demonstrate in testing but you theoretically knew it existed? You’d almost never know this wasn’t work until your nuclear reactor began to melt down.

The moral of story: kids will do what you tell them and mind you semi-colons at the end of constructs.

Posted in Errors, Java | Tagged error, Java, syntax, token | Leave a response

At The Nexus & The Universe – Updates

By Ryan on February 22, 2012

We’re rolling in new episodes. This week, two fantastic podcasts are ready for your listening pleasure.

First, we have At The Nexus #14: Super Ultra Mega Bonus Points with yours truly and of course Matthew Petschl.

This week Matthew Petschl, fire-wielding extraordinaire, and Ryan Rampersad reveal the third annual Physics Day with dry ice and cannons. In the news, we cover everything Apple: Mountain Lion, iPad 3 and Apple TV. After that, we touch upon two failed announcements, Amazon Prime memberships, solid state limits, improved MySQL performance, the horribly designed Windows 8 logo, and more!

Then we have The Universe #3: Science for Americans, with Sam Ebertz, myself and special quiet guest, Matthew Petschl. (Do you see a trend where there are only three people actually on the network?)

This week, Sam Ebertz and Ryan Rampersad have specual guest, Matthew Petschl, the pyro-technician extraordinaire and discuss continued NASA news, DNA robots, Fermilab sigma-results, the mysterious hypernucleus, ice melt, a fantastic discussion on exo-planet discoveries leading to verifiable inputs for Drake’s equation and more!

Posted in Podcast | Tagged at the nexus, podcast, the universe | Leave a response

wget: background download

By Ryan on February 17, 2012

Here I am at the University of Minnesota and I find out that I need to download this huge 3.4 gigabyte file. I don’t need it now, but I know I’ll need it eventually. What do I do? I ssh to my server at home and start up wget. But in the past, I realized, wget will fail to function properly if I close the terminal, and since I have class, that would happen quite soon. And this download will take hours.

wget is smart enough though to offer a background option that will allow it to be decoupled from the terminal process that started it.

Startup:
  -V,  --version           display the version of Wget and exit.
  -h,  --help              print this help.
  -b,  --background        go to background after startup.
  -e,  --execute=COMMAND   execute a `.wgetrc'-style command.

This allows me to freely leave the connection hanging, and it’ll still continue at home without me. But what about progress now? Ever heard of tail?

Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.

There’s an another option that will allow me to follow the increasingly added data to the file too, essentially appending it to the original 10 lines. In short, it will add 10 lines to the screen as soon as they are ready. When you started the background wget you also were told about the wget_log file: “wget-log”.

So just run a tail -f wget-log and you’ll see the output of the progress of your super massive download that is decoupled from your terminal session! It’s fantastic.

Posted in Tips | Tagged download, ssh, terminal, wget | 1 Response

The Universe #2 – Competition We’re Afraid Of

By Ryan on February 16, 2012

Back in the great days of the space race, we had competition we were afraid of. Today, we’re sorely lacking competitors in the race of reaching mars and the wonders beyond. On the second episode 2 of The Universe, Sam Ebertz and I discuss the space race that we’re not a part of, the joys of WolframAlpha pro, the incredible Milky Way duplicate, P=NP’s problems, and more!

This episode was delayed a little but now that it’s up, it’s an incredible show with lots of fun and jokes. If you’re in a science major, this is required listening. If you’re not, then well, you should listen to this so you’re “well rounded“. So listen to The Universe #2 – Competition We’re Afraid Of at The-Nexus.tv.

Posted in Podcast | Tagged podcast, universe | Leave a response

Java: Duplicate local variable

By Ryan on February 15, 2012

It’s usually easy to catch errors in Java, either through the IDE you’re using or at compile time. Recently, I helped a friend taking a course in Java with an error he was getting in a lab he was working on. It mentioned something about a duplicate variable.

Duplicate local variable

Unfortunately, I didn’t grab his erring source code earlier, so I can’t give the true real world source. Nevertheless, my own crafted code should illuminate the problem sufficiently.

public static void main(String[] args) {

	int magic = 10;
	if ( 10 < 11 ) {
		int magic = 12;
		magic++;
	}

}

Imagine you had a variable declaration and initialization at the top of some method. Then later you attempt to create another variable with the same name as before, but inside of a construct like a for loop or if. Unwittingly, you have attempted to redeclare an already declared variable.

Why doesn’t the IDE or compiler know that it’s a syntax error in that case, and that you obviously cannot redeclare variables? Java’s just funny like that, but I suspect it has something to do with the scoping rules in Java. The magic variable inside of the if-statement should be separate, and it is telling you it’s a duplicate, not that it isn’t allowed. So I suppose that’s helpful.

In short, the easy fix is to rename the inner-conflicting variable. So the magic that’s in the if-statement could be magic2 and anything that references it in the if should also be updated. That’s clear enough. The other possibility is to remove the int declaration. Imagine you were simply setting value instead of declaring. It’s an easy mistake to make, especially when you’ve a bunch of declarations repeated.

Happy deduplicating!

Posted in Errors, Java | Tagged duplication, error, Java, variable | Leave a response

Next »

Recent Tweet

  • Oh, how good is a Mumford & Son's song is. The perfect mix of solemn spring and winnowing winter. 10 hours ago

Recent Posts

  • Java: syntax error on token “else”, delete this token
  • At The Nexus & The Universe – Updates
  • wget: background download
  • The Universe #2 – Competition We’re Afraid Of
  • Java: Duplicate local variable
  • scheme: (clear)
  • The Universe & At The Nexus – Updates
  • Ting – The New Guy In No-Contract
  • The-Nexus.TV: Early AWS S3 Usage
  • Episode 11 – Comfy Pants & Doom Clock

Popular Topics

Apple Browsers cakephp chrome errors exam Google Java JavaScript linux MooTools PHP review terminal Tips twitter ubuntu unified Windows WordPress

Search

Archives

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Copyright © 2012 Ryan Rampersad.

Powered by WordPress, Hybrid, and Ryan's Hybrid Theme.