CEG's Linux User Group

  • Increase font size
  • Default font size
  • Decrease font size
Home Labs
Labs

Shell demonstrating string and int input

E-mail Print

Sample code for taking input from Shell demonstrating string and int input
For simple programs or testing purpose its not always required to take input thru GUI.

 

  • /**
  •  * Sample code for taking input from Shell demonstrating
  •  * string and int input
  •  * For simple programs or testing purpose its not always
  •  * required to take input thru GUI.
  •  *
  •  * @author aj.wh.ca
  •  * #link www.swiftthoughts.com
  •  */
  •  
  • import java.io.*;
  •  
  • public class ShellUtils
  • {
  • //get String or simply enter from shell
  • public static String getStringFromShell(String prompt)
  • {
  • try
  • {
  • System.out.print(prompt);
  • return new BufferedReader(new InputStreamReader(System.in)).readLine();
  • }
  • catch (IOException e)
  • {
  • e.printStackTrace();
  • }
  • return null ;
  • }
  •  
  • // get an int. Keep asking until not
  • public static int getIntFromShell(String prompt)
  • {
  • String line = "" ;
  • int num = 0 ;
  • while(line.equals(""))
  • {
  • line = getStringFromShell(prompt);
  • try
  • {
  • num = Integer.parseInt(line);
  • }
  • catch(NumberFormatException e)
  • {
  • System.out.println("Error: Invalid number");
  • line = "" ;
  • }
  • }
  • return num ;
  • }
  •  
  • // similiar methods can be made for getting char , double etc
  •  
  • public static void main(String args[])
  • {
  • String name = ShellUtils.getStringFromShell("Please enter your name ");
  • int age = ShellUtils.getIntFromShell("Please enter your age ");
  •  
  • System.out.println(name + " is "+ age + " years old !!!");
  • }
  •  
  • }
  • Last Updated on Thursday, 11 March 2010 23:26
     

    Circularly Singly Linked List

    E-mail Print
    User Rating: / 4
    PoorBest 

    CIRCULARLY SINGLY LINKED LIST

    Introduction :

    A singly linked circular list is a linked list where the last node in the list points to the first node in the list. A circular list does not contain NULL pointers. A good example of an application where circular linked list should be used is a timesharing problem solved by the operating system. In a timesharing environment, the operating system must maintain a list of present users and must alternately allow each user to use a small slice of CPU time, one user at a time. The operating system will pick a user, let him/her use a small amount of CPU time and then move on to the next user, etc. For this application, there should be no NULL pointers unless there is absolutely no one requesting CPU time.

    Last Updated on Tuesday, 16 June 2009 17:46 Read more...
     

    CIRCULAR QUEUE IMPLEMENTATION USING ADT

    E-mail Print
    User Rating: / 2
    PoorBest 

     

    IMPLEMENTATION OF A CIRCULAR QUEUE USING ADT

     

    The aim of this article is to implement a circular queue using abstract data type.

    INTRODUCTION:

    We know that a linear queue is a “first in first out “ data structure,i.e., insertion can be made only at the end and deletion can be made only at the front. In a linear queue, the traversal through the queue is possible only once,i.e.,once an element is deleted,we cannot insert another element in its position. This disadvantage of a linear queue is overcome by a circular queue, thus saving memory.

     

    Last Updated on Saturday, 13 June 2009 08:25 Read more...
     

    CIRCULAR QUEUE IMPLEMENTATION USING LINKED LIST

    E-mail Print
    User Rating: / 6
    PoorBest 

    IMPLEMENTATION OF A CIRCULAR QUEUE USING

    LINKED LIST

     

    INTRODUCTION:

    We know that a linear queue is a “first in first out “ data structure,i.e., insertion can be made only at the end and deletion can be made only at the front. In a linear queue, the traversal through the queue is possible only once,i.e.,once an element is deleted,we cannot insert another element in its position.

    This disadvantage of a linear queue is overcome by a circular queue, thus saving memory. In a circular queue, after rear reaches the end of the queue, it can be reset to zero. This helps in refilling the empty spaces in between.

    A linked list is a data structure that consists of a sequence of nodes. Each node consists of two fields-information and address. The information part contains the value that the node holds. The address part contains the address of the next node. The last node contains NULL in the address part.

    Last Updated on Saturday, 13 June 2009 08:21 Read more...
     

    LINEAR QUEUE IMPLEMENTATION USING LINKED LIST

    E-mail Print
    User Rating: / 6
    PoorBest 
    LINEAR QUEUE IMPLEMENTATION USING LINKED LIST


    Introduction :
    The linear queue is a data structure characterised by the fact that additions are made at the end, or tail, of the queue while removals are made from the front, or head, of the queue. For this reason, a queue is referred to as a FIFO structure (First-In First-Out).
    The efficient and quick way for implementing queue is through Linked List.

    Last Updated on Friday, 12 June 2009 08:06 Read more...
     

    LINEAR QUEUE IMPLEMENTATION USING ADT

    E-mail Print
    User Rating: / 4
    PoorBest 

    LINEAR QUEUE IMPLEMENTATION USING ARRAY

    Introduction :
    The linear queue is a data structure characterised by the fact that additions are made at the end, or tail, of the queue while removals are made from the front, or head, of the queue. For this reason, a queue is referred to as a FIFO structure (First-In First-Out).
    The most formal way of implementing queue is through ADT.

    Last Updated on Friday, 12 June 2009 08:07 Read more...
     

    Reversing a Linear Queue using Stack

    E-mail Print
    User Rating: / 5
    PoorBest 

    REVERSING A LINEAR QUEUE USING A STACK

     

    The objective of this article is to write a code to reverse the contents of the linear queue using a stack.

    STACK:

      A stack is a list in which all insertions and deletions are made at one end, called the tos(top of stack).The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists.

    LINEAR QUEUE:

      A linear Queue is an ordered collection of items from which items may be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (the rear of the queue). The first element to be inserted into the linear queue is deleted first. So,queues are sometimes referred to as First In First Out (FIFO) lists.

    Last Updated on Thursday, 11 June 2009 13:16 Read more...
     
    • «
    •  Start 
    •  Prev 
    •  1 
    •  2 
    •  3 
    •  4 
    •  Next 
    •  End 
    • »


    Page 1 of 4