Friday, October 27, 2017

Using Escape Character / Sequences or Special Characters

Disclaimer: If you find any inaccuracy in this article, please let me know how to improve it further.
==================================


List of all escape sequences is available at the bottom of https://docs.oracle.com/javase/tutorial/java/data/characters.html
One of those is "\t";

System.out.println("\t");
or,
System.out.println('\t');

The line above mimics the behavior of the "tab key" present on the keyboard. So, theywork in exactly the same way.

Tab key moves the cursor to the next "tab stops". Tab stops are the places where cursor jumps to, when the tab key is pressed.

In word processing applications such as LibreOffice Writer, you can set the locations or gaps for tab stops. Similarly, remember that we change "indentation level" in Dr Java IDE,  to 5 spaces (instead of 2), that tells amount of spaces the cursor will move when tab is pressed.

By default (since its invention), tab character moves 8 spaces, which is roughly an inch.
This behavior will be visible for the line of text that you print in java or write in notepad or word.

When you are writing some character, you are decreasing the space to be jumped.


For example,
if you only type "tab" and write "hello", it will give 8 spaces and "hello" will appear at 9th position.
If you write "winter", then "tab", then "bye", tab moves the cursor 2 spaces to reach 9th position.
If you write "winterA", then "tab", then "bye", tab moves the cursor 1 space to reach 9th position.
If you write "winterAB", then "tab", then "bye", tab moves the cursor 8 spaces to reach 17th position.

The lines containing hyphens and numbers below are for ease of demonstration only. The output should like as follows:

----\t--
12345678901234567890
        hello
winter  bye
winterA bye
winterAB        bye
12345678901234567890


In short, tab will move the cursor AFTER column positions that are multiples of 8. That is 9, 17, 25 ...... (8n+1) where n = 1, 2, ....

So, number of spaces to be jumped can vary from 1 to 8, depending on how many columns are occupied by previous text in the same line.

In some environments, instead of 8, it becomes 2 or 4 or 5 or 6 etc.
In those cases, tab stops will be at (2n+1) or (4n+1) or (5n+1) or (6n+1) etc.

Find out the behavior of your environment by copying and pasting following lines to the "interaction pane" of DrJava,

System.out.println("----\\t--");
System.out.println("12345678901234567890");
System.out.println("\thello");
System.out.println("winter\tbye");
System.out.println("winterAB\tbye");
System.out.println("12345678901234567890");


References:
1) http://en.wikipedia.org/wiki/Tab_stop
2) http://en.wikipedia.org/wiki/Tab_key#Usage
3) http://en.wikipedia.org/wiki/LibreOffice_Writer
4) http://www.coderanch.com/t/392682/java/java/tabs-spaces
5) http://cboard.cprogramming.com/c-programming/41287-horizontal-tab-%5Ct-what-determines-number-spaces.html
6) http://mathbits.com/MathBits/Java/Introduction/escapesequences.htm
7) https://docs.oracle.com/javase/tutorial/java/data/characters.html


If you know how to run java programs from commandline/console, then try printing \007 as well if you want to print something more awesome.

Don't forget to follow the steps :
https://web.archive.org/web/20140506231020/http://www.sadafnoor.com/blog/print-sound-java/

Save the program in My computer, directly inside C or D drive or any other drive
For example, as C:\testBeep.java
Then open and compile using DrJava
From Start menu -> Run, type cmd and click ok
From the black cmd / "command prompt" window,
type the following
java testBeep
It should make a beep sound.

Thursday, October 5, 2017

DrJava "Warning: Resource leak: '......' is never closed"

If you receive the following from DrJava "Warning: Resource leak:
'......' is never closed"

It is a warning / suggestion, not an error.
You may wish to add the following line as the last line of your program.
sc.close();
assuming sc is the scanner variable in your programs.

Monday, June 12, 2017

Program / Data Structure and Algorithm Visualization / Simulation

Being able to visualize or simulate how a program, data structure or algorithm is working may help to understand how it is working, help in troubleshooting, teaching, and may be in enhancing it further.
 
Please find some Visualization / Simulation sites below. Please feel free to suggest alternatives.


Data Structure and Algorithm Visualization

VisualGo


David Galles's Site

Algorithms and Data Structures Animations for the Liang Java, C++, and Python Books



Program Visualization

Java
Python, C, C++, Java, JS, Ruby, TypeScript

R

Web Apps (HTML, CSS, JS)


Recommended Reading

Sunday, February 26, 2017

Scanner input problem: nextLine combined with next/nextInt/nextDouble


nextInt() or nextDouble() takes one number and leaves "enter" in the
keyboard buffer.

next() takes one word and leaves "enter" in the keyboard buffer.

nextLine() keeps taking characters until "enter" and removes the
"enter" from keyboard buffer. So it effectively takes one sentence /
full name etc.

So, nextLine() written after nextInt() or nextDouble() or next() does
not work as expected because it takes that "enter" only which is left
by previous nextInt()/nextDouble()/next() and finishes its work.

In order to skip that unwanted "enter", use scanner.skip("[\r\n]+")
like in the following example.

//way 1 of 2:

       int n = scanner.nextInt();
        //following line is optional as nextInt/nextDouble/next() can skip "enter"
        scanner.skip("[\r\n]+");
        int m = scanner.nextInt();
        //following line is must, for the nextLine below to work
        scanner.skip("[\r\n]+");
        String fullName = scanner.nextLine();
        out.println("n="+n);
        out.println("m="+m);
        out.println("fullName="+fullName+"=");


//way 2 of 2:

        int n = scanner.nextInt();
        //following line is optional as nextInt/nextDouble/next() can skip "enter"
         scanner.skip("[\r\n]+");
        int m = scanner.nextInt();
        //following line is must for the fullName=..nextLine below to work
        scanner.nextLine();
        String fullName = scanner.nextLine();
        out.println("n="+n);
        out.println("m="+m);
        out.println("fullName="+fullName+"=");


Reference:
http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods