help with a java program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: help with a java program

  1. #1
    Join Date
    Jul 2009
    Posts
    4

    Post help with a java program

    I need to create a program that will read names and phone numbers from a text file. I then need to search for a phone number by providing a last name, I will also need to be able to add a name and phone number and also be able to delete a name and a phone number.

    Can anyone help me please?

    This is what I have so far:

    File file = new File("phones.txt");

    try {

    scanner = new Scanner(file);

    while (scanner.hasNextLine()) {

    String line = scanner.nextLine();

    System.out.println(line);

    }//end of while

    } catch (FileNotFoundException e) {

    e.printStackTrace();

    }

    All this does is read from a text file and then print the names and phone numbers. I need to be able to search for similar last names in that text file and provide the full names and phone numbers for that last name. I really need help with this !!

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    What is your plan of attack?

    Are you assured of the file structure of the text file? Is there a delimiter contained in each line?

    Do you understand what the Scanner class does with the records that you read in? Do you know that you can use methods of the class to capture each part of the imput line and have a String which can be used by your program?

    How are you going to interface with the user to ask how he/she wants to use it at that time?

    How are you going to store the data read from the input file (what data structgure are you going to use?) What is your plan of attack for finding the right name and phone number? For adding a new entry to your collection

    Lots to think about and plan out!

  3. #3
    Join Date
    Jul 2009
    Posts
    4

    Post help with a java program

    Well I do have a plan and i am doing good so far,
    but what i need this time is to be able to read just a part of the string.
    for ex:
    Smith;John;3337122

    i need to be able to read just smith and then read just john and then the phone number. is there something i could use to recognize the semi colon and just read what's in front of the semi colon and do the same for the second name.

    the other problem i have is when i add a record on the file, the originals get lost, how do i add a record without losing the original records?

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Do you know how Scanner works? By default, it will make an array of stings delimited by a NewLine/Enter code. So each entity in the array will be a sting of a line like "Smith; John; 337122". You can then have another Scanner object which will delimit based on the semi-colon that separate the "words" of the line. You will then have an array that is [Smith John 337122]. Then, while (hasNext() ) { next(); then assign to a variable to be included in your entry in your data structure } will read each item.

    You need to store your records in a data structure as you read them. What kind of data structure will give you the functionality that you need.

  5. #5
    Join Date
    Jul 2009
    Posts
    4

    Post help with a java program

    I get it now, thanks alot.
    but do you know how to write on a file without losing what's already on the file? because whenever I write on my file, I lose everything that was on it already.

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    It sounds as if you are writing to a file on a disk - is this right? If so, you want to open your output file "to append". There is usually a boolean switch for the API classes which write files to set "append" to "true". The default is false so it will write at the start of the file - erasing the old content - rather than the end of the file.

    Are you required to write sequentially to disc? Isn't it better to have entries kept in memory so you can work more quickly with them? Are you really set up to read and write directly to disc?

  7. #7
    Join Date
    Jul 2009
    Posts
    4

    Post help with a java program

    Hey, I was wondering if I could send you the program specification so you can write the codes for the whole program. Then you can send it to me so I can compare to my codes. What do you think?

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I don't think so. I will help you with concepts. I will look over code. I will even correct or suggest some content. I will not write the project for you so you can represent to whomever it is that you are submitting the project that it is your work.

Similar Threads

  1. Java book
    By Lou in forum Java
    Replies: 9
    Last Post: 09-19-2007, 05:58 AM
  2. Help! Simple Java Program.
    By SlickWilly440 in forum Java
    Replies: 2
    Last Post: 01-28-2006, 09:17 PM
  3. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 02:57 PM
  4. Has Sun Given Up on the Desktop?
    By Lori Piquet in forum Talk to the Editors
    Replies: 114
    Last Post: 10-10-2002, 06:01 AM
  5. .NET vs. Enterprise Java: Who's Got Better Security?
    By Glen Kunene in forum Talk to the Editors
    Replies: 17
    Last Post: 03-23-2002, 12:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center