Mar 21

We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used.

It reads the input from the user and checks if it is a valid double. If its not it prompts the user again. Could easily be modified for different validation that you may require.

  Scanner input = new Scanner( System.in );
  Double value = null;
  while (value==null) {
	  System.out.print("Please enter number: ");
	  String s = input.nextLine();
	  if (s.length()==0) {
		  System.out.println("You did not enter a value; Try again");
	  } else {
		  try {
			  value = new Double(s);
		  } catch (Exception ex) {
			  System.out.println("You did not enter a valid value; Try again");
		  }
	  }
  }
  double d = value.doubleValue();

written by objects \\ tags: , , , ,

Apr 05

Writing to the Firebug console can be done using
console.log("Hello World");

As well as a log() method there are also debug(), info(), warn() and error() methods which the log message gets suitably color coded in the Firebug console.

The methods all support printf style formatting

console.log("Received %d messages", messageCount);

written by objects \\ tags: , , ,

Feb 04

First step is to load this page and check that the problem is not with your VM

http://www.java.com/en/download/help/testvm.xml

If the VM is ok then next step is to check the Java Console.
First turn on full console logging by pressing ‘5’ in the Java Console
Then load the applet and check contents of console firstly for errors loading the applet jars and classes, then check for exceptions starting the applet itself.

Still can’t work out the problem? Post a comment here, preferably with a url that we can use to load it and we’ll check it for you.

written by objects \\ tags: ,