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 10 of 10

Thread: Build a database using Arrays and external file

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Build a database using Arrays and external file

    Hi there! I am a beginner in Java, I am trying to complete this program which manipulates a database of product records.
    my records are stored in a binary file and show the product name, price and quantity and look like this
    Desktop Mac £499.99 25

    I am trying to get the program to perform these 7 tasks
    • Load the contents of the file into a suitable data structure
    • Add a record
    • Find and display product details for a specified product name
    • Delete a product specified by product name
    • Change the quantity and price of a specific product
    • List products
    • Store all changes back to the binary file

    import java.io.*;
    import java.util.Scanner;
     
    public class Database {
     
        public static void main(String[] args) throws IOException {
     
            Scanner input = new Scanner(System.in);
     
            String fileName = null;
            String questionType;
            String question;
            String answerA;
            String answerB;
            String answerC;
            String answerD;
            String trueAnswer;
            int choice;
            String Delete;
     
     
            PrintWriter pWriter = null;     //clears the print writer
     
            try 
            {
                System.out.println("Your text file will be saved here: ");
                System.out.println("C:\\java\\records1.txt"); //text file will be saved in the stated directory
     
                pWriter = new PrintWriter(new FileWriter("C:\\java\\records.txt")); //where the text file is located
     
            } catch (IOException ex)
            {
                System.out.println("Doesn't exist, create a file and then try again");
            } 
     
            System.out.println("Product Database Menu");
            System.out.println("******************************************");
            System.out.println("1. Load File ");
            System.out.println("2. Add Product ");
            System.out.println("3. Find Product");
            System.out.println("4. Delete Product ");
            System.out.println("5. Change product details ");
            System.out.println("6. List Products ");
            System.out.println("7. Quit and Store ");
            System.out.println("  ***************************************  ");
            System.out.println("   Enter choice:>   ");
     
            choice = input.nextInt();
     
            if (choice == 1) 
     
            	String file_name = "C:/java/records.txt";
     
            try {
            	ReadFile file = new ReadFile (file_name);
            	String[] aryLines = file.OpenFile();
     
            	int i;
            	for (i=0; i < aryLines.length; i++) {
            		System.out.println(aryLines[i]);
            	}
            }
            catch (IOException e) {
            	System.out.println( e.getMessage() );
            }
     
                if (pWriter == null) {
     
                        System.out.println("There is nothing to Save, returning to Menu");    
                }
     
     
            } else if (choice == 2)  {
            }
     
            else if (choice == 3) { 
     
          }  
          else if (choice == 4) { 
                      {       //if statement to delte questions
     
                System.out.println("*WARNING! THIS WILL DELETE ALL records, DO YOU WISH TO CONTINUE?*");
                Delete = input.next();
     
                if (Delete.equals("Y"))  {
     
               pWriter.flush();
               } 
     
            }
          }    
          else if (choice == 5) { 
     
          }    
          else if (choice == 6) { 
     
          }    
          else if (choice == 7) { 
     
          }    
     
       }
    }           //end of program
    this is my code currently which is completely incorrect and does none of the above requirements.
    please help!
    Last edited by simc47; August 19th, 2012 at 01:43 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Build a database using Arrays and external file

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Take the list of program requirements and work on one item in the list at a time.
    Which one do you want to work on? What is your problem trying to do it?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Build a database using Arrays and external file

    ammended. sorry about that!
    I want to work on all of them Ive tried to get the loading of the code, but an error is returned.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Build a database using Arrays and external file

    Its best to work on one at a time: design it, code it, test it, repeat as needed.
    When its working, move to the next one.
    tried to get the loading of the code, but an error is returned.
    When you get an error, you need to copy the full text of the error message here and ask any questions about.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Build a database using Arrays and external file

    import java.io.*;
    import java.util.Scanner;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
     
    public class Database {
     
        public static void main(String[] args) throws IOException {
     
            Scanner input = new Scanner(System.in);
     
            String fileName = null;
            String questionType;
            String question;
            String answerA;
            String answerB;
            String answerC;
            String answerD;
            String trueAnswer;
            int choice;
            String Delete;
     
     
            PrintWriter pWriter = null;     //clears the print writer
     
            try 
            {
                System.out.println("Your text file will be saved here: ");
                System.out.println("C:\\java\\records1.txt"); //text file will be saved in the stated directory
     
                pWriter = new PrintWriter(new FileWriter("C:\\java\\records.txt")); //where the text file is located
     
            } catch (IOException ex)
            {
                System.out.println("Doesn't exist, create a file and then try again");
            } 
     
            System.out.println("Product Database Menu");
            System.out.println("******************************************");
            System.out.println("1. Load File ");
            System.out.println("2. Add Product ");
            System.out.println("3. Find Product");
            System.out.println("4. Delete Product ");
            System.out.println("5. Change product details ");
            System.out.println("6. List Products ");
            System.out.println("7. Quit and Store ");
            System.out.println("  ***************************************  ");
            System.out.println("   Enter choice:>   ");
     
            choice = input.nextInt();
     
            if (choice == 1) 
     
            	{
            File file = new File("records.txt");
            StringBuffer contents = new StringBuffer();
            BufferedReader reader = null;
     
            try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;
     
                // repeat until all lines is read
                while ((text = reader.readLine()) != null) {
                    contents.append(text)
                            .append(System.getProperty(
                                    "line.separator"));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
     
            // show file contents here
            System.out.println(contents.toString());
        }
     
     
     
             else if (choice == 2)  {
            }
     
            else if (choice == 3) { 
     
          }  
          else if (choice == 4) { 
     
          }    
          else if (choice == 5) { 
     
          }    
          else if (choice == 6) { 
     
          }    
          else if (choice == 7) { 
     
          }    
     
       }
    }           //end of program
    There is no error message now, but when i choose option 1 for reading my text file nothing is returned. is my code right for doing this?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Build a database using Arrays and external file

    nothing is returned
    Try debugging the code (see what it is doing) by adding some println statements to print out the values of variables as they are changed and used and to show the execution flow.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Build a database using Arrays and external file

    what java editor/viewer would you recommend?

    I'm not sure where to put the println statements, i'm totally lost

    thankyou for your patience and helping me by the way, it is much appreciated!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Build a database using Arrays and external file

    what java editor/viewer would you recommend?
    Sorry I don't know what a viewer. Any text editor would work for editing the source file. Some editors know about syntax and can color keywords and help with formatting. Ask google.

    What does: "nothing is returned" mean?

    One poor technique I see inthe code is the hardcoding of String values in different constructors:
                pWriter = new PrintWriter(new FileWriter("C:\\java\\records.txt")); //where the text file is located
            File file = new File("records.txt");

    You should define String variables that hold the filenames and use them in the constructors.
      String inputFN = "the input filename here";
      String outputFN = "the output filename here";
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Build a database using Arrays and external file

    well my program when compiled and ran in cmd, it prompts me to ask for an option, i choose number 1 and instead of returning my records file which looks like this: Desktop Mac £499.99 25 etc.....
    it just skips to c:/java>

    I have used the constructors now, what difference does this make may i ask?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Build a database using Arrays and external file

    what difference does this make
    It can keep you from making a stupid mistake. For example putting the input filename where the name of the output file should be.
    For example:
    new FileWriter(inputFN)
    or
    new FileReader(outputFN)
    it might not make sense to write to the input file or read from the output file.


    Add lots of println statements to the code to show where the program is executing.
    The extreme technique would be to put one after every statement in the program.
    The locations where you put them will be determined by what you want to know about how the code is executing. For example print out each line as it is read from the file.
    Last edited by Norm; August 19th, 2012 at 02:42 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. login; txt file database
    By entoke in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2012, 06:31 AM
  2. why i can't upload file to database
    By hidden_daisy in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: November 13th, 2011, 01:15 PM
  3. Accessing an external JAR file using Applet
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 12:51 AM
  4. [SOLVED] jar file Built(clean and build) perfectly, but not running as jar
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: July 11th, 2011, 11:41 AM
  5. How to build a .car file in Vignette?
    By andrew222 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 22nd, 2009, 04:49 PM