Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help: Insert , Delate, Display in my project

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Help: Insert , Delate, Display in my project

    Someone who can help to improve my program .. I need to add an Insert, Display and Delete in my program
    but i dont know how to do that.
    the sample output just like this :

    -~-~-~-CONVERSION CHOICES-~-~-~-
    +-------------------------------+
    | [1] - Decimal |
    | [2] - Binary |
    | [3] - Octal |
    | [4] - Hexadecimal |
    | [5] - Exit |
    +-------------------------------+

    Select Number: 1
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    +-------------------------------+
    | [1] - ADD |
    | [2] - DELETE |
    | [3] - DISPLAY |
    | [4] - Back to Choices |
    | [5] - Back to Main Menu |
    +-------------------------------+
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    Enter your choice: 1
    Enter Code: 1
    Enter a Decimal Number: 200
    Successfully added!

    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    +-------------------------------+
    | [1] - ADD |
    | [2] - DELETE |
    | [3] - DISPLAY |
    | [4] - Back to Choices |
    | [5] - Back to Main Menu |
    +-------------------------------+
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    Enter your choice: 1
    Enter Code: 2
    Enter a Decimal Number: 100
    Successfully added!

    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    +-------------------------------+
    | [1] - ADD |
    | [2] - DELETE |
    | [3] - DISPLAY |
    | [4] - Back to Choices |
    | [5] - Back to Main Menu |
    +-------------------------------+
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    Enter your choice: 3
    1.The Binary equivalent of 200 is : 11001000
    The Octal equivalent of 200 is : 310
    The Hex equivalent of 200 is : C8
    2.The Binary equivalent of 100 is : 1100100
    The Octal equivalent of 100 is : 144
    The Hex equivalent of 100 is : 64

    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    +-------------------------------+
    | [1] - ADD |
    | [2] - DELETE |
    | [3] - DISPLAY |
    | [4] - Back to Choices |
    | [5] - Back to Main Menu |
    +-------------------------------+
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    Enter your choice: 2
    Enter Code: 1
    Successfully Deleted!

    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    +-------------------------------+
    | [1] - ADD |
    | [2] - DELETE |
    | [3] - DISPLAY |
    | [4] - Back to Choices |
    | [5] - Back to Main Menu |
    +-------------------------------+
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    Enter your choice: 3
    1.The Binary equivalent of 100 is : 1100100
    The Octal equivalent of 100 is : 144
    The Hex equivalent of 100 is : 64

    Here is my sample code so far.
    MainProgram.java
    import java.util.Scanner;
    public class MainProgram {
     
        public static int menu()
        {
            Scanner in = new Scanner (System.in);
     
            int ans = 0;
     
            System.out.println("\t\t-~-~-~-CONVERSION CHOICES-~-~-~-");
            System.out.println("\t\t+-------------------------------+");
            System.out.println("\t\t|       [1] - Decimal           |");
            System.out.println("\t\t|       [2] - Binary            |");
            System.out.println("\t\t|       [3] - Octal             |");
            System.out.println("\t\t|       [4] - Hexadecimal       |");
            System.out.println("\t\t|       [5] - Exit              |");
            System.out.println("\t\t+-------------------------------+");
            System.out.println("\t\t-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-");
            System.out.print("Select a Number: ");
            ans = in.nextInt();
     
            while (ans<=0 || ans >5)
            {
                System.out.println("\t\tINVALID INPUT");
                System.out.println();
                System.out.print("Enter Again: ");
                ans = in.nextInt();
            }
     
            return ans;
     
        }
        public static void main(String [] args) 
        {
            Scanner in = new Scanner (System.in);
     
            int ans = menu();
     
            while(true)
            {
                switch(ans)
                {
                    case 1:
                        int anscon1 ;
                            System.out.println();
                            System.out.println("\t\t-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-");
                            System.out.println("\t\t+-------------------------------+");
                            System.out.println("\t\t|   [1] - ADD                   |");
                            System.out.println("\t\t|   [2] - DELETE                |");
                            System.out.println("\t\t|   [3] - DISPLAY               |");
                            System.out.println("\t\t|   [4] - Back to Choices       |");
                            System.out.println("\t\t|   [5] - Back to Main Menu     |");
                            System.out.println("\t\t+-------------------------------+");
                            System.out.println("\t\t-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-");
                            System.out.print("Enter your choice:  ");
                            anscon1 = in.nextInt();
     
                            if(anscon1 == 1)
                            {
     
                            }
                            if(anscon1 == 2)
                            {
     
                            }
                            if(anscon1 == 3)
                            {
     
                            }
                            if(anscon1 == 4)
                            {
                                System.out.println("\t\t   -o-o-o-Choices-o-o-o-");
                            }
                            if(anscon1 == 5)
                            {
                                ans = menu();
                            }
                        break;

    Conversion.java
    import java.util.Scanner;
     
    public class Conversion {
     
        /*---------------------DECIMAL---------------------*/
     
        // Decimal to Binary
        public String DecToBin()
        {
            Scanner in = new Scanner (System.in);
            String numDec = " ";
            System.out.print("Enter a Decimal Number: ");
            numDec = in.nextLine();
            int binDec = Integer.parseInt(numDec,10);
            String decBin = Integer.toBinaryString(binDec);
            System.out.println("The Binary equivalent is: " + decBin);
            System.out.println("-------------------------------------");
            return decBin;
        }
     
        //Decimal to Octal
        public String DecToOct()
        {
            Scanner in = new Scanner (System.in);
            String numDec = " ";
            System.out.print("Enter a Decimal Number: ");
            numDec = in.nextLine();
            int octDec = Integer.parseInt(numDec,10);
            String decOct = Integer.toOctalString(octDec);
            System.out.println("The Octal equivalent is: " + decOct);
            System.out.println("-------------------------------------");
            return decOct;
        }
     
        //Decimal to Hexadecimal
        public String DecToHex()
        {
            Scanner in = new Scanner (System.in);
            String numDec = " ";
            System.out.print("Enter a Decimal Number: ");
            numDec = in.nextLine();
            int hexDec = Integer.parseInt(numDec,10);
            String decHex = Integer.toHexString(hexDec);
            System.out.println("The Octal equivalent is: " + decHex.toUpperCase());
            System.out.println("-------------------------------------");
            return decHex;
        }
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help: Insert , Delate, Display in my project

    The first question I'd ask is, "To what are you adding the user's input?" The answer should be, "To some kind of collection of objects that describe each data item." Your program doesn't have the data item class or the collection in which to store objects of that class, so you need to add them. The data object might be a String stored in an ArrayList<String> at the index specified by the "Code", or you could create your own class to store the data value, Code, and any/all of the conversions.

    The conversion methods you've shown are doing too much. Based on the sample run you've shown, the user enters one data value, not an Integer or a Decimal, or a ?. So your program should accept the data value in a common format in one place, store it, and then calculate the conversions on that value. This approach should require only one Scanner object for the whole program, not the several you are currently creating.

    I see you're new at using methods, not quite sure what a method should do, when to use a new one, etc. A general rule of thumb is that a method should do exactly one thing, do it very well, and the one thing the method does should be described by the method's name.

    Refactor your program some and come back when you're ready to move forward. Good luck, and keep coding!

Similar Threads

  1. Insert original title here: Hello there!
    By Leetment in forum Member Introductions
    Replies: 0
    Last Post: May 9th, 2012, 02:48 PM
  2. How to insert date in database
    By AJAXx195 in forum JDBC & Databases
    Replies: 1
    Last Post: January 24th, 2012, 03:26 PM
  3. how to insert the value to table in form
    By palani in forum AWT / Java Swing
    Replies: 4
    Last Post: September 3rd, 2010, 12:23 PM
  4. Unable to insert records
    By javaprogrammer11 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: March 24th, 2010, 03:16 AM
  5. Insert sort algorithm
    By Alysosh in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 26th, 2009, 09:28 AM