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

Thread: phone book code problems

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default phone book code problems

    THE CODE IS SUPPOSE TO PROMPT USER TO MAKE A SELECTION BY ENTERING X,Y,Z.
    THEN IF X IS ENTERED- IT SEARCHES FOR NAME IN THE TEXTFILE phonebook.txt.
    Y IS FOR EDITING THE NAMES AND Z FOR ADDING A DIFFERENT NAME


    package phonebook ;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.*;
     
    public class phonebook {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            boolean b;
            String holder;
            List<String> results = new ArrayList<String>();
            int count = 0;
            String parameter;
            String paramet;
            String Z = null ;
            String X = null ;
            String Y = null ;
     
            try{
     
                Scanner kbdin = new Scanner(System.in);
     
                System.out.println("Enter X to Search for name");
                System.out.println("Enter Y to Edit name");
                System.out.println("Enter Z to Add  name");
                paramet = in.readLine();
            if (paramet!=X){ System.out.print(paramet);
             BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter name:");
                parameter = in.readLine();
                System.out.println("");
     
                File file = new File("C:\\Users\\musa\\Documents\\NetBeansProjects\\PhoneBook\\src\\phonebook\\phonenumber.txt");
     
                Scanner s = new Scanner(file);
                while(b = s.hasNext()){
                    holder = s.next();
                    if(holder.startsWith(parameter, 0)) {
                    results.add(holder);
                    count++;
                    }
     
                }
                System.out.println("+++++++ "+count+" OUTPUT +++++++");
                if(results.isEmpty()){
     
                  System.out.println();
                  System.out.println("ZITA RASHAIKWA!!");
                  System.out.println();
                }
                for(String r : results){
                    System.out.println(r);
                }
                System.out.println("");
        }
         else if (paramet!=Y){
     
            System.out.println("Enter name to edit");
            Scanner kbdin = new Scanner(System.in);
                  Scanner editname = new Scanner(System.in);
            String filename ="phonenumber.txt";
        FileWriter targetFile = new  FileWriter(filename);
        PrintWriter writer = new PrintWriter(targetFile);
     
    writer.println(editname);
     
     
     
         }
     
    else if (paramet!=Z){
        System.out.println("Enter name to Add to contacts");
     
            Scanner kbdin = new Scanner(System.in);
                   Scanner addname = new Scanner(System.in);
           BufferedWriter out = new BufferedWriter(new FileWriter("phonebook.txt"));
        out.write("addname");
        out.close();
     
     
    }
     
     
     
    else {
            System.out.println("INVALID SELECTION!!");
    }   }
     
             catch(IOException e) {}}
            // TODO code application logic here
        }

    The compile output is:

    run:
    Enter X to Search for name
    Enter Y to Edit name
    Enter Z to Add name
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
    at phonebook.phonebook.main(phonebook.java:39)
    Java Result:
    Last edited by mu'min; December 16th, 2010 at 08:55 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: phone book code problems

    When posting code, make sure you use the code tags. Take a look at other posts as examples.

    That isn't compiler output. That's a runtime error. What happens when you compile without running? Which line is line 39?

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: phone book code problems

    There are quite a few errors in your code!!

    For a start, you have declared the Scanner kbdin multiple times. Why?
    You can easily do this with just 1 Scanner.

    Also, this is wrong:

    paramet = in.readLine();

    If this is meant for the Scanner, the method readLine is undefined. Try .nextLine();
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Source code for Email address book/contacts importer
    By jega004 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 23rd, 2012, 12:49 PM
  2. Problems with Code?
    By Sean137 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2010, 03:15 PM
  3. phone book
    By Skat in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 3rd, 2010, 09:56 AM
  4. [SOLVED] how to get phone time and date
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: August 26th, 2009, 11:15 AM