Showing posts with label user input. Show all posts
Showing posts with label user input. Show all posts

Java receive user input and sort it in ascending order

Complete source code below is a simple java program that will receive user input and after that sort it in ascending order before display it's result.

*********************************************************************
COMPLETE SOURCE CODE FOR : AscendingString.java
*********************************************************************


import java.util.Scanner;

public class AscendingString
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
String[]a=new String[5];
for(int i=0;i<5;i++)
{
System.out.println(i+1+".Put your String");
String temp=s.nextLine();
a[i]=temp;
}

System.out.println("**********");
System.out.println("RESULT");
System.out.println("**********");

for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
String temp=a[i];
String tempB=a[j];
if(temp.compareTo(tempB)<0)
{
a[i]=tempB;
a[j]=temp;
}
}
}

for(int i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Java validate integer user input


Sometime, when we play with user input, we want to validate the input value is specific to certain data type. For example, you have a simple java program that query for user's age. Like we know age is in integer data type. So how to handle if a user put his age something like this, "fifty" or 50.34. This is impossible right? But if the user want to test the robustness of your system from error handling view, this is not impossible.Now i want to share with you, how you can handle this in a simple java program that run through command prompt.The basic idea is, if a user put other than integer value, our program will throw an exception. So, we will put our code that tell user what he/she do is wrong in the exception block.In other tutorial, i will share with you how to create your own exception handling.

***************************************************************
COMPLETE SOURCE CODE FOR : ValidateIntegerInput.java
***************************************************************


import java.util.Scanner;
import java.util.InputMismatchException;

public class ValidateIntegerInput
{
public static void main(String[]args)
{
boolean benchmark=true;
while(benchmark)
{
Scanner a=new Scanner(System.in);

try
{
System.out.println("Put 0 to exit");
System.out.println("What is your age?");

//nextInt will throw InputMismatchException
//if the next token does not match the Integer
//regular expression, or is out of range
int b=a.nextInt();
if(b==0)
{
benchmark=false;
}
else
{
System.out.println("Your age is : "+b);
}
}
catch(InputMismatchException exception)
{
//Print "This is not an integer"
//when user put other than integer
System.out.println("Please put integer value");
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}
}


***************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************

Compare current date with user input date in java

Complete source code below will show you, how to compare current date with user input date in java.

***************************************************************************
COMPLETE SOURCE CODE FOR : GetDate.java
***************************************************************************


import java.util.Date;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class GetDate implements ActionListener
{
int year=0;
int month=0;
int date=0;

String [] yearContent={"2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011"};
String [] monthContent={"January","February","March","April","Mei","Jun","July","August","September","October","November","December"};
String [] dateContent={"1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31"};

JComboBox yearComboBox=new JComboBox(yearContent);
JComboBox monthComboBox=new JComboBox(monthContent);
JComboBox dateComboBox=new JComboBox(dateContent);

JButton button=new JButton("Click here to compare to current date");

JFrame frame=new JFrame("Compare date with current date");

public GetDate()
{
button.addActionListener(this);

Box box=Box.createHorizontalBox();
box.add(dateComboBox);
box.add(monthComboBox);
box.add(yearComboBox);

frame.setLayout(new GridLayout(2,1));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(box);
frame.add(button);
frame.setSize(500,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
String tempDate=(String)dateComboBox.getSelectedItem();
date=Integer.parseInt(tempDate);

month=monthComboBox.getSelectedIndex();

String tempYear=(String)yearComboBox.getSelectedItem();
year=Integer.parseInt(tempYear)-1900;

Date currentDateValue=new Date();
Date userChooserDate=new Date(year,month,date);

int currentYear=currentDateValue.getYear();
int currentMonth=currentDateValue.getMonth();
int currentDate=currentDateValue.getDate();

if((date==currentDate)&&(month==currentMonth)&&(year==currentYear))
{
JOptionPane.showMessageDialog(frame,"Same date","SAME",JOptionPane.INFORMATION_MESSAGE);
}
else if(userChooserDate.compareTo(currentDateValue)<0)>0)
{
JOptionPane.showMessageDialog(frame,"Welcome to the future","FUTURE",JOptionPane.INFORMATION_MESSAGE);
}
}
}

public static void main(String[]args)
{
GetDate gd=new GetDate();
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************

Read floating point number user input in java

Complete source code below, will show you how to wait and read floating point number user input in command prompt.

********************************************************************
COMPLETE SOURCE CODE FOR : ReadFloatingPointNumberUserInput.java
********************************************************************


import java.util.Scanner;

public class ReadFloatingPointNumberUserInput
{
public static void main(String[]args)
{
try
{
//Create a scanner object
//Scanner is a predefined class in java that will be use to scan text
//System.in is mean, we will receive input from standard input stream
Scanner readUserInput=new Scanner(System.in);

//Print into command prompt(Put a floating point number : )
System.out.print("Put a floating point number : ");

//Wait and READ FLOATING POINT NUMBER from user input after user hit 'Enter'
float myPointNumber=readUserInput.nextFloat();

//Print what store in myPointNumber
System.out.println("My floating point number is : "+myPointNumber);
}
catch(Exception exception)
{
//Error that will be print if user put other than floating point number
System.out.println("Hey !!! Don't put other than floating point number");
}
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Read integer user input in java

Complete source code below, will show you how to wait and read integer user input in command prompt.

**********************************************************************
COMPLETE SOURCE CODE FOR : ReadIntegerUserInput.java
**********************************************************************


import java.util.Scanner;

public class ReadIntegerUserInput
{
public static void main(String[]args)
{
try
{
//Create a scanner object
//Scanner is a predefined class in java that will be use to scan text
//System.in is mean, we will receive input from standard input stream
Scanner readUserInput=new Scanner(System.in);

//Print into command prompt(What is your age ?)
System.out.println("What is your age ?");

//Wait and READ INTEGER INPUT from user after user hit 'Enter'
int myAge=readUserInput.nextInt();

//Print what store in myAge
System.out.println("Your age is : "+myAge);
}
catch(Exception exception)
{
//Error that will be print if user put other than number include
//point number
System.out.println("Hey !!! Don't put other than number");
}
}
}


**********************************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************************

Read a word user input in java

Complete source code below, will show you how to wait and read only a word from user input in command prompt.

*****************************************************************
COMPLETE SOURCE CODE FOR : ReadOneWordUserInput.java
*****************************************************************


import java.util.Scanner;

public class ReadOneWordUserInput
{
public static void main(String[]args)
{
//Create a scanner object
//Scanner is a predefined class in java that will be use to scan text
//System.in is mean, we will receive input from standard input stream
Scanner readUserInput=new Scanner(System.in);

//Print into command prompt(What is your gender ?)
//Print into command prompt(Female/Male)
System.out.println("What is your gender ?\nFemale/Male");

//Wait and READ A WORD from user input after user hit 'Enter'
//Method next() in class scanner will read only first word in user input
//So, second word that seperate by space will be ignore.
String myGender=readUserInput.next();

//Print what store in myGender
System.out.println(myGender);
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Read text user input in java

Complete source code below, will show you how to wait and read text user input in command prompt.

********************************************************************
COMPLETE SOURCE CODE FOR : ReadTextUserInput.java
********************************************************************


import java.util.Scanner;

public class ReadTextUserInput
{
public static void main(String[]args)
{
//Create a scanner object
//Scanner is a predefined class in java that will be use to scan text
//System.in is mean, we will receive input from standard input stream
Scanner readUserInput=new Scanner(System.in);

//Print into command prompt(What is your name ?)
System.out.println("What is your name ?");

//Wait and READ TEXT INPUT from user after user hit 'Enter'
String myName=readUserInput.nextLine();

//Print what store in myName
System.out.println(myName);
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Wait user input in java

Complete source code below will show you, how to wait for user input in command prompt.
Input is a line of text.

**********************************************************************
COMPLETE SOURCE CODE FOR : WaitUserInput.java
**********************************************************************


import java.util.Scanner;

public class WaitUserInput
{
public static void main(String[]args)
{
//Create a scanner object
//Scanner is a predefined class in java that will be use to scan text
//System.in is mean, we will receive input from standard input stream
Scanner readUserInput=new Scanner(System.in);

//Print into command prompt(What is your name ?)
System.out.println("What is your name ?");

//WAIT FOR USER INPUT
//After user put it's name, he or she must press 'Enter'
//After user press 'Enter', all the input contents will read into 'myName'
String myName=readUserInput.nextLine();

//Print what it store in myName
System.out.println(myName);
}
}


**********************************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************************

User input in java

Ok, now i share with you how you can handle user input in java.

>>Receive user input from command line


>>Receive user input from input box


>>Receive user input from text field

>>Receive user input from text area

RELAXING NATURE VIDEO