Sort numbers in Java to find minimum and maximum values without using Array

7 Comments


Recently a reader contacted me with a question about sorting numbers in Java. After sorting the number the program then needs to print the largest and smallest values. I have written a post earlier that shows one way of finding largest and smallest numbers. That approach used Arrays but the reader wanted to find largest and smallest values from a group of numbers without using Arrays. So here is another approach.

This program accepts input from the user and then prints out the largest and smallest numbers.



import java.util.*;
public class NumberSorter{
	public static void main(String args[]){
	  double a, b, c, x, y;
	  Scanner console = new Scanner(System.in);
	  System.out.print("Enter the first number: " );
	  a = console.nextDouble() ;
	  System.out.print("Enter the second number: " );
	  b  = console.nextDouble() ;
	  System.out.print("Enter the third number: " );
	  c = console.nextDouble();
	  x= Math.min(a, Math.min(b, c));
	  y= Math.max(a, Math.max(b, c));
	  System.out.println("\n" +x + " is the smallest number.\n" + y
	                     +" is the largest number.");
	}
}

Hope you find this useful. Share your ideas on how else can we find largest and smallest values from a group of numbers Java.

article clipper vert Sort numbers in Java to find minimum and maximum values without using Array
 

Java String comparison. The difference between == and equals().

24 Comments

javaString1 300x297 Java String comparison. The difference between == and equals().Many Java beginners find it difficult to differentiate between == operator and the “equals()” method when comparing String variables in Java. They assume that both operations perform the same function and either one can be used to compare two string variables. I have even seen many experienced programmers committing the mistake of using “==” to compare the values of two strings.

So what is the difference between “==” and “equals()” and what is the correct method of comparing two string variables?
More

article clipper vert Java String comparison. The difference between == and equals().
 

How to convert an ArrayList to array in Java

1 Comment


Today, I will share a very basic Java program that converts an ArrayList to array. For most Java developers this is a routine task, but many new comers to Java have asked me this question of how to convert an array to ArrayList. Here is the example code.
More

article clipper vert How to convert an ArrayList to array in Java
 

How to validate date using Java regular expression

7 Comments


My earlier post on how to validate email address, SSN and phone number validation using Java regex still attracts lot of visitors. Today I realized that another piece of data that many programmers need to validate is the date. Many Java applications have to process input date values, so I thought it will be beneficial to this blog readers to show how regular expression can be used to validate date in java.

First I’ll show you how to validate date using java reg ex in US format and later I’ll show you how that same logic can be applied to validate date in English format (used in most countries outside North America).
More

article clipper vert How to validate date using Java regular expression
 

How to write to properties file in Java

4 Comments


One of most visited posts on this blog is How to read properties file in Java. In that post I explained how you can read from a properties file. But many people came to that post searching for an example on how to write to a properties file. So I thought it will be beneficial for those visitors if we a have separate post with an example of how to write to a properties file in Java.

More

article clipper vert How to write to properties file in Java
 

Best way to check if a Java String is a number.

2 Comments

One of the routine tasks in many Java applications is to convert a string to a number. For instance, you may have a form where user submits his or her age. The input will come to your Java application as a String but if you need the age to do some calculation you need to convert that String into a number.
More

article clipper vert Best way to check if a Java String is a number.
 

How to determine smallest and largest number in Java

No Comments

numbers 300x141 How to determine smallest and largest number in Java

Numbers


A couple of weeks ago a reader asked me for a Java program to read command-line input from the user and then display the smallest value the user has entered. He wanted to use digit ’0′ to indicate that the user is done entering the data and not to include 0 in the list of numbers entered by the user. Here is the Java code to do this. Bonus: This program also determines the largest value entered by the user.
More

article clipper vert How to determine smallest and largest number in Java
 

New features coming to Java 7

2 Comments


Joseph Darcy, the lead of Project Coin, formally announced the approved changes to the Java language to be included in JDK 7.  Although there was no major change announced, the announced enhancements will improve program readability. Following are few changes that I am glad they are going to incorporate in JDK7.
More

article clipper vert New features coming to Java 7
 

How to search for a string in a file using Java regular expression.

4 Comments


If you need to search for a phrase or a string in a text file Java regular expression is the easiest way to accomplish this task. Here is a simple example that demonstrates how you can use Java regular expression to find a string or a phrase in a text file.
More

article clipper vert How to search for a string in a file using Java regular expression.
 

How to remove (delete) Eclipse workspace from the dropdown list.

3 Comments

Eclipse is my favorite Java IDE. To manage projects Eclipse uses ‘Workspaces’. Workspace is basically a folder on your disk where your project files are stored. To work on a project you load its workspace in Eclipse. Eclipse remembers the workspaces you worked on previously. A drop down list of all (configurable) previous workspaces is displayed when you want to switch workspace. This is pretty good except for one little annoyance. Eclipse presents all previous workspaces even if you have deleted the project folder from the disk. All those deleted workspaces only clutter the list. But there is a very easy way to clean up the list. All you have to do is to edit org.eclipse.ui.ide.prefs file.

This file is located under configuration/.setting directory under your eclipse installation directory. For e.g. if you have Eclipse installed under C:\ides\eclipse the file will be under C:\ides\eclipse\configuration\.settings.
More

article clipper vert How to remove (delete) Eclipse workspace from the dropdown list.