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

Thread: What's wrong with my code :(

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post What's wrong with my code :(

    Can someone help me about my system. Im having trouble about my code. I cant figure out how it works. Lets see. Soo its about File class. How am i be able to compare string to notepad data. I've tried this code:


    import java.util.*;
    import java.io.*;
    import java.io.File;
     
    public class DeleteF {
        Scanner in = new Scanner(System.in);
        File file = new File("C:\\Documents and Settings\\user\\My Documents"
                + "\\NetBeansProjects\\StudentInformation\\src\\information.txt");
     
        ArrayList store = new ArrayList();
        String idM, record, id, temp;
        int match = 0;
     
     
        public void delete() throws Exception{
            Scanner inFile = new Scanner(file);
            inFile.useDelimiter("#");
     
            System.out.print("ID: ");
            idM = in.nextLine();
     
     
            while(inFile.hasNext()){
                  record = inFile.nextLine();
    //            System.out.println(record);
     
                  if(inFile.hasNext()){
                      inFile.nextLine();
     
                    if(idM.equals(inFile.next())){
                     System.out.println("LOL");        
                     }
     
                    else{           
                        System.out.println("No records found!");
     
                      }
     
     
                  }
     
     
                  store.add(record);
     
            }//end of while
     
     
            for(int x = 0; x < store.size(); x++){
                System.out.println(store.get(x));
     
     
            }
     
     
     
        }//end of delete()
     
     
    }//end of DeleteF class



    My problem is, the user should enter an existing ID. And when it matches to the ID from the notepad, it will be deleted ..



    1.JPG


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

    Default Re: What's wrong with my code :(

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

    My problem is, the user should enter an existing ID
    Can you train the user so he only enters good data? How does the user know what good data is? Should the program display all the IDs and allow the user to chose one?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ZDreamer08 (April 27th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code :(

    Can you train the user so he only enters good data? How does the user know what good data is? Should the program display all the IDs and allow the user to chose one?

    sorry. i've just joined today.

    The output for that is:

    [1] Add
    [2] Delete
    [3] Edit
    [4] View
    [5] Exit

    choice: _____


    the user will choose from the following menu. and im currently working at the DELETE FUNCTION. on the delete function, the user will input an ID which is already existed on the notepad that he wants to delete.

    example:

    This is the data on the notepad.
    1.JPG

    the user wanted to delete the 1#Zsannen Mariano#3rd Year#, he will enter the ID number. which is number 1. and when he enter it, the whole information the ID number 1 will be deleted. the main menu:

    [1] Add
    [2] Delete
    [3] Edit
    [4] View
    [5] Exit

    choice: _____

    will prompt again and ask the user to choose. if he choose number 4 - VIEW .. the data from the notepad will be displayed and the deleted data earlier will not be included on the notepad.


    Hope you understand my explanation

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What's wrong with my code :(

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

    What does the code do now when it is executed? Can you copy the full contents of the console window and paste it here from when the program is executed and add some comments describing what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code :(

    and i've edited the code.

    import java.util.*;
    import java.io.*;
    import java.io.File;
    import java.io.PrintWriter;
     
    public class DeleteF {
        Scanner in = new Scanner(System.in);
        File file = new File("C:\\Documents and Settings\\user\\My Documents"
                + "\\NetBeansProjects\\StudentInformation\\src\\information.txt");
     
        ArrayList store = new ArrayList();
        String idM, record, id, temp;
        int match = 0;
     
     
        public void delete() throws Exception{
            Scanner inFile = new Scanner(file);
            inFile.useDelimiter("#");
     
            System.out.print("ID: ");
            idM = in.nextLine();
     
     
            while(inFile.hasNext()){
                  record = inFile.nextLine();
    //            System.out.println(record);
     
                    if(inFile.hasNext()){
                        inFile.nextLine();
     
                        if(idM.equals(inFile.next())){
                        System.out.println(inFile.next());  
                        }
     
                        else{
                        store.add(record);
                        }
                    }//end of checking line condition
            }//end of while
     
            file.delete();
     
            file.createNewFile();
            PrintWriter pw = new PrintWriter(file);
     
     
            for(int x = 0; x < store.size(); x++){
                pw.print(store.get(x));
                pw.println();
     
     
            }
     
     
            pw.close();
     
        }//end of delete()
     
     
    }//end of DeleteF class



    1.JPG -- FIRST 2.JPG -- SECOND

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What's wrong with my code :(

    How is the code executed for testing? There is no main() method.

    where are the rest of the classes that are needed to execute the program?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code :(

    The main method is in another class. The add, delete, edit, view and exit functions are just methods that has been called on the main class. I've separated it and instantiate it on the main.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What's wrong with my code :(

    What specific questions do you have about the posted code?

    How can the code be executed for testing if needed?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What's wrong with my code?
    By cbplayer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 13th, 2013, 03:07 PM
  2. what is wrong with my code
    By BLUEJ in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 10th, 2013, 07:52 PM
  3. What is wrong with my code??
    By joes71monte in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 27th, 2012, 11:56 AM
  4. What is Wrong with my code
    By xew123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:59 AM
  5. What's wrong with my code?
    By lindmando in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2011, 11:13 PM

Tags for this Thread