<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7133515633663030606</id><updated>2024-09-04T05:03:14.972-07:00</updated><category term="Java"/><category term="Algorithm"/><title type='text'>Crazenezz</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://crazenezz.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7133515633663030606/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://crazenezz.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Edward Tumuwo</name><uri>http://www.blogger.com/profile/00887625716270803366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7133515633663030606.post-800446255509745131</id><published>2012-08-14T01:38:00.000-07:00</published><updated>2012-08-14T01:38:16.531-07:00</updated><title type='text'>CrzDB vs Standard JDBC</title><content type='html'>I tried to make a library that has function for CRUD (Create, Read, Update and Delete) to simplify database connection with JDBC. Let see the example so we can understand what is the main point of the &lt;a href=&quot;https://github.com/Crazenezz/CrzDB&quot; target=&quot;_blank&quot;&gt;CrzDB&lt;/a&gt;:&lt;br /&gt;
&lt;br /&gt;
First create database named &quot;tryout&quot; in MySQL:&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;mysql&amp;gt; create database tryout;&lt;/li&gt;
&lt;li&gt;mysql&amp;gt; use tryout;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
And create the table &quot;user&quot;:&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;mysql&amp;gt; create table `user`(&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -&amp;gt; `user_name` varchar(100) NOT NULL,&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -&amp;gt; `address` varchar(200) DEFAULT NULL,&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -&amp;gt; `first_name` varchar(50) NOT NULL,&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -&amp;gt; `last_name` varchar(50) DEFAULT NULL,&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -&amp;gt; `middle_name` varchar(50) DEFAULT NULL,&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -&amp;gt; PRIMARY KEY (`user_name`));&lt;/li&gt;
&lt;/ol&gt;
After that insert one record for testing:&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;mysql&amp;gt; INSERT INTO `user` VALUES(&#39;crazenezz&#39;, NULL, &#39;craze&#39;, NULL, NULL);&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
If we using Standard JDBC we will see this kind of code to get all the user.&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import com.mysql.jdbc.Connection;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import com.mysql.jdbc.Statement;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import java.sql.DriverManager;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import java.sql.ResultSet;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import java.sql.SQLException;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import java.util.logging.Level;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import java.util.logging.Logger;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;public class SimpleDBConnection {&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; public static void main(String[] args) {&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try {&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Connection conn = (Connection) DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/tryout&quot;, &quot;root&quot;, &quot;root&quot;);&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Statement state = (Statement) conn.createStatement();&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ResultSet rSet = state.executeQuery(&quot;SELECT * FROM user&quot;);&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while(rSet.next()) {&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; System.out.println(&quot;Username: &quot; + rSet.getString(&quot;user_name&quot;));&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; System.out.println(&quot;First Name: &quot; + rSet.getString(&quot;first_name&quot;));&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (SQLException ex) {&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Logger.getLogger(SimpleDBConnection.class.getName()).log(Level.SEVERE, null, ex);&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;}&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;And let see if we use the &lt;a href=&quot;https://github.com/Crazenezz/CrzDB&quot; target=&quot;_blank&quot;&gt;CrzDB&lt;/a&gt;:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;import java.util.Map;&lt;/li&gt;
&lt;li&gt;import org.crz.db.config.Config;&lt;/li&gt;
&lt;li&gt;import org.crz.db.config.abs.impl.MySQLConfig;&lt;/li&gt;
&lt;li&gt;import org.crz.db.query.SQLQuery;&lt;/li&gt;
&lt;li&gt;public class CustomDBConnection {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Config config = new Config(MySQLConfig.getInstance(&quot;tryout&quot;));&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SQLQuery query = new SQLQuery(config.getState(), &quot;user&quot;);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;Map&amp;gt; datas = query.getAll();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(Map data : datas) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;Username: &quot; + data.get(&quot;user_name&quot;));&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;First Name: &quot; + data.get(&quot;first_name&quot;));&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.closeConnection();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li style=&quot;background-color: #eeeeee;&quot;&gt;}&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;It can save your time for query and you don&#39;t need to know and memorize every queries. For now &lt;a href=&quot;https://github.com/Crazenezz/CrzDB&quot; target=&quot;_blank&quot;&gt;CrzDB&lt;/a&gt; support MySQL, PostgreSQL and Oracle for database connection, in the future I want to expand the connection to other databases. And no need to change for the query inside if we want to immigrate to other database e.q from MySQL to Oracle or PostgreSQL to DB2. &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Because in my opinion application programmer don&#39;t need to be like database programmer.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;If you more interested about this library you can check it out from &lt;a href=&quot;https://github.com/Crazenezz/CrzDB&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. and you can see another example in &lt;a href=&quot;https://github.com/Crazenezz/CrzDB/blob/master/test/ConnectionTest.java&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;I&#39;m using:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Netbeans 7.1.1 as IDE&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;JDK 1.7.0_05&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://crazenezz.blogspot.com/feeds/800446255509745131/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://crazenezz.blogspot.com/2012/08/crzdb-vs-standard-jdbc.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7133515633663030606/posts/default/800446255509745131'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7133515633663030606/posts/default/800446255509745131'/><link rel='alternate' type='text/html' href='http://crazenezz.blogspot.com/2012/08/crzdb-vs-standard-jdbc.html' title='CrzDB vs Standard JDBC'/><author><name>Edward Tumuwo</name><uri>http://www.blogger.com/profile/00887625716270803366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7133515633663030606.post-6544201538132577612</id><published>2012-07-25T23:15:00.004-07:00</published><updated>2012-07-31T19:48:28.504-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>import package.* vs. import package.class_name</title><content type='html'>&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;import java.util.*;&lt;/li&gt;
&lt;li&gt;public class ReadArgs {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Scanner input = new Scanner(System.in);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String text = input.next();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(text);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Code 1&lt;/i&gt;&lt;/div&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;import java.util.Scanner;&lt;/li&gt;
&lt;li&gt;public class ReadArgs {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Scanner input = new Scanner(System.in);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String text = input.next();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(text);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Code 2 &lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
When you see two codes above, you will see 1 different in the first line. What is the difference with &lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import java.util.*&lt;/span&gt; and &lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import java.util.Scanner&lt;/span&gt;?&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import java.util.*&lt;/span&gt; will make all classes inside the java.util package visible and can be used in the class that import the package (in this case ReadArgs class). &lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import.java.util.Scanner&lt;/span&gt; will make only Scanner class will be visible in the class that import the package.&lt;/li&gt;
&lt;li&gt;There is no different performance or size after the class compiled.&lt;/li&gt;
&lt;li&gt;Better to use &lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import package.class_name&lt;/span&gt; for documentation and don&#39;t forget to order by class name for readability. &lt;i&gt;There are drawback for this if you don&#39;t use tools or IDE such as Netbeans or Eclipse for build the code. You will get some difficulties when delete the unused class when the application get bigger.&amp;nbsp;&lt;/i&gt;&lt;/li&gt;
&lt;/ol&gt;
So, there&lt;i&gt; &lt;/i&gt;is no different whether you use the &lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import package.*&lt;/span&gt; and &lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import package.class_name&lt;/span&gt; if we see on performance, and you can use the &lt;span style=&quot;background-color: #eeeeee;&quot;&gt;import package.class_name&lt;/span&gt; for readability and documentation purpose.&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;
&lt;b&gt;References:&lt;/b&gt;&lt;i&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;a href=&quot;http://www.leepoint.net/notes-java/language/10basics/import.html&quot; target=&quot;_blank&quot;&gt;Packages and Import&lt;/a&gt;&lt;/i&gt;</content><link rel='replies' type='application/atom+xml' href='http://crazenezz.blogspot.com/feeds/6544201538132577612/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://crazenezz.blogspot.com/2012/07/import-package-vs-import.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7133515633663030606/posts/default/6544201538132577612'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7133515633663030606/posts/default/6544201538132577612'/><link rel='alternate' type='text/html' href='http://crazenezz.blogspot.com/2012/07/import-package-vs-import.html' title='import package.* vs. import package.class_name'/><author><name>Edward Tumuwo</name><uri>http://www.blogger.com/profile/00887625716270803366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7133515633663030606.post-1586305074975814549</id><published>2012-07-13T02:40:00.001-07:00</published><updated>2012-07-25T23:41:43.303-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Playing with Pseudocode - Sequence of Numbers</title><content type='html'>If I have string of numbers like this &quot;12345&quot; for the input, and I want the output as a sequence of 3 numbers:&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;123&lt;/li&gt;
&lt;li&gt;234&lt;/li&gt;
&lt;li&gt;345&lt;/li&gt;
&lt;li&gt;451 &lt;/li&gt;
&lt;li&gt;512&lt;/li&gt;
&lt;/ol&gt;
How to convert it to become Java Program?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;First, let us make the pseudo-code before trying to code instead.&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;Declare `n` = &quot;12345&quot; &lt;/li&gt;
&lt;li&gt;Declare `length` = length of `n`&lt;/li&gt;
&lt;li&gt;Declare `i` as integer&lt;/li&gt;
&lt;li&gt;Loop start from `i` = 0 to `length`- 1&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;Display sub-string of `n` from `i` to `i` + 3&lt;/li&gt;
&lt;/ol&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;b&gt;And convert it to code.&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;&amp;nbsp;public class SequenceNumber {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String n = &quot;12345&quot;;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int length = n.length();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i = 0; i &amp;lt; length; i++) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(n.substring(i, i + 3));&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;} &lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;b&gt;&amp;nbsp;And let us check the output.&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;123&lt;/li&gt;
&lt;li&gt;234&lt;/li&gt;
&lt;li&gt;345&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: red;&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;in thread &quot;main&quot; java.lang.StringIndexOutOfBoundException: String index out of&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;range: 6&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: red;&quot;&gt;at java.lang.String.substring(String.java:1955)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: red;&quot;&gt;at SequenceNumber.main(SequenceNumber.java:8) &lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&amp;nbsp;Wow, for the first time to code and we already make an exception, nice. Let&#39;s try figure out, in the line 8 of class Sequence Number the compiler said that &quot;&lt;b&gt;String index out of range: 6&lt;/b&gt;&quot; that means when `i` = 3 it will get the sub-string of `n` from 3 to 6. If you try to display the `length` it will print 5, so the problem that make this exception is `i` + 3. Let&#39;s change the pseudo-code to make it avoid the exception.&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;Declare `n` = &quot;12345&quot; &lt;/li&gt;
&lt;li&gt;Declare `length` = length of `n`&lt;/li&gt;
&lt;li&gt;Declare `i` as integer&lt;/li&gt;
&lt;li&gt;Declare `x` as integer &lt;/li&gt;
&lt;li&gt;Loop start from `i` = 0 to `length` - 1&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;Check if `i` + 3 &amp;gt; `length`&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;Set `x` = length of sub-string of `n` from `i`&lt;/li&gt;
&lt;li&gt;Display sub-string of `n` from `i` concat with sub-string of `n` from 0 to 3 - `x`&lt;/li&gt;
&lt;/ol&gt;
&lt;li&gt;Else&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;Display sub-string of `n` from `i` to `i` + 3&lt;/li&gt;
&lt;/ol&gt;
&lt;/ol&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;b&gt;And make some update in the current code.&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;public class SequenceNumber {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String n = &quot;12345&quot;;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int length = n.length();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int x;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (i = 0; i &amp;lt; length; i++) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (i + 3 &amp;gt; length) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = n.substring(i).length();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(n.substring(i) + n.substring(0, 3 - x));&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(n.substring(i, i + 3));&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;i&gt;If you want to make it shorter in the code, try this one.&lt;/i&gt;&lt;br /&gt;
&lt;ol style=&quot;background-color: #eeeeee;&quot;&gt;
&lt;li&gt;public class SequenceNumber {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char[] digits = Integer.toString(12345).toCharArray();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; digits.length; i++) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(&quot;&quot; + digits[i] + digits[(i + 1) % digits.length] + digits[(i + 2) % digits.length]);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&amp;nbsp;Good luck...&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;References:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;a href=&quot;http://stackoverflow.com/a/10794160/1159948&quot; target=&quot;_blank&quot;&gt;How can get the possible 3 digit combination from a number having more than 3 digits in java.&lt;/a&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;ol&gt;
&lt;/ol&gt;
&lt;/ol&gt;</content><link rel='replies' type='application/atom+xml' href='http://crazenezz.blogspot.com/feeds/1586305074975814549/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://crazenezz.blogspot.com/2012/07/playing-with-pseudocode-sequence-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7133515633663030606/posts/default/1586305074975814549'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7133515633663030606/posts/default/1586305074975814549'/><link rel='alternate' type='text/html' href='http://crazenezz.blogspot.com/2012/07/playing-with-pseudocode-sequence-of.html' title='Playing with Pseudocode - Sequence of Numbers'/><author><name>Edward Tumuwo</name><uri>http://www.blogger.com/profile/00887625716270803366</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>