Category Archives: Code Samples

Simple Java Codes and Basic Programs Example for Beginners

Simple Java Codes and Basic Programs Example for Beginners
NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT&T T-Mobile
Current Bid: $63.96

Lists of Simple Java Codes, Java Basic Program for beginners, Examples of Java sample programs and projects as well as other java Simple Programs for beginners.

This hub has the lists of Java simple codes for beginners. You can run and test it with the java compiler you have. As for me, I use netbeans and eclipse compilers, though I use netbeans more. I left some comments on few codes to be more clear. If you want to learn java more, feel free to check my other tutorials for beginners and other java source code examples. May you enjoy your coding on java.

Echoing words in Java

To output words in Java use the pre-defined function System.out.print().Here is the complete source codes for outputting word in Java.

public class Main
{
public static void main(String[] args)
{
System.out.print(“This is just an example…”);
}
}

Output:

This is just an example…

Inputting and Echoing integer and string entered by user

If the program prompt input from user their will be declaration of variables. See the source codes below:

import java.util.Scanner; //to be able to use scanner predefined function for input
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); // input is an object and you can choose your own too.
int num1; //num1 is a variable of integer type you can change it
String word1; //word1 is a variable of string type you can change it
System.out.print(“Enter a number and I will echo it later: “);
num1=input.nextInt(); //read input for num1
System.out.print(“Enter a word and I will echo it later: “);
word1 = input.next(); //read input for word1
System.out.println(“Here is the echo of what you have entered:”);
System.out.println(“The number you have entered is: ” + num1); //echoing input to num1
System.out.println(“The word you have entered is: ” + word1); //echoing input to word1
}
}

See Also:

How to Run Java Code Online

Output:
Enter a number and I will echo it later: 88

Enter a word and I will echo it later:hello

Here is the echo of what you have entered:

The number you have entered is 88

The word you have entered is hello

These are just a very simple program in Java to view more codes I will upload more programming projects like basic arithmetic in java including addition, subtraction, multiplication, square root and some programs in OOP (Object Oriented Programming) using looping, array and function. Thanks for reading.

Updated:

Java Tutorials and Tips

Complete List of Java Tutorial Examples for Beginners
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java: Complete Simple Easy Steps
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Class: Learn More About Classes in Java

Java Video Tutorials

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99
Learn JAVA language -14h video & docs tutorials
Current Bid: $4.59

Java Books

Java Programming : From Problem Analysis to Program Design by D. S. Malik…
Current Bid: $2.99
Introduction to Java Programming, Comprehensive Version 9e by Y. Daniel Liang
Current Bid: $50.80

Other Java Source Codes

What is a Java Scanner Class and How to Use it?
What is a Static Method in Java?
Casting in Java
BufferedReader in Java
Fibonacci Java Program: Complete Program for Fibonacci Series
Count the Number of Vowels in Java String
Java Program: How to Use If Statement in Java Programming
Java Program: How to Use If-Else Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming
Java Source code on Adding Numbers inside the Array using For Loop
Java Program: How to Use Switch Statement in Java
Java Source codes on Adding numbers inside the Array Using Recursion
Java Program: How to Sort Numbers in Ascending Order
Java Program: How to Sort Numbers in an Array in a Descending Order
Java Program: How to Parse a String into Integer in Java Programming
Java Program: Count the Number of String Characters in Java
Java Program: Reverse String in Java Using For Loop
Java Program: Palindrome Test Java Source Code
Sample Java Program for String Tokenizer
Sample Program for JOptionpane and Java Source Code for Basic Calculator
Java Code Sample: Sort Numbers in Bubble Sort

Create A Logging Function In A C# Singleton

Create A Logging Function In A C# Singleton

How to write a logging function for your C# software

I have used the singleton design pattern for many years. It comes in handy for so many things, most especially for logging functions. A singleton is a class which can only be instantiated once, thus using it to hold a globally accessible logging function makes sense.

To accomplish this, you start by creating a class in Visual Studio that will contain all of your singleton code.

Creating the singleton class

public class MyEnvironment
{
private static MyEnvironment instance = null;
private static string sLogFilePath;
protected MyEnvironment()
{
sLogFilePath = Application.StartupPath.ToString();
}
public static MyEnvironment Instance()
{
if (instance == null)
{
instance = new MyEnvironment();
}
return instance;
}

Notice that there is a variable in the code called sLogFilePath. This will hold the path where our log files will be written. By default it is assigned the directory in which the application was started. You can add additional paths if a directory other than the application root is needed, or you can create a simple assignment call that allows your application to set the log file directory on the fly. This is great if your application allows the users to change these directories.

public SetLoggingDir
{
set { sLogFilePath = value; }
}

From here, we now create the WriteToLog function. This function will act as the global logging function for any and all classes in our current project. The way it’s written here, the logs run for a day as indicated by the filename. This allows you to delete old logs after a period of time.

public void WriteToLog(string sText)
{
DateTime dtNow = DateTime.Now;
StreamWriter myStream = null;
sText = dtNow.ToShortDateString() + ” ” +
dtNow.ToShortTimeString() + “: ” + sText;
string sLogFilename = sLogFilePath + “\” +
string.Format(“{0:D2}{1:D2}{2:D2}MyApp.log”, (dtNow.Year-2000), dtNow.Month, dtNow.Day);
using (myStream = new StreamWriter(sLogFilename, true))
{
myStream.WriteLine(sText);
}
}

The function takes the message to be logged as the argument. The function then creates a string that is date / time stamped followed by the message. To call the WriteToLog function, simply use:

MyEnvironment.Instance().WriteToLog(“Application message test”);

 

This canned function embedded within a singleton makes this some of the most re-usable code I have. Hope this helps you out.

Check out some of my software

eSource Development – Your partner in creating world class websites and software

Source For Reference

Professional Test Driven Development with C#: Developing Real World Applications with TDD
Amazon Price: $23.94
List Price: $44.99
Windows 8 Apps with XAML and C# Unleashed
Amazon Price: $27.45
List Price: $49.99
Agile Principles, Patterns, and Practices in C#
Amazon Price: $33.00
List Price: $69.99
Professional Test Driven Development with C#: Developing Real World Applications with TDD
Amazon Price: $44.99